This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: support InteractsWithAuthentication in test case
Showing
3 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src/foundation/src/Testing/Concerns/InteractsWithAuthentication.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwooleTW\Hyperf\Foundation\Testing\Concerns; | ||
|
||
use SwooleTW\Hyperf\Auth\Contracts\Authenticatable as UserContract; | ||
use SwooleTW\Hyperf\Auth\Contracts\FactoryContract as AuthManagerContract; | ||
|
||
trait InteractsWithAuthentication | ||
{ | ||
/** | ||
* Set the currently logged in user for the application. | ||
*/ | ||
public function actingAs(UserContract $user, ?string $guard = null): static | ||
{ | ||
return $this->be($user, $guard); | ||
} | ||
|
||
/** | ||
* Set the currently logged in user for the application. | ||
*/ | ||
public function be(UserContract $user, ?string $guard = null): static | ||
{ | ||
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) { | ||
$user->wasRecentlyCreated = false; | ||
} | ||
|
||
$this->app->get(AuthManagerContract::class) | ||
->guard($guard) | ||
->setUser($user); | ||
|
||
$this->app->get(AuthManagerContract::class) | ||
->shouldUse($guard); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Assert that the user is authenticated. | ||
*/ | ||
public function assertAuthenticated(?string $guard = null): static | ||
{ | ||
$this->assertTrue($this->isAuthenticated($guard), 'The user is not authenticated'); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Assert that the user is not authenticated. | ||
*/ | ||
public function assertGuest(?string $guard = null): static | ||
{ | ||
$this->assertFalse($this->isAuthenticated($guard), 'The user is authenticated'); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Return true if the user is authenticated, false otherwise. | ||
*/ | ||
protected function isAuthenticated(?string $guard = null): bool | ||
{ | ||
return $this->app | ||
->get(AuthManagerContract::class) | ||
->guard($guard) | ||
->check(); | ||
} | ||
|
||
/** | ||
* Assert that the user is authenticated as the given user. | ||
*/ | ||
public function assertAuthenticatedAs(UserContract $user, ?string $guard = null): static | ||
{ | ||
$expected = $this->app | ||
->get(AuthManagerContract::class) | ||
->guard($guard) | ||
->user(); | ||
|
||
$this->assertNotNull($expected, 'The current user is not authenticated.'); | ||
|
||
$this->assertInstanceOf( | ||
get_class($expected), | ||
$user, | ||
'The currently authenticated user is not who was expected' | ||
); | ||
|
||
$this->assertSame( | ||
$expected->getAuthIdentifier(), | ||
$user->getAuthIdentifier(), | ||
'The currently authenticated user is not who was expected' | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Assert that the given credentials are valid. | ||
*/ | ||
public function assertCredentials(array $credentials, ?string $guard = null): static | ||
{ | ||
$this->assertTrue( | ||
$this->hasCredentials($credentials, $guard), | ||
'The given credentials are invalid.' | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Assert that the given credentials are invalid. | ||
*/ | ||
public function assertInvalidCredentials(array $credentials, ?string $guard = null): static | ||
{ | ||
$this->assertFalse( | ||
$this->hasCredentials($credentials, $guard), | ||
'The given credentials are valid.' | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Return true if the credentials are valid, false otherwise. | ||
*/ | ||
protected function hasCredentials(array $credentials, ?string $guard = null): bool | ||
{ | ||
$provider = $this->app | ||
->get(AuthManagerContract::class) | ||
->guard($guard) | ||
->getProvider(); | ||
|
||
$user = $provider->retrieveByCredentials($credentials); | ||
|
||
return $user && $provider->validateCredentials($user, $credentials); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/Foundation/Testing/Concerns/InteractsWithAuthenticationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwooleTW\Hyperf\Tests\Foundation\Testing\Concerns; | ||
|
||
use Hyperf\Context\Context; | ||
use Hyperf\Contract\ConfigInterface; | ||
use Mockery; | ||
use SwooleTW\Hyperf\Auth\Contracts\Authenticatable as UserContract; | ||
use SwooleTW\Hyperf\Auth\Contracts\FactoryContract as AuthManagerContract; | ||
use SwooleTW\Hyperf\Auth\Contracts\Guard; | ||
use SwooleTW\Hyperf\Foundation\Testing\Concerns\InteractsWithAuthentication; | ||
use SwooleTW\Hyperf\Tests\Foundation\Testing\ApplicationTestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class InteractsWithAuthenticationTest extends ApplicationTestCase | ||
{ | ||
use InteractsWithAuthentication; | ||
|
||
public function testAssertAsGuest() | ||
{ | ||
$guard = Mockery::mock(Guard::class); | ||
$guard->shouldReceive('check') | ||
->once() | ||
->andReturn(false); | ||
|
||
$this->app->get(AuthManagerContract::class) | ||
->extend('foo', fn () => $guard); | ||
$this->app->get(ConfigInterface::class) | ||
->set('auth.guards.foo', [ | ||
'driver' => 'foo', | ||
'provider' => 'users', | ||
]); | ||
|
||
Context::set('__auth.defaults.guard', 'foo'); | ||
|
||
$this->assertGuest(); | ||
$this->assertFalse($this->isAuthenticated()); | ||
} | ||
|
||
public function testAssertActingAs() | ||
{ | ||
$guard = Mockery::mock(Guard::class); | ||
$guard->shouldReceive('check') | ||
->once() | ||
->andReturn(true); | ||
$guard->shouldReceive('setUser') | ||
->once() | ||
->andReturn($user = Mockery::mock(UserContract::class)); | ||
$guard->shouldReceive('user') | ||
->once() | ||
->andReturn($user); | ||
$user->shouldReceive('getAuthIdentifier') | ||
->twice() | ||
->andReturn('id'); | ||
|
||
$this->app->get(AuthManagerContract::class) | ||
->extend('foo', fn () => $guard); | ||
$this->app->get(ConfigInterface::class) | ||
->set('auth.guards.foo', [ | ||
'driver' => 'foo', | ||
'provider' => 'users', | ||
]); | ||
|
||
Context::set('__auth.defaults.guard', 'foo'); | ||
|
||
$this->actingAs($user); | ||
|
||
$this->assertTrue($this->isAuthenticated()); | ||
$this->assertAuthenticatedAs($user); | ||
} | ||
} |