@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?
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?
@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');
}
}
@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, yes I did a dump after the event dispatch
@ricardo Perhaps its a namespacing issue.. prefix your listeners with a backslash:
protected $listen = [
'\Noah\Events\UserWasUpdated' => [
'\Noah\Listeners\UpdateUserInVictoria',
'\Noah\Listeners\UpdateUserInAbako',
],
];
@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!
@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
@talinon I already did the clear.
I did:
\Noah\Events\UserWasUpdated::class => [
\Noah\Listeners\UpdateUserInVictoria::class,
\Noah\Listeners\UpdateUserInAbako::class,
],
@ricardo How are you testing this exactly? In the browser? phpunit test?
@talinon phpunit
@ricardo So, are you using Event::fake()? If so, that explains why your listeners are not being invoked.
@talinon no sir. I create a user, then assert that databasehas.
@talinon 'cause you mentioned "browser" I tested in the browser, same problem
@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
@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!
@ricardo Glad you got it worked out!
Please or to participate in this conversation.