Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use AppDemo\Shared\Application\Service\UserContextProvider; |
| 11 | use AppDemo\User\Domain\Email; |
| 12 | use AppDemo\User\Domain\HashedPassword; |
| 13 | use AppDemo\User\Domain\User; |
| 14 | use AppDemo\User\Domain\UserGroup; |
| 15 | use AppDemo\User\Infrastructure\InMemoryUserRepository; |
| 16 | use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator; |
| 17 | use Phexium\Plugin\SqlDriver\Adapter\InMemoryDriver; |
| 18 | use Tests\AppDemo\Fake\Application\Service\RbacPermissionService as FakeRbacPermissionService; |
| 19 | use Tests\AppDemo\Fake\Application\Service\SessionService as FakeSessionService; |
| 20 | |
| 21 | beforeEach(function (): void { |
| 22 | $this->sessionService = new FakeSessionService(); |
| 23 | $this->userRepository = new InMemoryUserRepository( |
| 24 | new InMemoryDriver(), |
| 25 | new TimestampIdGenerator() |
| 26 | ); |
| 27 | $this->rbacService = new FakeRbacPermissionService(); |
| 28 | $this->idGenerator = new TimestampIdGenerator(); |
| 29 | |
| 30 | $this->provider = new UserContextProvider( |
| 31 | $this->sessionService, |
| 32 | $this->userRepository, |
| 33 | $this->rbacService, |
| 34 | ); |
| 35 | }); |
| 36 | |
| 37 | test('Should return user context with user when authenticated', function (): void { |
| 38 | $fixture = new User( |
| 39 | $this->idGenerator->from(123), |
| 40 | Email::fromString('toto@example.com'), |
| 41 | HashedPassword::fromHash('$plain$hashedpassword'), |
| 42 | UserGroup::from('user') |
| 43 | ); |
| 44 | $this->userRepository->save($fixture); |
| 45 | $this->sessionService->setUserAuthenticated($fixture->getId(), $fixture->getEmail()); |
| 46 | |
| 47 | $userContext = $this->provider->getCurrentUserContext(); |
| 48 | |
| 49 | expect($userContext->isAuthenticated())->toBeTrue(); |
| 50 | |
| 51 | $user = $userContext->getUser(); |
| 52 | |
| 53 | expect($user->getId()->equals($fixture->getId()))->toBeTrue() |
| 54 | ->and($user->getEmail()->equals($fixture->getEmail()))->toBeTrue() |
| 55 | ; |
| 56 | }); |
| 57 | |
| 58 | test('Should return user context without user when not authenticated', function (): void { |
| 59 | $userContext = $this->provider->getCurrentUserContext(); |
| 60 | |
| 61 | expect($userContext->isAuthenticated())->toBeFalse() |
| 62 | ->and($userContext->getUser())->toBeNull() |
| 63 | ; |
| 64 | }); |