-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
/.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
[], | ||
); | ||
} | ||
} |