Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
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
10pest()->group('unit');
11
12use AppDemo\Shared\Application\Middleware\UserContextMiddleware;
13use AppDemo\Shared\Domain\UserContext;
14use Nyholm\Psr7\Response;
15use Nyholm\Psr7\ServerRequest;
16use Tests\AppDemo\Fake\Application\Service\RbacPermissionService as FakeRbacPermissionService;
17use Tests\AppDemo\Fake\Application\Service\SessionService as FakeSessionService;
18use Tests\AppDemo\Fake\Application\Service\UserContextProvider as FakeUserContextProvider;
19use Tests\Phexium\Fake\Plugin\Http\RequestHandler as FakeRequestHandler;
20use Tests\Phexium\Fake\Plugin\Session\Session as FakeSession;
21
22beforeEach(function (): void {
23    $this->sessionService = new FakeSessionService();
24    $this->userContextProvider = new FakeUserContextProvider();
25    $this->middleware = new UserContextMiddleware(
26        $this->sessionService,
27        $this->userContextProvider
28    );
29});
30
31describe('Context injection', function (): void {
32    it('adds user context from provider', function (): void {
33        $request = new ServerRequest('GET', '/');
34        $expectedResponse = new Response(200);
35        $handler = new FakeRequestHandler($expectedResponse);
36        $rbacService = new FakeRbacPermissionService();
37        $userContext = new UserContext(null, $rbacService);
38
39        $this->userContextProvider->setUserContext($userContext);
40
41        $result = $this->middleware->process($request, $handler);
42
43        expect($result)->toBe($expectedResponse)
44            ->and($handler->getHandleCallCount())->toBe(1)
45        ;
46
47        $handledRequest = $handler->getLastRequest();
48        expect($handledRequest)->not->toBeNull()
49            ->and($handledRequest->getAttribute('user_context'))->toBe($userContext)
50        ;
51    });
52});
53
54describe('Error handling', function (): void {
55    it('starts session if not already started', function (): void {
56        $request = new ServerRequest('GET', '/');
57        $expectedResponse = new Response(200);
58        $handler = new FakeRequestHandler($expectedResponse);
59        $rbacService = new FakeRbacPermissionService();
60        $userContext = new UserContext(null, $rbacService);
61
62        $fakeSession = new FakeSession();
63        $this->sessionService = new FakeSessionService($fakeSession);
64        $this->middleware = new UserContextMiddleware(
65            $this->sessionService,
66            $this->userContextProvider
67        );
68
69        $this->userContextProvider->setUserContext($userContext);
70
71        $this->middleware->process($request, $handler);
72
73        expect($fakeSession->isStarted())->toBeTrue();
74    });
75});