Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit

Permalink
feat: support InteractsWithAuthentication in test case
Browse files Browse the repository at this point in the history
albertcht committed Dec 16, 2024
1 parent 799f45d commit e192958
Showing 3 changed files with 216 additions and 1 deletion.
137 changes: 137 additions & 0 deletions src/foundation/src/Testing/Concerns/InteractsWithAuthentication.php
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);
}
}
4 changes: 3 additions & 1 deletion src/foundation/src/Testing/TestCase.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
namespace SwooleTW\Hyperf\Foundation\Testing;

use Mockery as m;
use SwooleTW\Hyperf\Foundation\Testing\Concerns\InteractsWithAuthentication;
use SwooleTW\Hyperf\Foundation\Testing\Concerns\InteractsWithConsole;
use SwooleTW\Hyperf\Foundation\Testing\Concerns\InteractsWithContainer;
use SwooleTW\Hyperf\Foundation\Testing\Concerns\InteractsWithDatabase;
@@ -23,10 +24,11 @@ class TestCase extends \PHPUnit\Framework\TestCase
{
use InteractsWithContainer;
use MakesHttpRequests;
use MocksApplicationServices;
use InteractsWithAuthentication;
use InteractsWithConsole;
use InteractsWithDatabase;
use InteractsWithTime;
use MocksApplicationServices;

/**
* The callbacks that should be run after the application is created.
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);
}
}

0 comments on commit e192958

Please sign in to comment.