Passed
Push — master ( c256f6...7fc962 )
by Adrien
05:50
created

AuthenticationMiddlewareTest::testUserArchived()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Middleware;
6
7
use Application\Middleware\AuthenticationMiddleware;
8
use Application\Model\User;
9
use Application\Repository\UserRepository;
10
use Laminas\Diactoros\Response;
11
use Laminas\Diactoros\ServerRequest;
12
use Mezzio\Session\Session;
13
use Mezzio\Session\SessionInterface;
14
use Mezzio\Session\SessionMiddleware;
15
use PHPUnit\Framework\TestCase;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
class AuthenticationMiddlewareTest extends TestCase
21
{
22
    public function testEmptySession(): void
23
    {
24
        $session = $this->process(false, null);
25
26
        self::assertFalse($session->has('user'));
27
        self::assertTrue($session->has('other'));
28
        self::assertNull(User::getCurrent());
29
    }
30
31
    public function testUserNotFound(): void
32
    {
33
        $user = null;
34
        $session = $this->process(true, $user);
35
36
        self::assertFalse($session->has('user'));
37
        self::assertFalse($session->has('other'));
38
        self::assertNull(User::getCurrent());
39
    }
40
41
    public function testUserNoLimit(): void
42
    {
43
        $user = new User();
44
        $session = $this->process(true, $user);
45
46
        self::assertTrue($session->has('user'));
47
        self::assertTrue($session->has('other'));
48
        self::assertSame($user, User::getCurrent());
49
    }
50
51
    public function testUserArchived(): void
52
    {
53
        $user = new User();
54
        $user->setStatus(User::STATUS_ARCHIVED);
55
        $session = $this->process(true, $user);
56
57
        self::assertFalse($session->has('user'));
58
        self::assertFalse($session->has('other'));
59
        self::assertNull(User::getCurrent());
60
    }
61
62
    private function process(bool $userInSession, ?User $user): SessionInterface
63
    {
64
        User::setCurrent(null);
65
66
        $userRepository = new class($user) extends UserRepository {
67
            public function __construct(private readonly ?User $user)
68
            {
69
            }
70
71
            public function getOneById(int $id): ?User
72
            {
73
                return $this->user;
74
            }
75
        };
76
77
        $session = new Session(['other' => 'foo']);
78
        if ($userInSession) {
79
            $session->set('user', 123);
80
        }
81
        $request = new ServerRequest();
82
        $request = $request->withAttribute(SessionMiddleware::SESSION_ATTRIBUTE, $session);
83
84
        $response = new Response();
85
        $handler = new class($response) implements RequestHandlerInterface {
86
            public function __construct(private readonly ResponseInterface $response)
87
            {
88
            }
89
90
            public function handle(ServerRequestInterface $request): ResponseInterface
91
            {
92
                return $this->response;
93
            }
94
        };
95
96
        $middleware = new AuthenticationMiddleware($userRepository);
97
        $middleware->process($request, $handler);
98
99
        return $session;
100
    }
101
}
102