123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?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',
- ];
- /**
- * @return \Illuminate\Contracts\Auth\Guard|\Mockery\LegacyMockInterface|\Mockery\MockInterface
- */
- 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);
- }
- }
|