Skip to content

Commit

Permalink
Test listening to builtin events
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 16, 2023
1 parent f25df87 commit 94fac2a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/Listeners/LoginListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Listeners;

use App\Events\Login;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LoginListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param \App\Events\Login $event
* @return void
*/
public function handle(Login $event)
{
//
}
}
31 changes: 31 additions & 0 deletions app/Listeners/LogoutListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Listeners;

use App\Events\Logout;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LogoutListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param \App\Events\Logout $event
* @return void
*/
public function handle(Logout $event)
{
//
}
}
10 changes: 10 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
use App\Events\OrderCreatedEvent;
use App\Events\OrderDeletedEvent;
use App\Events\OrderUpdatedEvent;
use App\Listeners\LoginListener;
use App\Listeners\LogoutListener;
use App\Listeners\OrderCreationListener;
use App\Listeners\OrderDeletionListener;
use App\Listeners\OrderUpdateListener;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -21,6 +25,12 @@ class EventServiceProvider extends ServiceProvider
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Login::class => [
LoginListener::class,
],
Logout::class => [
LogoutListener::class,
],
Registered::class => [
SendEmailVerificationNotification::class,
],
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use App\Events\OrderCreatedEvent;
use App\Events\OrderDeletedEvent;
use App\Events\OrderUpdatedEvent;
use App\Listeners\LoginListener;
use App\Listeners\LogoutListener;
use App\Listeners\OrderCreationListener;
use App\Listeners\OrderDeletionListener;
use App\Listeners\OrderUpdateListener;
use App\Models\Order;
use App\Models\User;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Event;
Expand Down Expand Up @@ -88,4 +92,14 @@ public function triggering_an_event_after_deleting_orders()

Event::assertDispatched(OrderDeletedEvent::class);
}

/** @test */
public function listening_to_builtin_events()
{
Event::fake();

Event::assertListening(Login::class, LoginListener::class);

Event::assertListening(Logout::class, LogoutListener::class);
}
}

0 comments on commit 94fac2a

Please sign in to comment.