Skip to content

[12.x] QueueFake::listenersPushed() #55063

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

Merged
merged 3 commits into from
Mar 19, 2025

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Mar 18, 2025

It's a bit unwieldy to test queued listeners. I imagine most people went through the process I did today.

  1. Try to write a test to check if a listener was queued by checking if the listener was pushed to the queue
  2. Test fails... debug code to make sure it's being called
  3. dd(Queue::pushedJobs())
  4. ooooh, it's inside of a CallQueuedListener class.
  5. Write another test that fails
  6. Inspect the CallQueuedListener class and see that the event is under the data property, and that property is an array

The hope here is that this makes it a little bit easier for the next person.

Queue::fake();
event(new SomeEvent(value: 'look, a value!'));

$this->assertCount(
    1,
    Queue::listenersPushed(
        SomeEventListener::class,
        fn (SomeEvent $event) => $event->value === 'look, a value!'
    )
);

Note that this is slightly different than the *pushed methods in that the first parameter in the callback is the event. Those this is a deviation, I think this is probably what most people want to perform assertions against.

Other possibilities

Should this be queuedEvents() and have it return a Collection of arrays [$event, $handler]?

@taylorotwell taylorotwell merged commit 6d6e4e1 into laravel:12.x Mar 19, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants