I have a working code where when a Customer record is created, an event will dispatch and a listener will then send a notification to the Customer's agent.
EventServiceProvider.php
protected $listen = [
'App\Events\CustomerCreated' => [
'App\Listeners\TriggerExternalCustomerCreation',
'App\Listeners\SendNotificationCustomerCreated',
],
]
SendNotificationCustomerCreated.php
public function handle(CustomerCreated $event)
{
$when = now()->addMinutes(0);
$event->customer->manager->notify((new CustomerCreatedNotification($event->customer))->delay($when));
}
Here's my test case:-
public function customer_created_event_dispatch()
{
// $this->markTestIncomplete('This test has not been implemented yet.');
$this->withoutExceptionHandling();
Event::fake();
Notification::fake();
$user = factory(User::class)->states('order management')->create();
$data = $this->data(['address' => true, 'contact' => true]);
$response = $this->actingAs($user)->post(route('customers.store'), $data);
$response->assertSessionHasNoErrors();
$customers = Customer::all();
$customer = $customers->first();
$manager = $customer->manager;
$this->assertCount(1, $customers);
Event::assertDispatched(CustomerCreated::class, function ($event) use ($customers) {
return $event->customer->id === $customers->first()->id;
});
Notification::assertSentTo(
$manager,
CustomerCreatedNotification::class,
function ($notification, $channels) use ($customer) {
return $notification->customer->id === $customer->id;
}
);
}
Initially the listener is queued, but i try removed from queue but error remains.
I can confirmed that the event did dispatch since it passed the Event::assertDispatched. But failed at the last assertion with below errors:-
The expected [App\Notifications\CustomerCreatedNotification] notification was not sent