Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
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 | pest()->group('unit'); |
| 11 | |
| 12 | use AppDemo\Shared\Application\Service\UserContextProvider; |
| 13 | use AppDemo\User\Domain\Email; |
| 14 | use AppDemo\User\Domain\HashedPassword; |
| 15 | use AppDemo\User\Domain\User; |
| 16 | use AppDemo\User\Domain\UserGroup; |
| 17 | use AppDemo\User\Infrastructure\InMemoryUserRepository; |
| 18 | use AppDemo\User\Infrastructure\Mapper\UserMapper; |
| 19 | use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator; |
| 20 | use Phexium\Plugin\SqlDriver\Adapter\InMemoryDriver; |
| 21 | use Tests\AppDemo\Fake\Application\Service\RbacPermissionService as FakeRbacPermissionService; |
| 22 | use Tests\AppDemo\Fake\Application\Service\SessionService as FakeSessionService; |
| 23 | |
| 24 | beforeEach(function (): void { |
| 25 | $this->sessionService = new FakeSessionService(); |
| 26 | $this->idGenerator = new TimestampIdGenerator(); |
| 27 | $mapper = new UserMapper($this->idGenerator); |
| 28 | $this->userRepository = new InMemoryUserRepository( |
| 29 | new InMemoryDriver(), |
| 30 | $mapper |
| 31 | ); |
| 32 | $this->rbacService = new FakeRbacPermissionService(); |
| 33 | |
| 34 | $this->provider = new UserContextProvider( |
| 35 | $this->sessionService, |
| 36 | $this->userRepository, |
| 37 | $this->rbacService, |
| 38 | ); |
| 39 | }); |
| 40 | |
| 41 | describe('Context creation', function (): void { |
| 42 | it('returns user context with user when authenticated', function (): void { |
| 43 | $fixture = new User( |
| 44 | $this->idGenerator->from(123), |
| 45 | Email::fromString('toto@example.com'), |
| 46 | HashedPassword::fromHash('$plain$hashedpassword'), |
| 47 | UserGroup::from('user') |
| 48 | ); |
| 49 | $this->userRepository->save($fixture); |
| 50 | $this->sessionService->setUserAuthenticated($fixture->getId(), $fixture->getEmail()); |
| 51 | |
| 52 | $userContext = $this->provider->getCurrentUserContext(); |
| 53 | |
| 54 | expect($userContext->isAuthenticated())->toBeTrue(); |
| 55 | |
| 56 | $user = $userContext->getUser(); |
| 57 | |
| 58 | expect($user->getId()->equals($fixture->getId()))->toBeTrue() |
| 59 | ->and($user->getEmail()->equals($fixture->getEmail()))->toBeTrue() |
| 60 | ; |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe('Fallback', function (): void { |
| 65 | it('returns user context without user when not authenticated', function (): void { |
| 66 | $userContext = $this->provider->getCurrentUserContext(); |
| 67 | |
| 68 | expect($userContext->isAuthenticated())->toBeFalse() |
| 69 | ->and($userContext->getUser())->toBeNull() |
| 70 | ; |
| 71 | }); |
| 72 | }); |