Home > Blockchain >  How to test laravel login event
How to test laravel login event

Time:11-25

Trying to write a test for my LoginListener. As the name states, it listens for the login event and then logs a record of this in the activity_log table.

When I try run the test it throws the below error:

LoginListener::handle(): Argument #1 ($event) must be of type Illuminate\Auth\Events\Login, string given

What I need to know is how to correctly call the login event?

public function testSuccessfulLoginStoresActivity()
{
    $event = \Illuminate\Auth\Events\Login::class;

    $listener = new LoginListener();

    Auth::login($this->user);

    $listener->handle($event);

    $this->assertDatabaseHas('activity_log', [
        'event' => ActivityLogEventType::USER_LOGIN(),
        'description' => 'User Login',
        'account_id' => $this->practice->account->id,
    ]);
}

CodePudding user response:

The following sets the namespace with the class name included. Does not create an event object, as the error states that needs to be passed.

 $event = \Illuminate\Auth\Events\Login::class;

You need to instantiate an Login event, the first parameter is the guard, use the one you have in your config, second parameter is user and third is if it should be remembered as a bool.

use Illuminate\Auth\Events\Login;

$event = new Login('web', $this->user, true);

Which will fix your current error and most likely as i see it fix the test.

I would thou believe you do not need to login your user for this test to work. So remove the login code

Auth::login($this->user);
  • Related