Skip to content

Commit

Permalink
Add basic unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Oct 17, 2023
1 parent 523c51a commit 430cbb0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
/.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"Webgriffe\\PayumLockRequestExtension\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Webgriffe\\PayumLockRequestExtension\\": "tests/"
}
},
"authors": [
{
"name": "Webgriffe SRL",
Expand Down
36 changes: 36 additions & 0 deletions tests/InMemoryStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\PayumLockRequestExtension;

use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;

final class InMemoryStore implements PersistingStoreInterface
{
public function __construct(
public array $locks = [],
) {
}

public function save(Key $key): void
{
$this->locks[(string) $key] = $key;
}

public function delete(Key $key): void
{
unset($this->locks[(string) $key]);
}

public function exists(Key $key): bool
{
return array_key_exists((string) $key, $this->locks);
}

public function putOffExpiration(Key $key, float $ttl): void
{
// do nothing, memory locks forever.
}
}
53 changes: 53 additions & 0 deletions tests/LockRequestExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\PayumLockRequestExtension;

use Payum\Core\Extension\Context;
use Payum\Core\Gateway;
use Payum\Core\Model\Identity;
use Payum\Core\Model\Token;
use Payum\Core\Request\Capture;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\LockFactory;
use Webgriffe\PayumLockRequestExtension\LockRequestExtension;
use PHPUnit\Framework\TestCase;

class LockRequestExtensionTest extends TestCase
{
private LockRequestExtension $extension;

private InMemoryStore $lockStore;

protected function setUp(): void
{
$this->lockStore = new InMemoryStore();
$this->extension = new LockRequestExtension(
new LockFactory($this->lockStore),
);
}

public function test_it_locks_requests(): void
{
$context = $this->createContext();
$this->extension->onPreExecute($context);

self::assertTrue($this->lockStore->exists(new Key('webgriffe_payum_lock_request_extension_PaymentClass#5')));

$this->extension->onPostExecute($context);

self::assertFalse($this->lockStore->exists(new Key('webgriffe_payum_lock_request_extension_PaymentClass#5')));
}

private function createContext(): Context
{
$paymentToken = new Token();
$paymentToken->setDetails(new Identity(5, 'PaymentClass'));
return new Context(
new Gateway(),
new Capture($paymentToken),
[],
);
}
}

0 comments on commit 430cbb0

Please sign in to comment.