laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.47k stars 11.01k forks source link

Registered event being dispatched twice since v11.9.0 #51727

Closed ibrunotome closed 5 months ago

ibrunotome commented 5 months ago

Laravel Version

11.10.0

PHP Version

8.3.4

Database Driver & Version

sqlite

Description

Since 11.9.0, the event Registered dispatched on RegisteredUserController is being listened twice, so this test:

<?php

use App\Notifications\WelcomeNotification;

test('new users can register', function () {
    Notification::fake();

    $response = $this->post('/register', [
        'name' => 'Test User',
        'email' => 'test@example.com',
        'password' => 'password',
        'password_confirmation' => 'password',
    ])->dump()->assertNoContent();

    $this->assertAuthenticated();
    $response->assertNoContent();

    Notification::assertSentTimes(WelcomeNotification::class, 1);
});

started to fail: Tests\Feature\Auth\RegistrationTest > new users can register Expected [App\Notifications\WelcomeNotification] to be sent 1 times, but was sent 2 times. Failed asserting that 2 is identical to 1.

While writing this issue I noted that this only occurs if the argument $event in the listener has the explicit type, like handle(Registered $event)

Steps To Reproduce

<?php

namespace App\Listeners;

use App\Notifications\WelcomeNotification;
use Illuminate\Auth\Events\Registered;

class SendWelcomeNotification
{
    public function handle(Registered $event): void
    {
        $event->user->notify(new WelcomeNotification());
    }
}

So it's like the type before $event is dispatching the event again? I mean, this is not the behavior prior v11.9.0, the test pass in v11.8.0 and below.

crynobone commented 5 months ago

Seems like the same as https://github.com/laravel/framework/issues/51610. Are you using the EventServiceProvider directly in v11? See https://laravel.com/docs/11.x/releases#service-providers

ibrunotome commented 5 months ago

No, my bootstrap/app.php has only AppServiceProvider, but I was registering the event/listener in AppServiceProvider with:

Event::listen(Registered::class, SendEmailVerificationNotification::class);
Event::listen(Registered::class, SendWelcomeNotification::class);
Event::listen(WebhookReceived::class, StripeEventListener::class);

removing it from AppServiceProvider and running the test agains, It passes like it was in v11.8 and previous versions.

crynobone commented 5 months ago

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

crynobone commented 5 months ago

removing it from AppServiceProvider and running the test agains, It passes like it was in v11.8 and previous versions.

Yes that is the expected behaviour now.