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
10use AppDemo\Shared\Application\Middleware\RbacPermissionMiddleware;
11use AppDemo\Shared\Domain\UserContext;
12use AppDemo\User\Domain\UserGroup;
13use Nyholm\Psr7\Factory\Psr17Factory;
14use Nyholm\Psr7\Response;
15use Nyholm\Psr7\ServerRequest;
16use Tests\AppDemo\Fake\Application\Service\RbacPermissionService as FakeRbacPermissionService;
17use Tests\AppDemo\Fixture\UserMother;
18use Tests\Phexium\Fake\Plugin\Http\RequestHandler as FakeRequestHandler;
19
20const REQUIRED_PERMISSION = 'some-permission';
21
22beforeEach(function (): void {
23    $this->rbacPermissionService = new FakeRbacPermissionService();
24    $this->responseFactory = new Psr17Factory();
25
26    $this->middleware = new RbacPermissionMiddleware(
27        $this->rbacPermissionService,
28        $this->responseFactory,
29        REQUIRED_PERMISSION
30    );
31});
32
33test('Should proceed to next handler when user has permission', function (): void {
34    $user = UserMother::user();
35    $userContext = new UserContext($user, $this->rbacPermissionService);
36    $expectedResponse = new Response(200);
37
38    $this->rbacPermissionService->addPermission(UserGroup::User, REQUIRED_PERMISSION);
39
40    $request = new ServerRequest('GET', '/');
41    $request = $request->withAttribute('user_context', $userContext);
42    $handler = new FakeRequestHandler($expectedResponse);
43
44    $response = $this->middleware->process($request, $handler);
45
46    expect($response)->toBe($expectedResponse)
47        ->and($handler->getHandleCallCount())->toBe(1)
48    ;
49});
50
51test('Should return 403 when user does not have permission', function (): void {
52    $user = UserMother::user();
53    $userContext = new UserContext($user, $this->rbacPermissionService);
54
55    $request = new ServerRequest('GET', '/');
56    $request = $request->withAttribute('user_context', $userContext);
57    $handler = new FakeRequestHandler(new Response(200));
58
59    $response = $this->middleware->process($request, $handler);
60
61    expect($response->getStatusCode())->toBe(RbacPermissionMiddleware::STATUS_FORBIDDEN)
62        ->and((string) $response->getBody())->toBe('Permission denied.')
63        ->and($handler->getHandleCallCount())->toBe(0)
64    ;
65});
66
67test('Should return 403 when user is not authenticated', function (): void {
68    $userContext = new UserContext(null, $this->rbacPermissionService);
69
70    $request = new ServerRequest('GET', '/');
71    $request = $request->withAttribute('user_context', $userContext);
72    $handler = new FakeRequestHandler(new Response(200));
73
74    $response = $this->middleware->process($request, $handler);
75
76    expect($response->getStatusCode())->toBe(RbacPermissionMiddleware::STATUS_FORBIDDEN)
77        ->and((string) $response->getBody())->toBe('Authentication required.')
78        ->and($handler->getHandleCallCount())->toBe(0)
79    ;
80});