Skip to content

Commit

Permalink
Add support for listener shouldQueue method (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 authored Jul 25, 2023
1 parent 23439c9 commit 02b7731
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Decorators/ListenerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions tests/AsListenerWithShouldQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 () {
Expand All @@ -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;
});

Expand All @@ -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);
Expand Down

0 comments on commit 02b7731

Please sign in to comment.