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