ldc-boutique-cnc-sable/tests/Unit/Infra/Adapters/Services/AppAdminAuthServiceTest.php
2026-10-27 09:58:12 +01:00

121 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Exceptions\AuthServiceException;
use App\Models\User;
use Illuminate\Contracts\Auth\StatefulGuard;
use Infrastructure\Adapters\Services\AppAdminAuthService;
use Mockery\MockInterface;
describe('AppAdminAuthService', function () {
it('checks abilities with user', function () {
// GIVEN
/** @var StatefulGuard|MockInterface $guard */
$guard = \Mockery::mock(StatefulGuard::class);
$user = \Mockery::mock(User::class);
$user->shouldReceive('can')->with('create-post', [])->andReturn(true);
$guard->shouldReceive('user')->andReturn($user);
$service = new AppAdminAuthService($guard);
// WHEN
$result = $service->canWithUser('create-post');
// THEN
expect($result)->toBeTrue();
});
it('returns false for abilities if user is null', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$guard->shouldReceive('user')->andReturn(null);
$service = new AppAdminAuthService($guard);
// WHEN
$result = $service->canWithUser('create-post');
// THEN
expect($result)->toBeFalse();
});
it('checks if user is logged in', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$guard->shouldReceive('check')->andReturn(true);
$service = new AppAdminAuthService($guard);
// WHEN
$check = $service->check();
$guest = $service->guest();
// THEN
expect($check)->toBeTrue()
->and($guest)->toBeFalse();
});
it('gets user safely', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$user = \Mockery::mock(User::class);
$guard->shouldReceive('user')->andReturn($user);
$service = new AppAdminAuthService($guard);
// WHEN
$returned = $service->getUser();
// THEN
expect($returned)->toBeInstanceOf(User::class)
->and($returned)->not->toBe($user); // cloned
});
it('throws exception when user is not a backend User model', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$guard->shouldReceive('user')->andReturn(new \stdClass());
$service = new AppAdminAuthService($guard);
// WHEN
$action = fn () => $service->getUser();
// THEN
expect($action)->toThrow(\InvalidArgumentException::class, 'Auth service needs a backend guard');
});
it('gets user as mandatory', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$user = new User();
$user->id = '123';
$guard->shouldReceive('user')->andReturn($user);
$service = new AppAdminAuthService($guard);
// WHEN
$returned = $service->getUserAsMandatory();
$id = $service->getMandatoryUserId();
// THEN
expect($returned)->toBeInstanceOf(User::class)
->and($id)->toBe('123');
});
it('throws exception when getting user as mandatory but user is null', function () {
// GIVEN
$guard = \Mockery::mock(StatefulGuard::class);
$guard->shouldReceive('user')->andReturn(null);
$service = new AppAdminAuthService($guard);
// WHEN
$action = fn () => $service->getUserAsMandatory();
// THEN
expect($action)->toThrow(AuthServiceException::class, 'Authenticated user is required from here');
});
});