From cc5309b6a656e58b16bfd7f0b32bdd172deaaa47 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 22:26:34 +0700 Subject: [PATCH] Replace Adapter with Queue --- src/Middleware/AdapterHandler.php | 2 +- .../FailureHandlingRequest.php | 21 ------ src/Middleware/Request.php | 15 +---- src/Middleware/SendAgainMiddleware.php | 4 +- src/Queue.php | 2 +- src/Worker/Worker.php | 2 +- tests/App/FakeQueue.php | 64 +++++++++++++++++++ .../Middleware/Consume/ConsumeRequestTest.php | 8 +-- .../Consume/MiddlewareDispatcherTest.php | 7 +- .../Consume/MiddlewareFactoryTest.php | 5 +- .../MiddlewareDispatcherTest.php | 2 +- .../FailureHandling/MiddlewareFactoryTest.php | 3 +- .../Middleware/MiddlewareDispatcherTest.php | 34 +++++++--- .../Unit/Middleware/MiddlewareFactoryTest.php | 8 ++- .../{PushRequestTest.php => RequestTest.php} | 9 +-- 15 files changed, 121 insertions(+), 65 deletions(-) create mode 100644 tests/App/FakeQueue.php rename tests/Unit/Middleware/{PushRequestTest.php => RequestTest.php} (50%) diff --git a/src/Middleware/AdapterHandler.php b/src/Middleware/AdapterHandler.php index 7564016b..7447280c 100644 --- a/src/Middleware/AdapterHandler.php +++ b/src/Middleware/AdapterHandler.php @@ -13,7 +13,7 @@ final class AdapterHandler implements MessageHandlerInterface { public function handle(Request $request): Request { - if (($adapter = $request->getAdapter()) === null) { + if (($adapter = $request->getQueue()?->getAdapter()) === null) { throw new AdapterNotConfiguredException(); } return $request->withMessage($adapter->push($request->getMessage())); diff --git a/src/Middleware/FailureHandling/FailureHandlingRequest.php b/src/Middleware/FailureHandling/FailureHandlingRequest.php index 418e4552..c68a62ab 100644 --- a/src/Middleware/FailureHandling/FailureHandlingRequest.php +++ b/src/Middleware/FailureHandling/FailureHandlingRequest.php @@ -21,25 +21,4 @@ public function getException(): ?Throwable { return $this->exception; } - - public function getQueue(): QueueInterface - { - return $this->queue; - } - - public function withException(Throwable $exception): self - { - $instance = clone $this; - $instance->exception = $exception; - - return $instance; - } - - public function withQueue(QueueInterface $queue): self - { - $instance = clone $this; - $instance->queue = $queue; - - return $instance; - } } diff --git a/src/Middleware/Request.php b/src/Middleware/Request.php index ebcba055..c6ef7285 100644 --- a/src/Middleware/Request.php +++ b/src/Middleware/Request.php @@ -12,8 +12,7 @@ //final class Request class Request { - protected ?QueueInterface $queue = null; - public function __construct(private MessageInterface $message, private ?AdapterInterface $adapter) + public function __construct(private MessageInterface $message, private ?QueueInterface $queue) { } @@ -22,11 +21,6 @@ public function getMessage(): MessageInterface return $this->message; } - public function getAdapter(): ?AdapterInterface - { - return $this->adapter; - } - public function withMessage(MessageInterface $message): self { $instance = clone $this; @@ -35,12 +29,9 @@ public function withMessage(MessageInterface $message): self return $instance; } - public function withAdapter(AdapterInterface $adapter): self + public function getQueue(): ?QueueInterface { - $instance = clone $this; - $instance->adapter = $adapter; - - return $instance; + return $this->queue; } public function withQueue(QueueInterface $queue): self diff --git a/src/Middleware/SendAgainMiddleware.php b/src/Middleware/SendAgainMiddleware.php index 242415a6..e514de53 100644 --- a/src/Middleware/SendAgainMiddleware.php +++ b/src/Middleware/SendAgainMiddleware.php @@ -36,8 +36,8 @@ public function process(Request $request, MessageHandlerInterface $handler): Req $envelope = new FailureEnvelope($message, $this->createMeta($message)); $envelope = $this->queue->push($envelope); - $request1 = $request->withMessage($envelope); - return $request1->withQueue($this->queue); + $request = $request->withMessage($envelope); + return $request->withQueue($this->queue); } return $handler->handle($request); diff --git a/src/Queue.php b/src/Queue.php index b3f272d2..12a22430 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -53,7 +53,7 @@ public function push( ['data' => $message->getData(), 'metadata' => $message->getMetadata()] ); - $request = new Request($message, $this->adapter); + $request = new Request($message, $this); $message = $this->pushMiddlewareDispatcher ->dispatch($request, $this->createHandler($middlewareDefinitions)) ->getMessage(); diff --git a/src/Worker/Worker.php b/src/Worker/Worker.php index c26eaa55..ba27de75 100644 --- a/src/Worker/Worker.php +++ b/src/Worker/Worker.php @@ -63,7 +63,7 @@ public function process(MessageInterface $message, QueueInterface $queue): Messa )); } - $request = new Request($message, $queue->getAdapter()); + $request = new Request($message, $queue); $closure = fn (MessageInterface $message): mixed => $this->injector->invoke([$handler, 'handle'], [$message]); try { $result = $this->consumeMiddlewareDispatcher->dispatch($request, new ConsumeFinalHandler($closure)); diff --git a/tests/App/FakeQueue.php b/tests/App/FakeQueue.php new file mode 100644 index 00000000..dabaad68 --- /dev/null +++ b/tests/App/FakeQueue.php @@ -0,0 +1,64 @@ +adapter = $adapter; + + return $new; + } + + public function getChannelName(): string + { + return $this->channelName; + } + + public function withChannelName(string $channel): self + { + throw new Exception('`withChannelName()` method is not implemented yet.'); + } + + public function getAdapter(): ?AdapterInterface + { + return $this->adapter; + } +} diff --git a/tests/Unit/Middleware/Consume/ConsumeRequestTest.php b/tests/Unit/Middleware/Consume/ConsumeRequestTest.php index 49bf511b..b7d4b8a1 100644 --- a/tests/Unit/Middleware/Consume/ConsumeRequestTest.php +++ b/tests/Unit/Middleware/Consume/ConsumeRequestTest.php @@ -4,9 +4,9 @@ namespace Yiisoft\Queue\Tests\Unit\Middleware\Consume; -use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Middleware\Request; +use Yiisoft\Queue\QueueInterface; use Yiisoft\Queue\Tests\TestCase; final class ConsumeRequestTest extends TestCase @@ -14,10 +14,10 @@ final class ConsumeRequestTest extends TestCase public function testImmutable(): void { $message = new Message('test'); - $adapter = $this->createMock(AdapterInterface::class); - $consumeRequest = new Request($message, $adapter); + $queue = $this->createMock(QueueInterface::class); + $consumeRequest = new Request($message, $queue); $this->assertNotSame($consumeRequest, $consumeRequest->withMessage($message)); - $this->assertNotSame($consumeRequest, $consumeRequest->withAdapter($adapter)); + $this->assertNotSame($consumeRequest, $consumeRequest->withQueue($queue)); } } diff --git a/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php b/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php index 668f851a..42e18016 100644 --- a/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php +++ b/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php @@ -25,12 +25,11 @@ public function testCallableMiddlewareCalled(): void { $request = $this->getRequest(); $queue = $this->createMock(QueueInterface::class); - $adapter = $this->createMock(AdapterInterface::class); $dispatcher = $this->createDispatcher()->withMiddlewares( [ - static function (Request $request) use ($adapter): Request { - return $request->withMessage(new Message('New closure test data'))->withAdapter($adapter); + static function (Request $request) use ($queue): Request { + return $request->withMessage(new Message('New closure test data'))->withQueue($queue); }, ] ); @@ -180,7 +179,7 @@ private function getRequest(): Request { return new Request( new Message('data'), - $this->createMock(AdapterInterface::class) + $this->createMock(QueueInterface::class) ); } } diff --git a/tests/Unit/Middleware/Consume/MiddlewareFactoryTest.php b/tests/Unit/Middleware/Consume/MiddlewareFactoryTest.php index a73a6267..39bfd6f4 100644 --- a/tests/Unit/Middleware/Consume/MiddlewareFactoryTest.php +++ b/tests/Unit/Middleware/Consume/MiddlewareFactoryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use Yiisoft\Queue\QueueInterface; use Yiisoft\Queue\Tests\Integration\Support\ConsumeMiddleware; use Yiisoft\Queue\Tests\Unit\Middleware\Support\TestCallableMiddleware; use Yiisoft\Test\Support\Container\SimpleContainer; @@ -58,7 +59,7 @@ public function testCreateFromClosureResponse(): void $middleware = $this->getMiddlewareFactory($container)->createMiddleware( fn (): Request => new Request( new Message('test data'), - $this->createMock(AdapterInterface::class), + $this->createMock(QueueInterface::class), ) ); self::assertSame( @@ -201,7 +202,7 @@ private function getRequest(): Request { return new Request( new Message(['data']), - $this->createMock(AdapterInterface::class) + $this->createMock(QueueInterface::class) ); } } diff --git a/tests/Unit/Middleware/FailureHandling/MiddlewareDispatcherTest.php b/tests/Unit/Middleware/FailureHandling/MiddlewareDispatcherTest.php index 42b7170d..024e9eef 100644 --- a/tests/Unit/Middleware/FailureHandling/MiddlewareDispatcherTest.php +++ b/tests/Unit/Middleware/FailureHandling/MiddlewareDispatcherTest.php @@ -168,7 +168,7 @@ private function getRequest(): Request { return new Request( new Message('data'), - $this->createMock(AdapterInterface::class) + $this->createMock(QueueInterface::class) ); } } diff --git a/tests/Unit/Middleware/FailureHandling/MiddlewareFactoryTest.php b/tests/Unit/Middleware/FailureHandling/MiddlewareFactoryTest.php index 62955f84..795562da 100644 --- a/tests/Unit/Middleware/FailureHandling/MiddlewareFactoryTest.php +++ b/tests/Unit/Middleware/FailureHandling/MiddlewareFactoryTest.php @@ -14,6 +14,7 @@ use Yiisoft\Queue\Middleware\MiddlewareFactoryInterface; use Yiisoft\Queue\Middleware\MiddlewareInterface; use Yiisoft\Queue\Middleware\Request; +use Yiisoft\Queue\QueueInterface; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Message\Message; @@ -182,7 +183,7 @@ private function getConsumeRequest(): Request { return new Request( new Message('data'), - $this->createMock(AdapterInterface::class) + $this->createMock(QueueInterface::class) ); } } diff --git a/tests/Unit/Middleware/MiddlewareDispatcherTest.php b/tests/Unit/Middleware/MiddlewareDispatcherTest.php index a6cf7e46..9196b672 100644 --- a/tests/Unit/Middleware/MiddlewareDispatcherTest.php +++ b/tests/Unit/Middleware/MiddlewareDispatcherTest.php @@ -6,6 +6,8 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use Yiisoft\Queue\QueueInterface; +use Yiisoft\Queue\Tests\App\FakeQueue; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Message\Message; @@ -26,9 +28,15 @@ public function testCallableMiddlewareCalled(): void $dispatcher = $this->createDispatcher()->withMiddlewares( [ - static fn (Request $request, AdapterInterface $adapter): Request => $request + static fn (Request $request): Request => $request ->withMessage(new Message('New closure test data')) - ->withAdapter($adapter->withChannel('closure-channel')), + ->withQueue( + $request->getQueue()->getAdapter() === null + ? $request->getQueue() + : $request->getQueue()->withAdapter( + $request->getQueue()->getAdapter()->withChannel('closure-channel') + ) + ), ] ); @@ -38,7 +46,7 @@ public function testCallableMiddlewareCalled(): void * @psalm-suppress NoInterfaceProperties * @psalm-suppress PossiblyNullPropertyFetch */ - $this->assertSame('closure-channel', $request->getAdapter()->channel); + $this->assertSame('closure-channel', $request->getQueue()->getAdapter()->channel); } public function testArrayMiddlewareCallableDefinition(): void @@ -69,8 +77,6 @@ public function testFactoryArrayDefinition(): void public function testMiddlewareFullStackCalled(): void { - $request = $this->getRequest(); - $middleware1 = static function (Request $request, MessageHandlerInterface $handler): Request { $request = $request->withMessage($request->getMessage()->withData('new test data')); @@ -82,20 +88,31 @@ public function testMiddlewareFullStackCalled(): void * * @psalm-suppress PossiblyNullReference */ - $request = $request->withAdapter($request->getAdapter()->withChannel('new channel')); + $queue = $request->getQueue(); + if ($queue !== null && $queue->getAdapter() !== null) { + $request = $request->withQueue( + $queue->withAdapter( + $queue->getAdapter()->withChannel('new channel') + ) + ); + } return $handler->handle($request); }; $dispatcher = $this->createDispatcher()->withMiddlewares([$middleware1, $middleware2]); + $request = $this->getRequest(); $request = $dispatcher->dispatch($request, $this->getRequestHandler()); $this->assertSame('new test data', $request->getMessage()->getData()); /** * @psalm-suppress NoInterfaceProperties * @psalm-suppress PossiblyNullPropertyFetch */ - $this->assertSame('new channel', $request->getAdapter()->channel); + $this->assertNotNull($request->getQueue()); + $this->assertNotNull($request->getQueue()->getAdapter()); + $this->assertInstanceOf(FakeAdapter::class, $request->getQueue()->getAdapter()); + $this->assertSame('new channel', $request->getQueue()->getAdapter()->channel); } public function testMiddlewareStackInterrupted(): void @@ -185,6 +202,7 @@ private function createContainer(array $instances = []): ContainerInterface private function getRequest(): Request { - return new Request(new Message('data'), new FakeAdapter()); + $queue = new FakeQueue('chan1'); + return new Request(new Message('data'), $queue->withAdapter(new FakeAdapter())); } } diff --git a/tests/Unit/Middleware/MiddlewareFactoryTest.php b/tests/Unit/Middleware/MiddlewareFactoryTest.php index 7709932d..b413880e 100644 --- a/tests/Unit/Middleware/MiddlewareFactoryTest.php +++ b/tests/Unit/Middleware/MiddlewareFactoryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use Yiisoft\Queue\QueueInterface; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Message\Message; @@ -46,9 +47,10 @@ public function testCreateCallableFromArray(): void public function testCreateFromClosureResponse(): void { $container = $this->getContainer([TestCallableMiddleware::class => new TestCallableMiddleware()]); + $queue = $this->createMock(QueueInterface::class); $middleware = $this->getMiddlewareFactory($container)->createMiddleware( - static function (): Request { - return new Request(new Message('test data'), new FakeAdapter()); + static function () use($queue): Request { + return new Request(new Message('test data'), $queue); } ); self::assertSame( @@ -167,6 +169,6 @@ public function handle(Request $request): Request private function getRequest(): Request { - return new Request(new Message('data'), new FakeAdapter()); + return new Request(new Message('data'), $this->createMock(QueueInterface::class)); } } diff --git a/tests/Unit/Middleware/PushRequestTest.php b/tests/Unit/Middleware/RequestTest.php similarity index 50% rename from tests/Unit/Middleware/PushRequestTest.php rename to tests/Unit/Middleware/RequestTest.php index d5ad92a6..93a47b82 100644 --- a/tests/Unit/Middleware/PushRequestTest.php +++ b/tests/Unit/Middleware/RequestTest.php @@ -6,17 +6,18 @@ use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Middleware\Request; +use Yiisoft\Queue\QueueInterface; use Yiisoft\Queue\Tests\App\FakeAdapter; use Yiisoft\Queue\Tests\TestCase; -final class PushRequestTest extends TestCase +final class RequestTest extends TestCase { public function testImmutable(): void { $message = new Message('test'); - $Request = new Request($message, new FakeAdapter()); + $request = new Request($message, $this->createMock(QueueInterface::class)); - $this->assertNotSame($Request, $Request->withAdapter(new FakeAdapter())); - $this->assertNotSame($Request, $Request->withMessage($message)); + $this->assertNotSame($request, $request->withQueue($this->createMock(QueueInterface::class))); + $this->assertNotSame($request, $request->withMessage($message)); } }