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

[9.x] Avoid mutating the $expectedLitener between loops on Event::assertListening #46095

Merged

Conversation

phcostabh
Copy link
Contributor

After upgrading from Laravel 8.x to 9.x, some of the assertions for the assertListening failed randomly.

I observed that when iteration over the listeners for a given event, if a Class[@]method callback (PostAutoEventSubscriber::class . '@handle') string is evaluated before any class-based listener (PostAutoEventSubscriber::class), it causes the assertion to fail wrongfully.

This happens because after the callback string is evaluated, the $expectedListener is normalized for the comparison. It causes the subsequent iteration to compare the $actualListener with the changed $expectedListener from the previous one.

You can attest that by changing the order in which the listeners are bound in the tests/Integration/Events/EventFakeTest.php from:

Event::listen('event', PostEventSubscriber::class);
Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');

To:

Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');
Event::listen('event', PostEventSubscriber::class);

To solve the issue, we're just "caching" the normalized form of the expected listeners to avoid mutability between the iteration loops.

@taylorotwell taylorotwell merged commit c72459a into laravel:9.x Feb 13, 2023
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