testHandleModifiesSessionIfSessionIsInitialized()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Middleware;
6
7
use Opulence\Http\Requests\Request;
8
use Opulence\Http\Responses\Response;
9
use Opulence\Sessions\ISession;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
13
class LastGridPageTest extends TestCase
14
{
15
    /** @var LastGridPage - System Under Test */
16
    protected LastGridPage $sut;
17
18
    /** @var MockObject|ISession */
19
    protected $sessionMock;
20
21
    public function setUp(): void
22
    {
23
        $this->sessionMock = $this->createMock(ISession::class);
24
25
        $this->sut = new LastGridPage($this->sessionMock);
26
    }
27
28
    public function testHandleDoesNotModifySessionIfResponseIsError()
29
    {
30
        $this->sessionMock->expects($this->never())->method('set');
31
32
        $requestStub  = new Request([], [], [], [], [], [], null);
33
        $responseStub = new Response();
34
        $responseStub->setStatusCode(rand(400, 600));
35
36
        $next = function () use ($responseStub) {
37
            return $responseStub;
38
        };
39
40
        $actualResult = $this->sut->handle($requestStub, $next);
41
42
        $this->assertSame($responseStub, $actualResult);
43
    }
44
45
    public function testHandleDoesNotModifySessionIfSessionIsNotInitialized()
46
    {
47
        $this->sessionMock->expects($this->once())->method('has')->willReturn(false);
48
        $this->sessionMock->expects($this->never())->method('set');
49
50
        $requestStub  = new Request([], [], [], [], [], [], null);
51
        $responseStub = new Response();
52
53
        $next = function () use ($responseStub) {
54
            return $responseStub;
55
        };
56
57
        $actualResult = $this->sut->handle($requestStub, $next);
58
59
        $this->assertSame($responseStub, $actualResult);
60
    }
61
62
    public function testHandleModifiesSessionIfSessionIsInitialized()
63
    {
64
        $this->sessionMock->expects($this->any())->method('has')->willReturn(true);
65
        $this->sessionMock->expects($this->once())->method('set');
66
67
        $requestStub  = new Request([], [], [], [], [], [], null);
68
        $responseStub = new Response();
69
70
        $next = function () use ($responseStub) {
71
            return $responseStub;
72
        };
73
74
        $actualResult = $this->sut->handle($requestStub, $next);
75
76
        $this->assertSame($responseStub, $actualResult);
77
    }
78
}
79