Ricardo's avatar
Level 48

Test with event works on Laravel 7 but not in Laravel 8

Hi there,

In the functional test that fires an event when a user is updated, the listeners are fired when using Laravel 7, in Laravel 8 the listeners are not triggered.

Did someone has any clue what I'm missing?

0 likes
16 replies
Talinon's avatar

@Ricardo There is nothing obvious between the version that would cause this. I upgraded from v7 without any issues. Can you show your test and relevant code?

Ricardo's avatar
Level 48

@talinon thank you.

Nothing fancy

use Noah\Events\UserWasUpdated;

class User extends Authenticatable

    public static function boot()
    {
        parent::boot();

        static::saved(function ($user) {

            UserWasUpdated::dispatch($user);

        });
    }
}

In EventServiceProdiver:

    protected $listen = [
        'Noah\Events\UserWasUpdated' => [
            'Noah\Listeners\UpdateUserInVictoria',
            'Noah\Listeners\UpdateUserInAbako',
        ],
   ];

The listener:

namespace Noah\Listeners;

use Noah\Events\UserWasUpdated;

class UpdateUserInVictoria
{
    public function handle(UserWasUpdated $event)
    {
        dump('I was called');
    }
}
Talinon's avatar

@ricardo Hmm..

Have you included the Dispatchable trait within the event class? although excluding that should have caused an exception to be thrown..

class UserWasUpdated {

use Illuminate\Foundation\Events\Dispatchable;

...
}

Have you tried placing dd() within the model's boot method to make sure it's getting at least that far?

 static::saved(function ($user) {

 	    dd('called');
            UserWasUpdated::dispatch($user);

        });

Talinon's avatar

@ricardo Perhaps its a namespacing issue.. prefix your listeners with a backslash:

  protected $listen = [
        '\Noah\Events\UserWasUpdated' => [
            '\Noah\Listeners\UpdateUserInVictoria',
            '\Noah\Listeners\UpdateUserInAbako',
        ],
   ];
Ricardo's avatar
Level 48

@talinon it seems that there is a issue with the listeners.

php artisan event:list

+-------------------------------+----------------------------------------------+
| Event                         | Listeners                                    |
+-------------------------------+----------------------------------------------+
| Noah\Events\UserWasUpdated    | Noah\Listeners\UpdateUserInVictoria@handle   |
|                               | Noah\Listeners\UpdateUserInAbako@handle      |
|                               | Noah\Listeners\UpdateUserInVictoria          |
|                               | Noah\Listeners\UpdateUserInAbako             |
+-------------------------------+----------------------------------------------+

But in Telescope/Events:

+----------------------------+-----------+----------+
| Name	                     | Listeners | Happened |
| Noah\Events\UserWasUpdated |	0        | 1m ago   |	
+----------------------------+-----------+----------+

There are no listeners!

Talinon's avatar

@ricardo Try clearing your event cache: php artisan event:clear

and did you prefix the classes with a slash in the EventServiceProvider? Otherwise it's likely going to register them as \Noah\Providers\Noah\Events\UserWasUpdated, etc

Ricardo's avatar
Level 48

@talinon I already did the clear.

I did:

        \Noah\Events\UserWasUpdated::class => [
            \Noah\Listeners\UpdateUserInVictoria::class,
            \Noah\Listeners\UpdateUserInAbako::class,
        ],
Talinon's avatar

@ricardo So, are you using Event::fake()? If so, that explains why your listeners are not being invoked.

Ricardo's avatar
Level 48

@talinon 'cause you mentioned "browser" I tested in the browser, same problem

Talinon's avatar

@ricardo This is likely my last shot at this...

It looks like you've changed your base naemspace from App to Noah, so make sure that is relfected in your config/app.php file:

Noah\Providers\EventServiceProvider::class,

Not:

App\Providers\EventServiceProvider::class,

And make sure you have Noah set for PSR-4 within your composer.json:

and did you run php artisan name:Noah?

and run composer dump-autoload

1 like
Ricardo's avatar
Level 48

@talinon you won't be aware how helpful was your help, the problem goes down to:

@@ -60,12 +58,9 @@ class EventServiceProvider extends ServiceProvider
 
     public function register()
     {
+        parent::register();
+
         ...
     }

Big thank you!

Please or to participate in this conversation.