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); } }