From 05f50eb941c94769e36e2459e206f4dcb349a778 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Sun, 24 Nov 2024 10:52:20 -0500 Subject: [PATCH] minor(history): split `HistoryListener` --- config/storage_orm.php | 18 ++- .../ZenstruckMessengerMonitorExtension.php | 4 +- src/EventListener/AddMonitorStampListener.php | 28 ++++ .../HandleMonitorStampListener.php} | 73 +-------- .../ReceiveMonitorStampListener.php | 85 ++++++++++ ...ZenstruckMessengerMonitorExtensionTest.php | 16 +- .../AddMonitorStampListenerTest.php | 40 +++++ .../HandleMonitorStampListenerTest.php | 100 ++++++++++++ .../ReceiveMonitorStampListenerTest.php} | 151 ++++-------------- tests/Unit/History/Model/ResultsTest.php | 2 +- 10 files changed, 311 insertions(+), 206 deletions(-) create mode 100644 src/EventListener/AddMonitorStampListener.php rename src/{History/HistoryListener.php => EventListener/HandleMonitorStampListener.php} (54%) create mode 100644 src/EventListener/ReceiveMonitorStampListener.php create mode 100644 tests/Unit/EventListener/AddMonitorStampListenerTest.php create mode 100644 tests/Unit/EventListener/HandleMonitorStampListenerTest.php rename tests/Unit/{History/HistoryListenerTest.php => EventListener/ReceiveMonitorStampListenerTest.php} (56%) diff --git a/config/storage_orm.php b/config/storage_orm.php index 3f3d2e5..ce78c26 100644 --- a/config/storage_orm.php +++ b/config/storage_orm.php @@ -8,7 +8,9 @@ use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; use Zenstruck\Messenger\Monitor\Command\PurgeCommand; use Zenstruck\Messenger\Monitor\Command\SchedulePurgeCommand; -use Zenstruck\Messenger\Monitor\History\HistoryListener; +use Zenstruck\Messenger\Monitor\EventListener\AddMonitorStampListener; +use Zenstruck\Messenger\Monitor\EventListener\HandleMonitorStampListener; +use Zenstruck\Messenger\Monitor\EventListener\ReceiveMonitorStampListener; use Zenstruck\Messenger\Monitor\History\ResultNormalizer; use Zenstruck\Messenger\Monitor\History\Storage; use Zenstruck\Messenger\Monitor\History\Storage\ORMStorage; @@ -29,14 +31,20 @@ ->set('.zenstruck_messenger_monitor.history.result_normalizer', ResultNormalizer::class) ->args([param('kernel.project_dir')]) - ->set('.zenstruck_messenger_monitor.history.listener', HistoryListener::class) + ->set('.zenstruck_messenger_monitor.listener.add_monitor_stamp', AddMonitorStampListener::class) + ->tag('kernel.event_listener', ['method' => '__invoke', 'event' => SendMessageToTransportsEvent::class]) + + ->set('.zenstruck_messenger_monitor.listener.receive_monitor_stamp', ReceiveMonitorStampListener::class) + ->args([ + abstract_arg('exclude_classes') + ]) + ->tag('kernel.event_listener', ['method' => '__invoke', 'event' => WorkerMessageReceivedEvent::class]) + + ->set('.zenstruck_messenger_monitor.listener.handle_monitor_stamp', HandleMonitorStampListener::class) ->args([ service('zenstruck_messenger_monitor.history.storage'), service('.zenstruck_messenger_monitor.history.result_normalizer'), - abstract_arg('exclude_classes'), ]) - ->tag('kernel.event_listener', ['method' => 'addMonitorStamp', 'event' => SendMessageToTransportsEvent::class]) - ->tag('kernel.event_listener', ['method' => 'receiveMessage', 'event' => WorkerMessageReceivedEvent::class]) ->tag('kernel.event_listener', ['method' => 'handleSuccess', 'event' => WorkerMessageHandledEvent::class]) ->tag('kernel.event_listener', ['method' => 'handleFailure', 'event' => WorkerMessageFailedEvent::class]) diff --git a/src/DependencyInjection/ZenstruckMessengerMonitorExtension.php b/src/DependencyInjection/ZenstruckMessengerMonitorExtension.php index 21790e0..c0b966e 100644 --- a/src/DependencyInjection/ZenstruckMessengerMonitorExtension.php +++ b/src/DependencyInjection/ZenstruckMessengerMonitorExtension.php @@ -103,8 +103,8 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container $container->getDefinition('zenstruck_messenger_monitor.history.storage') ->setArgument(1, $entity) ; - $container->getDefinition('.zenstruck_messenger_monitor.history.listener') - ->setArgument(2, $mergedConfig['storage']['exclude']) + $container->getDefinition('.zenstruck_messenger_monitor.listener.receive_monitor_stamp') + ->setArgument(0, $mergedConfig['storage']['exclude']) ; if (!\class_exists(Schedule::class)) { diff --git a/src/EventListener/AddMonitorStampListener.php b/src/EventListener/AddMonitorStampListener.php new file mode 100644 index 0000000..fa0fd48 --- /dev/null +++ b/src/EventListener/AddMonitorStampListener.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Messenger\Monitor\EventListener; + +use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; +use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; + +/** + * @author Kevin Bond + * + * @internal + */ +final class AddMonitorStampListener +{ + public function __invoke(SendMessageToTransportsEvent $event): void + { + $event->setEnvelope($event->getEnvelope()->with(new MonitorStamp())); + } +} diff --git a/src/History/HistoryListener.php b/src/EventListener/HandleMonitorStampListener.php similarity index 54% rename from src/History/HistoryListener.php rename to src/EventListener/HandleMonitorStampListener.php index f06d985..04fb1f2 100644 --- a/src/History/HistoryListener.php +++ b/src/EventListener/HandleMonitorStampListener.php @@ -9,68 +9,31 @@ * file that was distributed with this source code. */ -namespace Zenstruck\Messenger\Monitor\History; +namespace Zenstruck\Messenger\Monitor\EventListener; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; -use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\Stamp\HandledStamp; -use Symfony\Component\Scheduler\Messenger\ScheduledStamp; -use Zenstruck\Messenger\Monitor\History\Model\Result; use Zenstruck\Messenger\Monitor\History\Model\Results; -use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp; +use Zenstruck\Messenger\Monitor\History\ResultNormalizer; +use Zenstruck\Messenger\Monitor\History\Storage; use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; -use Zenstruck\Messenger\Monitor\Stamp\TagStamp; /** * @author Kevin Bond * * @internal - * - * @phpstan-import-type Structure from Result */ -final class HistoryListener +final class HandleMonitorStampListener { - /** - * @param class-string[] $excludedClasses - */ public function __construct( private Storage $storage, private ResultNormalizer $normalizer, - private array $excludedClasses, ) { } - public function addMonitorStamp(SendMessageToTransportsEvent $event): void - { - $event->setEnvelope($event->getEnvelope()->with(new MonitorStamp())); - } - - public function receiveMessage(WorkerMessageReceivedEvent $event): void - { - $envelope = $event->getEnvelope(); - - if ($this->isMonitoringDisabled($envelope)) { - return; - } - - $stamp = $envelope->last(MonitorStamp::class); - - if (\class_exists(ScheduledStamp::class) && $scheduledStamp = $envelope->last(ScheduledStamp::class)) { - // scheduler transport doesn't trigger SendMessageToTransportsEvent - $stamp = new MonitorStamp($scheduledStamp->messageContext->triggeredAt); - - $event->addStamps(TagStamp::forSchedule($scheduledStamp)); - } - - if ($stamp instanceof MonitorStamp) { - $event->addStamps($stamp->markReceived($event->getReceiverName())); - } - } - public function handleSuccess(WorkerMessageHandledEvent $event): void { if (!$stamp = $event->getEnvelope()->last(MonitorStamp::class)) { @@ -107,29 +70,6 @@ public function handleFailure(WorkerMessageFailedEvent $event): void ); } - private function isMonitoringDisabled(Envelope $envelope): bool - { - $messageClass = $envelope->getMessage()::class; - - foreach ($this->excludedClasses as $excludedClass) { - if (\is_a($messageClass, $excludedClass, true)) { - return true; - } - } - - $stamp = $envelope->last(DisableMonitoringStamp::class) ?? DisableMonitoringStamp::getFor($messageClass); - - if (!$stamp) { - return false; - } - - if ($stamp->onlyWhenNoHandler && !$this->hasNoHandlers($envelope)) { - return false; - } - - return true; - } - private function createResults(Envelope $envelope, ?HandlerFailedException $exception = null): Results { $results = []; @@ -157,9 +97,4 @@ private function createResults(Envelope $envelope, ?HandlerFailedException $exce return new Results($results); } - - private function hasNoHandlers(Envelope $envelope): bool - { - return [] === $envelope->all(HandledStamp::class); - } } diff --git a/src/EventListener/ReceiveMonitorStampListener.php b/src/EventListener/ReceiveMonitorStampListener.php new file mode 100644 index 0000000..ad9e14c --- /dev/null +++ b/src/EventListener/ReceiveMonitorStampListener.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Messenger\Monitor\EventListener; + +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; +use Symfony\Component\Messenger\Stamp\HandledStamp; +use Symfony\Component\Scheduler\Messenger\ScheduledStamp; +use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp; +use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; +use Zenstruck\Messenger\Monitor\Stamp\TagStamp; + +/** + * @author Kevin Bond + * + * @internal + */ +final class ReceiveMonitorStampListener +{ + /** + * @param class-string[] $excludedClasses + */ + public function __construct(private array $excludedClasses) + { + } + + public function __invoke(WorkerMessageReceivedEvent $event): void + { + $envelope = $event->getEnvelope(); + + if ($this->isMonitoringDisabled($envelope)) { + return; + } + + $stamp = $envelope->last(MonitorStamp::class); + + if (\class_exists(ScheduledStamp::class) && $scheduledStamp = $envelope->last(ScheduledStamp::class)) { + // scheduler transport doesn't trigger SendMessageToTransportsEvent + $stamp = new MonitorStamp($scheduledStamp->messageContext->triggeredAt); + + $event->addStamps(TagStamp::forSchedule($scheduledStamp)); + } + + if ($stamp instanceof MonitorStamp) { + $event->addStamps($stamp->markReceived($event->getReceiverName())); + } + } + + private function isMonitoringDisabled(Envelope $envelope): bool + { + $messageClass = $envelope->getMessage()::class; + + foreach ($this->excludedClasses as $excludedClass) { + if (\is_a($messageClass, $excludedClass, true)) { + return true; + } + } + + $stamp = $envelope->last(DisableMonitoringStamp::class) ?? DisableMonitoringStamp::getFor($messageClass); + + if (!$stamp) { + return false; + } + + if ($stamp->onlyWhenNoHandler && !$this->hasNoHandlers($envelope)) { + return false; + } + + return true; + } + + private function hasNoHandlers(Envelope $envelope): bool + { + return [] === $envelope->all(HandledStamp::class); + } +} diff --git a/tests/Integration/DependencyInjection/ZenstruckMessengerMonitorExtensionTest.php b/tests/Integration/DependencyInjection/ZenstruckMessengerMonitorExtensionTest.php index 1be43f6..5745100 100644 --- a/tests/Integration/DependencyInjection/ZenstruckMessengerMonitorExtensionTest.php +++ b/tests/Integration/DependencyInjection/ZenstruckMessengerMonitorExtensionTest.php @@ -17,7 +17,9 @@ use PHPUnit\Framework\Constraint\LogicalNot; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Zenstruck\Messenger\Monitor\DependencyInjection\ZenstruckMessengerMonitorExtension; -use Zenstruck\Messenger\Monitor\History\HistoryListener; +use Zenstruck\Messenger\Monitor\EventListener\AddMonitorStampListener; +use Zenstruck\Messenger\Monitor\EventListener\HandleMonitorStampListener; +use Zenstruck\Messenger\Monitor\EventListener\ReceiveMonitorStampListener; use Zenstruck\Messenger\Monitor\History\Model\ProcessedMessage; use Zenstruck\Messenger\Monitor\History\Storage; use Zenstruck\Messenger\Monitor\History\Storage\ORMStorage; @@ -40,7 +42,9 @@ public function no_config(): void $this->assertContainerBuilderHasAlias(Transports::class, 'zenstruck_messenger_monitor.transports'); $this->assertContainerBuilderHasAlias(Workers::class, 'zenstruck_messenger_monitor.workers'); $this->assertThat($this->container, new LogicalNot(new ContainerBuilderHasAliasConstraint(Storage::class))); - $this->assertThat($this->container, new LogicalNot(new ContainerBuilderHasServiceDefinitionConstraint('.zenstruck_messenger_monitor.history.listener'))); + $this->assertThat($this->container, new LogicalNot(new ContainerBuilderHasServiceDefinitionConstraint('.zenstruck_messenger_monitor.listener.add_monitor_stamp'))); + $this->assertThat($this->container, new LogicalNot(new ContainerBuilderHasServiceDefinitionConstraint('.zenstruck_messenger_monitor.listener.receive_monitor_stamp'))); + $this->assertThat($this->container, new LogicalNot(new ContainerBuilderHasServiceDefinitionConstraint('.zenstruck_messenger_monitor.listener.handle_monitor_stamp'))); } /** @@ -55,8 +59,10 @@ public function orm_config(): void $this->assertContainerBuilderHasService('zenstruck_messenger_monitor.history.storage', ORMStorage::class); $this->assertContainerBuilderHasServiceDefinitionWithArgument('zenstruck_messenger_monitor.history.storage', 1, ProcessedMessageImpl::class); $this->assertContainerBuilderHasAlias(Storage::class, 'zenstruck_messenger_monitor.history.storage'); - $this->assertContainerBuilderHasService('.zenstruck_messenger_monitor.history.listener', HistoryListener::class); - $this->assertContainerBuilderHasServiceDefinitionWithArgument('.zenstruck_messenger_monitor.history.listener', 2, []); + $this->assertContainerBuilderHasService('.zenstruck_messenger_monitor.listener.add_monitor_stamp', AddMonitorStampListener::class); + $this->assertContainerBuilderHasService('.zenstruck_messenger_monitor.listener.receive_monitor_stamp', ReceiveMonitorStampListener::class); + $this->assertContainerBuilderHasServiceDefinitionWithArgument('.zenstruck_messenger_monitor.listener.receive_monitor_stamp', 0, []); + $this->assertContainerBuilderHasService('.zenstruck_messenger_monitor.listener.handle_monitor_stamp', HandleMonitorStampListener::class); } /** @@ -69,7 +75,7 @@ public function storage_with_excluded_classes(): void 'exclude' => ['stdClass'], ]]); - $this->assertContainerBuilderHasServiceDefinitionWithArgument('.zenstruck_messenger_monitor.history.listener', 2, [\stdClass::class]); + $this->assertContainerBuilderHasServiceDefinitionWithArgument('.zenstruck_messenger_monitor.listener.receive_monitor_stamp', 0, [\stdClass::class]); } /** diff --git a/tests/Unit/EventListener/AddMonitorStampListenerTest.php b/tests/Unit/EventListener/AddMonitorStampListenerTest.php new file mode 100644 index 0000000..b256bf0 --- /dev/null +++ b/tests/Unit/EventListener/AddMonitorStampListenerTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Messenger\Monitor\Tests\Unit\EventListener; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; +use Zenstruck\Messenger\Monitor\EventListener\AddMonitorStampListener; +use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; + +/** + * @author Kevin Bond + */ +final class AddMonitorStampListenerTest extends TestCase +{ + /** + * @test + */ + public function adds_monitor_stamp(): void + { + $listener = new AddMonitorStampListener(); + $envelope = new Envelope(new \stdClass()); + $event = new SendMessageToTransportsEvent($envelope, []); + + $this->assertNull($event->getEnvelope()->last(MonitorStamp::class)); + + $listener->__invoke($event); + + $this->assertInstanceOf(MonitorStamp::class, $event->getEnvelope()->last(MonitorStamp::class)); + } +} diff --git a/tests/Unit/EventListener/HandleMonitorStampListenerTest.php b/tests/Unit/EventListener/HandleMonitorStampListenerTest.php new file mode 100644 index 0000000..8ce932e --- /dev/null +++ b/tests/Unit/EventListener/HandleMonitorStampListenerTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Messenger\Monitor\Tests\Unit\EventListener; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; +use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; +use Symfony\Component\Messenger\Stamp\HandledStamp; +use Zenstruck\Messenger\Monitor\EventListener\HandleMonitorStampListener; +use Zenstruck\Messenger\Monitor\History\Model\Results; +use Zenstruck\Messenger\Monitor\History\ResultNormalizer; +use Zenstruck\Messenger\Monitor\History\Storage; +use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; + +/** + * @author Kevin Bond + */ +final class HandleMonitorStampListenerTest extends TestCase +{ + /** + * @test + */ + public function handles_success(): void + { + $envelope = new Envelope(new \stdClass(), [ + (new MonitorStamp())->markReceived('foo'), + new HandledStamp('handler', 'return'), + ]); + $storage = $this->createMock(Storage::class); + $storage->expects($this->once())->method('save')->with( + $this->isInstanceOf(Envelope::class), + $this->isInstanceOf(Results::class), + ); + + $listener = new HandleMonitorStampListener($storage, new ResultNormalizer(__DIR__)); + + $listener->handleSuccess($event = new WorkerMessageHandledEvent($envelope, 'foo')); + + $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isFinished()); + } + + /** + * @test + */ + public function handles_success_invalid(): void + { + $storage = $this->createMock(Storage::class); + $storage->expects($this->never())->method('save'); + + $listener = new HandleMonitorStampListener($storage, new ResultNormalizer(__DIR__)); + + $listener->handleSuccess(new WorkerMessageHandledEvent(new Envelope(new \stdClass()), 'foo')); + $listener->handleSuccess(new WorkerMessageHandledEvent(new Envelope(new \stdClass(), [new MonitorStamp()]), 'foo')); + } + + /** + * @test + */ + public function handles_failure(): void + { + $envelope = new Envelope(new \stdClass(), [(new MonitorStamp())->markReceived('foo')]); + $exception = new \RuntimeException(); + $storage = $this->createMock(Storage::class); + $storage->expects($this->once())->method('save')->with( + $this->isInstanceOf(Envelope::class), + $this->isInstanceOf(Results::class), + $exception, + ); + + $listener = new HandleMonitorStampListener($storage, new ResultNormalizer(__DIR__)); + + $listener->handleFailure($event = new WorkerMessageFailedEvent($envelope, 'foo', $exception)); + + $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isFinished()); + } + + /** + * @test + */ + public function handles_failure_invalid(): void + { + $storage = $this->createMock(Storage::class); + $storage->expects($this->never())->method('save'); + + $listener = new HandleMonitorStampListener($storage, new ResultNormalizer(__DIR__)); + + $listener->handleFailure(new WorkerMessageFailedEvent(new Envelope(new \stdClass()), 'foo', new \RuntimeException())); + $listener->handleFailure(new WorkerMessageFailedEvent(new Envelope(new \stdClass(), [new MonitorStamp()]), 'foo', new \RuntimeException())); + } +} diff --git a/tests/Unit/History/HistoryListenerTest.php b/tests/Unit/EventListener/ReceiveMonitorStampListenerTest.php similarity index 56% rename from tests/Unit/History/HistoryListenerTest.php rename to tests/Unit/EventListener/ReceiveMonitorStampListenerTest.php index c3af2e3..94ce86b 100644 --- a/tests/Unit/History/HistoryListenerTest.php +++ b/tests/Unit/EventListener/ReceiveMonitorStampListenerTest.php @@ -9,22 +9,16 @@ * file that was distributed with this source code. */ -namespace Zenstruck\Messenger\Monitor\Tests\Unit\History; +namespace Zenstruck\Messenger\Monitor\Tests\Unit\EventListener; use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; -use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; -use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; use Symfony\Component\Messenger\Stamp\HandledStamp; use Symfony\Component\Scheduler\Generator\MessageContext; use Symfony\Component\Scheduler\Messenger\ScheduledStamp; use Symfony\Component\Scheduler\Trigger\TriggerInterface; -use Zenstruck\Messenger\Monitor\History\HistoryListener; -use Zenstruck\Messenger\Monitor\History\Model\Results; -use Zenstruck\Messenger\Monitor\History\ResultNormalizer; -use Zenstruck\Messenger\Monitor\History\Storage; +use Zenstruck\Messenger\Monitor\EventListener\ReceiveMonitorStampListener; use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp; use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; use Zenstruck\Messenger\Monitor\Stamp\TagStamp; @@ -32,36 +26,20 @@ /** * @author Kevin Bond */ -final class HistoryListenerTest extends TestCase +final class ReceiveMonitorStampListenerTest extends TestCase { - /** - * @test - */ - public function adds_monitor_stamp(): void - { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); - $envelope = new Envelope(new \stdClass()); - $event = new SendMessageToTransportsEvent($envelope, []); - - $this->assertNull($event->getEnvelope()->last(MonitorStamp::class)); - - $listener->addMonitorStamp($event); - - $this->assertInstanceOf(MonitorStamp::class, $event->getEnvelope()->last(MonitorStamp::class)); - } - /** * @test */ public function skips_standard_messages_without_monitor_stamp(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new \stdClass()); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); $this->assertEmpty($event->getEnvelope()->all(MonitorStamp::class)); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertEmpty($event->getEnvelope()->all(MonitorStamp::class)); } @@ -71,13 +49,13 @@ public function skips_standard_messages_without_monitor_stamp(): void */ public function marks_standard_message_as_received(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new \stdClass(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); $this->assertSame('foo', $event->getEnvelope()->last(MonitorStamp::class)->transport()); @@ -89,7 +67,7 @@ public function marks_standard_message_as_received(): void */ public function marks_scheduled_message_as_received(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new \stdClass(), [new ScheduledStamp(new MessageContext( 'default', 'id', @@ -100,7 +78,7 @@ public function marks_scheduled_message_as_received(): void $this->assertNull($event->getEnvelope()->last(MonitorStamp::class)); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); $this->assertSame('foo', $event->getEnvelope()->last(MonitorStamp::class)->transport()); @@ -108,87 +86,16 @@ public function marks_scheduled_message_as_received(): void $this->assertSame('schedule:default:id', $event->getEnvelope()->last(TagStamp::class)->value); } - /** - * @test - */ - public function handles_success(): void - { - $envelope = new Envelope(new \stdClass(), [ - (new MonitorStamp())->markReceived('foo'), - new HandledStamp('handler', 'return'), - ]); - $storage = $this->createMock(Storage::class); - $storage->expects($this->once())->method('save')->with( - $this->isInstanceOf(Envelope::class), - $this->isInstanceOf(Results::class), - ); - - $listener = new HistoryListener($storage, new ResultNormalizer(__DIR__), []); - - $listener->handleSuccess($event = new WorkerMessageHandledEvent($envelope, 'foo')); - - $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isFinished()); - } - - /** - * @test - */ - public function handles_success_invalid(): void - { - $storage = $this->createMock(Storage::class); - $storage->expects($this->never())->method('save'); - - $listener = new HistoryListener($storage, new ResultNormalizer(__DIR__), []); - - $listener->handleSuccess(new WorkerMessageHandledEvent(new Envelope(new \stdClass()), 'foo')); - $listener->handleSuccess(new WorkerMessageHandledEvent(new Envelope(new \stdClass(), [new MonitorStamp()]), 'foo')); - } - - /** - * @test - */ - public function handles_failure(): void - { - $envelope = new Envelope(new \stdClass(), [(new MonitorStamp())->markReceived('foo')]); - $exception = new \RuntimeException(); - $storage = $this->createMock(Storage::class); - $storage->expects($this->once())->method('save')->with( - $this->isInstanceOf(Envelope::class), - $this->isInstanceOf(Results::class), - $exception, - ); - - $listener = new HistoryListener($storage, new ResultNormalizer(__DIR__), []); - - $listener->handleFailure($event = new WorkerMessageFailedEvent($envelope, 'foo', $exception)); - - $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isFinished()); - } - - /** - * @test - */ - public function handles_failure_invalid(): void - { - $storage = $this->createMock(Storage::class); - $storage->expects($this->never())->method('save'); - - $listener = new HistoryListener($storage, new ResultNormalizer(__DIR__), []); - - $listener->handleFailure(new WorkerMessageFailedEvent(new Envelope(new \stdClass()), 'foo', new \RuntimeException())); - $listener->handleFailure(new WorkerMessageFailedEvent(new Envelope(new \stdClass(), [new MonitorStamp()]), 'foo', new \RuntimeException())); - } - /** * @test */ public function can_disable_monitoring_with_envelope_stamp(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new \stdClass(), [new MonitorStamp(), new DisableMonitoringStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -198,11 +105,11 @@ public function can_disable_monitoring_with_envelope_stamp(): void */ public function can_disable_monitoring_message_attribute(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new DisabledMonitoringMessage(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -212,11 +119,11 @@ public function can_disable_monitoring_message_attribute(): void */ public function can_disable_monitoring_message_interface_attribute(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new DisableMonitoringViaInterface(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -226,11 +133,11 @@ public function can_disable_monitoring_message_interface_attribute(): void */ public function can_disable_monitoring_message_attribute_without_handler(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new DisabledMonitoringWithoutHandlerMessage(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -240,14 +147,14 @@ public function can_disable_monitoring_message_attribute_without_handler(): void */ public function can_disable_monitoring_message_without_handler(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope(new \stdClass(), [ new MonitorStamp(), new DisableMonitoringStamp(true), ]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -257,7 +164,7 @@ public function can_disable_monitoring_message_without_handler(): void */ public function handle_disable_monitoring_message_attribute_with_handler(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope( new EnabledMonitoringWithHandlerMessage(), [ @@ -267,7 +174,7 @@ public function handle_disable_monitoring_message_attribute_with_handler(): void ); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -277,7 +184,7 @@ public function handle_disable_monitoring_message_attribute_with_handler(): void */ public function handle_disable_monitoring_message_with_handler(): void { - $listener = new HistoryListener($this->createMock(Storage::class), new ResultNormalizer(__DIR__), []); + $listener = new ReceiveMonitorStampListener([]); $envelope = new Envelope( new \stdClass(), [ @@ -288,7 +195,7 @@ public function handle_disable_monitoring_message_with_handler(): void ); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertTrue($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -298,9 +205,7 @@ public function handle_disable_monitoring_message_with_handler(): void */ public function can_disable_monitoring_message_via_config(): void { - $listener = new HistoryListener( - $this->createMock(Storage::class), - new ResultNormalizer(__DIR__), + $listener = new ReceiveMonitorStampListener( [ MessageToDisableViaConfig::class, ] @@ -308,7 +213,7 @@ public function can_disable_monitoring_message_via_config(): void $envelope = new Envelope(new MessageToDisableViaConfig(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } @@ -318,9 +223,7 @@ public function can_disable_monitoring_message_via_config(): void */ public function can_disable_extended_monitoring_message_via_config(): void { - $listener = new HistoryListener( - $this->createMock(Storage::class), - new ResultNormalizer(__DIR__), + $listener = new ReceiveMonitorStampListener( [ MessageToDisableViaConfig::class, ] @@ -328,7 +231,7 @@ public function can_disable_extended_monitoring_message_via_config(): void $envelope = new Envelope(new ExtendedMessageToDisableViaConfig(), [new MonitorStamp()]); $event = new WorkerMessageReceivedEvent($envelope, 'foo'); - $listener->receiveMessage($event); + $listener->__invoke($event); $this->assertFalse($event->getEnvelope()->last(MonitorStamp::class)->isReceived()); } diff --git a/tests/Unit/History/Model/ResultsTest.php b/tests/Unit/History/Model/ResultsTest.php index a8c1a67..638f86b 100644 --- a/tests/Unit/History/Model/ResultsTest.php +++ b/tests/Unit/History/Model/ResultsTest.php @@ -30,6 +30,6 @@ public function can_create_with_null(): void $this->assertSame([], $results->all()); $this->assertSame([], $results->successes()); $this->assertSame([], $results->failures()); - $this->assertSame(null, $results->jsonSerialize()); + $this->assertNull($results->jsonSerialize()); } }