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

Registered event being dispatched twice since v11.9.0 #51727

Closed
ibrunotome opened this issue Jun 6, 2024 · 4 comments
Closed

Registered event being dispatched twice since v11.9.0 #51727

ibrunotome opened this issue Jun 6, 2024 · 4 comments

Comments

@ibrunotome
Copy link
Contributor

ibrunotome commented Jun 6, 2024

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

  • composer create-project laravel/laravel:^11.9 example-app
  • composer require laravel/breeze
  • php artisan breeze:install
  • choose api, pest
  • modify Tests\Feature\Auth\RegistrationTest to be like the provided code in the description step
  • php artisan make:notification WelcomeNotification
  • php artisan make:listener SendWelcomeNotification
<?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());
    }
}
  • note that the event argument has the explicit 'Registered' type, if I remove it, the test passes again

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
Copy link
Member

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

@ibrunotome
Copy link
Contributor Author

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

This comment was marked as outdated.

@crynobone
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants