Skip to content

Commit

Permalink
Merge pull request #3 from langleyfoxall/feature/writing-tests
Browse files Browse the repository at this point in the history
Added tests correctly
  • Loading branch information
warren-lf authored May 20, 2022
2 parents 204d117 + a6241fe commit 88f64b5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Can select events to ignore in the Config File
- Can select credentials to be omitted from the database in the Config File, such as passwords
- Developers can now add authentication events easily through the `AuthenticationLogSubscriber`
- Tests have been implemented to ensure the package works correctly throughout development

## v0.1

Expand Down
76 changes: 76 additions & 0 deletions tests/Feature/AuthenticationLogSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace LangleyFoxall\LaravelAuthenticationLog\Tests\Feature;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Authenticatable as AuthAuthenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use LangleyFoxall\LaravelAuthenticationLog\Tests\TestCase;
use LangleyFoxall\LaravelAuthenticationLog\Models\AuthenticationLogRecord;
use LangleyFoxall\LaravelAuthenticationLog\Subscribers\AuthenticationLogSubscriber;
use LangleyFoxall\LaravelAuthenticationLog\Tests\Models\User;
use Orchestra\Testbench\Factories\UserFactory;

class AuthenticationLogSubscriberTest extends TestCase
{
use RefreshDatabase;

public function setUp() : void
{
parent::setUp();
}

public function test_a_log_record_is_created_when_a_login_event_is_fired()
{
$user = new User;
$user->id = 1;

$event = new Login('web', $user, false);

Event::dispatch($event);

$this->assertDatabaseHas('authentication_log_records', [
'authenticatable_id' => $user->id,
'authenticatable_type' => get_class($user),
'eventType' => get_class($event)
]);
}

public function test_a_log_record_is_created_when_a_logout_event_is_fired()
{
$user = new User;
$user->id = 1;

$event = new Logout('web', $user);

Event::dispatch($event);

$this->assertDatabaseHas('authentication_log_records', [
'authenticatable_id' => $user->id,
'authenticatable_type' => get_class($user),
'eventType' => get_class($event)
]);
}

public function test_a_log_record_is_created_when_a_failed_event_is_fired()
{
$credentials = [
'name' => 'notJohn',
];

$event = new Failed('web', null, $credentials);

Event::dispatch($event);

$this->assertDatabaseHas('authentication_log_records', [
'credentials' => json_encode($credentials),
'eventType' => get_class($event)
]);
}
}
22 changes: 22 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LangleyFoxall\LaravelAuthenticationLog\Tests\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User implements Authenticatable
{
public $id;

// no-op -- method definitions given to comply with Authenticatable Interface,
// purely for testing purposes only
public function getAuthIdentifierName(){}
public function getAuthIdentifier(){}
public function getAuthPassword(){}
public function getRememberToken(){}
public function setRememberToken($value){}
public function getRememberTokenName(){}


}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ protected function getPackageProviders($app)

protected function getEnvironmentSetUp($app)
{

}
}
1 change: 1 addition & 0 deletions tests/Unit/AuthenticationLogRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function test_creating_an_authenticationLogRecord_stores_it_in_a_database
$attributes = [
'authenticatable_id' => 1,
'authenticatable_type' => "Test Type",
'eventType' => 'testEvent',
'recorded_at' => $timeLog
];

Expand Down

0 comments on commit 88f64b5

Please sign in to comment.