Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test trait #247

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}