Skip to content

Commit

Permalink
Merge pull request #247 from MGatner/test-trait
Browse files Browse the repository at this point in the history
Add test trait
  • Loading branch information
lonnieezell authored Jul 7, 2020
2 parents 086418f + cc809e5 commit 86a0256
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Test/AuthTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace Myth\Auth\Test;

use Config\Services;

/**
* Trait AuthTestTrait
*
* Provides additional utilities for authentication and authorization
* during testing.
*/
trait AuthTestTrait
{
/**
* Resets the Authentication and Authorization services.
* Particularly helpful between feature tests.
*/
protected function resetAuthServices()
{
Services::injectMock('authentication', Services::authentication('local', null, null, false));
Services::injectMock('authorization', Services::authorization(null, null, null, false));
$_SESSION = [];
}
}
37 changes: 37 additions & 0 deletions tests/unit/AuthTestTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Myth\Auth\Test\Fakers\PermissionFaker;
use Myth\Auth\Test\Fakers\UserFaker;
use Tests\Support\AuthTestCase;

class AuthTestTraitTest extends AuthTestCase
{
use \Myth\Auth\Test\AuthTestTrait;

public function testResetServicesResetsAuthentication()
{
$authentication = service('authentication');

$user = fake(UserFaker::class);

$authentication->login($user);
$this->assertTrue($authentication->isLoggedIn());

$this->resetAuthServices();

$this->assertFalse(service('authentication')->check());
}

public function testResetServicesResetsAuthorization()
{
$authorization = service('authorization');
$authorization->setUserModel(model(UserFaker::class));

$this->resetAuthServices();

$authorization = service('authorization');
$model = $this->getPrivateProperty($authorization, 'userModel');

$this->assertNotInstanceOf(UserFaker::class, $model);
}
}

0 comments on commit 86a0256

Please sign in to comment.