1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
<?php
namespace Illuminate\Tests\Foundation;
use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\Concerns\InteractsWithAuthentication;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class FoundationAuthenticationTest extends TestCase
{
use InteractsWithAuthentication;
/**
* @var \Mockery
*/
protected $app;
/**
* @return array
*/
protected $credentials = [
'email' => 'someone@laravel.com',
'password' => 'secret_password',
];
/**
* @var \Mockery
*/
protected function mockGuard()
{
$guard = m::mock(Guard::class);
$auth = m::mock(AuthManager::class);
$auth->shouldReceive('guard')
->once()
->andReturn($guard);
$this->app = m::mock(Application::class);
$this->app->shouldReceive('make')
->once()
->withArgs(['auth'])
->andReturn($auth);
return $guard;
}
protected function tearDown(): void
{
m::close();
}
public function testAssertAuthenticated()
{
$this->mockGuard()
->shouldReceive('check')
->once()
->andReturn(true);
$this->assertAuthenticated();
}
public function testAssertGuest()
{
$this->mockGuard()
->shouldReceive('check')
->once()
->andReturn(false);
$this->assertGuest();
}
public function testAssertAuthenticatedAs()
{
$expected = m::mock(Authenticatable::class);
$expected->shouldReceive('getAuthIdentifier')
->andReturn('1');
$this->mockGuard()
->shouldReceive('user')
->once()
->andReturn($expected);
$user = m::mock(Authenticatable::class);
$user->shouldReceive('getAuthIdentifier')
->andReturn('1');
$this->assertAuthenticatedAs($user);
}
protected function setupProvider(array $credentials)
{
$user = m::mock(Authenticatable::class);
$provider = m::mock(UserProvider::class);
$provider->shouldReceive('retrieveByCredentials')
->with($credentials)
->andReturn($user);
$provider->shouldReceive('validateCredentials')
->with($user, $credentials)
->andReturn($this->credentials === $credentials);
$this->mockGuard()
->shouldReceive('getProvider')
->once()
->andReturn($provider);
}
public function testAssertCredentials()
{
$this->setupProvider($this->credentials);
$this->assertCredentials($this->credentials);
}
public function testAssertCredentialsMissing()
{
$credentials = [
'email' => 'invalid',
'password' => 'credentials',
];
$this->setupProvider($credentials);
$this->assertInvalidCredentials($credentials);
}
}
|