From 530bf5647631868da0eded9260f7bb4491e629b7 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 1 Apr 2021 15:36:12 +0300 Subject: [PATCH] tests, null for ttl --- .../Middleware/LockableMessageMiddleware.php | 12 +- Tests/DataFixtures/TestMessage.php | 3 +- .../LockableMessageMiddlewareTest.php | 113 ++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php diff --git a/Messenger/Middleware/LockableMessageMiddleware.php b/Messenger/Middleware/LockableMessageMiddleware.php index 08bebcb..fc27882 100644 --- a/Messenger/Middleware/LockableMessageMiddleware.php +++ b/Messenger/Middleware/LockableMessageMiddleware.php @@ -39,7 +39,7 @@ class LockableMessageMiddleware implements MiddlewareInterface $message = $envelope->getMessage(); if ($envelope->all(ReceivedStamp::class) && $message instanceof LockableMessage) { - $lock = $this->lockFactory->createLock(md5(serialize($message))); + $lock = $this->lockFactory->createLock($this->hash($message), null); if (!$lock->acquire()) { return $envelope; } @@ -55,4 +55,14 @@ class LockableMessageMiddleware implements MiddlewareInterface return $stack->next()->handle($envelope, $stack); } + + /** + * @param LockableMessage $message + * + * @return string + */ + private function hash(LockableMessage $message): string + { + return md5(serialize($message)); + } } diff --git a/Tests/DataFixtures/TestMessage.php b/Tests/DataFixtures/TestMessage.php index b69877e..1884b1d 100644 --- a/Tests/DataFixtures/TestMessage.php +++ b/Tests/DataFixtures/TestMessage.php @@ -3,8 +3,9 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures; use RetailCrm\ServiceBundle\Messenger\CommandMessage; +use RetailCrm\ServiceBundle\Messenger\Middleware\LockableMessage; -class TestMessage extends CommandMessage +class TestMessage extends CommandMessage implements LockableMessage { public function __construct() { diff --git a/Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php b/Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php new file mode 100644 index 0000000..ea74f78 --- /dev/null +++ b/Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php @@ -0,0 +1,113 @@ +lockFactory = $this->createMock(LockFactory::class); + } + + public function testHandle(): void + { + $store = $this->createMock(PersistingStoreInterface::class); + $key = new Key(uniqid()); + $lock = new Lock($key, $store); + $this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock); + $envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]); + + $next = $this->createMock(MiddlewareInterface::class); + $next->method('handle')->willReturn($envelope); + $stack = $this->createMock(StackInterface::class); + $stack->method('next')->willReturn($next); + + $middleware = new LockableMessageMiddleware($this->lockFactory); + $result = $middleware->handle($envelope, $stack); + + static::assertInstanceOf(Envelope::class, $result); + } + + public function testLockHandle(): void + { + $store = $this->createMock(PersistingStoreInterface::class); + $store->method('save')->willThrowException(new LockConflictedException); + $key = new Key(uniqid()); + $lock = new Lock($key, $store); + $this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock); + $envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]); + + $next = $this->createMock(MiddlewareInterface::class); + $next->method('handle')->willReturn($envelope); + $stack = $this->createMock(StackInterface::class); + $stack->method('next')->willReturn($next); + + $middleware = new LockableMessageMiddleware($this->lockFactory); + $result = $middleware->handle($envelope, $stack); + + static::assertInstanceOf(Envelope::class, $result); + } + + public function testNonLockableHandle(): void + { + $store = $this->createMock(PersistingStoreInterface::class); + $store->method('save')->willThrowException(new LockConflictedException); + $key = new Key(uniqid()); + $lock = new Lock($key, $store); + $this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock); + $envelope = new Envelope(new \stdClass(), [new ReceivedStamp('test')]); + + $next = $this->createMock(MiddlewareInterface::class); + $next->method('handle')->willReturn($envelope); + $stack = $this->createMock(StackInterface::class); + $stack->method('next')->willReturn($next); + + $middleware = new LockableMessageMiddleware($this->lockFactory); + $result = $middleware->handle($envelope, $stack); + + static::assertInstanceOf(Envelope::class, $result); + } + + public function testNonReceivedHandle(): void + { + $store = $this->createMock(PersistingStoreInterface::class); + $store->method('save')->willThrowException(new LockConflictedException); + $key = new Key(uniqid()); + $lock = new Lock($key, $store); + $this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock); + $envelope = new Envelope(new TestMessage()); + + $next = $this->createMock(MiddlewareInterface::class); + $next->method('handle')->willReturn($envelope); + $stack = $this->createMock(StackInterface::class); + $stack->method('next')->willReturn($next); + + $middleware = new LockableMessageMiddleware($this->lockFactory); + $result = $middleware->handle($envelope, $stack); + + static::assertInstanceOf(Envelope::class, $result); + } +}