Skip to content

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

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
phcostabh:fix/event-fake-expected-listener-mutability
Feb 13, 2023
Merged

[9.x] Avoid mutating the $expectedLitener between loops on Event::assertListening#46095
taylorotwell merged 2 commits into
laravel:9.xfrom
phcostabh:fix/event-fake-expected-listener-mutability

Conversation

@phcostabh

Copy link
Copy Markdown

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