Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
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
8declare(strict_types=1);
9
10use AppDemo\Shared\Application\Service\UserContextProvider;
11use AppDemo\User\Domain\Email;
12use AppDemo\User\Domain\HashedPassword;
13use AppDemo\User\Domain\User;
14use AppDemo\User\Domain\UserGroup;
15use AppDemo\User\Infrastructure\InMemoryUserRepository;
16use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator;
17use Phexium\Plugin\SqlDriver\Adapter\InMemoryDriver;
18use Tests\AppDemo\Fake\Application\Service\RbacPermissionService as FakeRbacPermissionService;
19use Tests\AppDemo\Fake\Application\Service\SessionService as FakeSessionService;
20
21beforeEach(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
37test('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
58test('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});