Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for listener shouldQueue method #247

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading