From c6d87b8d26f817f9c8694384d9b3db877cf19b75 Mon Sep 17 00:00:00 2001 From: dmason30 Date: Sun, 23 Jul 2023 21:00:11 +0100 Subject: [PATCH] Add support for listener shouldQueue method --- src/Decorators/ListenerDecorator.php | 9 +++++++++ tests/AsListenerWithShouldQueueTest.php | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Decorators/ListenerDecorator.php b/src/Decorators/ListenerDecorator.php index 7a39ee1..09a57c8 100644 --- a/src/Decorators/ListenerDecorator.php +++ b/src/Decorators/ListenerDecorator.php @@ -26,6 +26,15 @@ public function handle(...$arguments) } } + public function shouldQueue(...$arguments) + { + if ($this->hasMethod('shouldQueue')) { + return $this->resolveFromArgumentsAndCall('shouldQueue', $arguments); + } + + return true; + } + protected function resolveFromArgumentsAndCall($method, $arguments) { $arguments = $this->resolveClassMethodDependencies( diff --git a/tests/AsListenerWithShouldQueueTest.php b/tests/AsListenerWithShouldQueueTest.php index c400778..bd6b390 100644 --- a/tests/AsListenerWithShouldQueueTest.php +++ b/tests/AsListenerWithShouldQueueTest.php @@ -15,6 +15,7 @@ class AsListenerWithShouldQueueTest implements ShouldQueue public static int $constructed = 0; public static int $handled = 0; + public static int $shouldQueue = 0; public static ?int $latestResult; public function __construct() @@ -35,6 +36,13 @@ public function asListener(OperationRequestedEvent $event): void { $this->handle($event->operation, $event->left, $event->right); } + + public function shouldQueue(OperationRequestedEvent $event): bool + { + static::$shouldQueue++; + + return true; + } } beforeEach(function () { @@ -44,6 +52,7 @@ public function asListener(OperationRequestedEvent $event): void // And reset the static properties between each test. AsListenerWithShouldQueueTest::$constructed = 0; AsListenerWithShouldQueueTest::$handled = 0; + AsListenerWithShouldQueueTest::$shouldQueue = 0; AsListenerWithShouldQueueTest::$latestResult = null; }); @@ -69,6 +78,7 @@ public function asListener(OperationRequestedEvent $event): void // Then the action was triggered as a queued listener. expect(AsListenerWithShouldQueueTest::$latestResult)->toBe(3); expect(AsListenerWithShouldQueueTest::$handled)->toBe(1); + expect(AsListenerWithShouldQueueTest::$shouldQueue)->toBe(1); // And was constructed twice. Once before and once during the queued job. expect(AsListenerWithShouldQueueTest::$constructed)->toBe(2);