Passed
Pull Request — master (#108)
by Alexander
01:46
created

SessionMiddlewareTest::setUpRequestHandlerMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests\Session;
5
6
use Nyholm\Psr7\Response;
7
use Psr\Http\Message\ResponseInterface;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\UriInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Cache\Tests\TestCase;
13
use Yiisoft\Yii\Web\Session\SessionException;
14
use Yiisoft\Yii\Web\Session\SessionInterface;
15
use Yiisoft\Yii\Web\Session\SessionMiddleware;
16
17
class SessionMiddlewareTest extends TestCase
18
{
19
    const COOKIE_PARAMETERS = [
20
        'path' => 'examplePath',
21
        'domain' => 'exampleDomain',
22
        'httponly' => 'httponly',
23
        'samesite' => 'Strict',
24
        'lifetime' => 3600,
25
        'secure' => true,
26
    ];
27
28
    const CURRENT_SID = 'exampleCurrentSidValue';
29
    const REQUEST_SID = 'exampleRequestSidValue';
30
    const SESSION_NAME = 'exampleSessionName';
31
32
    /**
33
     * @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $requestHandlerMock;
36
37
    /**
38
     * @var SessionInterface|PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $sessionMock;
41
42
    /**
43
     * @var ServerRequestInterface|PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $requestMock;
46
47
    /**
48
     * @var UriInterface|PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $uriMock;
51
52
    /**
53
     * @var SessionMiddleware
54
     */
55
    private $sessionMiddleware;
56
57
    public function setUp()
58
    {
59
        parent::setUp();
60
        $this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class);
61
        $this->sessionMock = $this->createMock(SessionInterface::class);
62
        $this->sessionMiddleware = new SessionMiddleware($this->sessionMock);
63
        $this->requestMock = $this->createMock(ServerRequestInterface::class);
64
        $this->uriMock = $this->createMock(UriInterface::class);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function processDiscardsSessionWhenRequestHandlerThrowsException()
71
    {
72
        $this->requestHandlerMock
73
            ->expects($this->once())
74
            ->method('handle')
75
            ->willThrowException(new \Exception());
76
77
        $this->sessionMock
78
            ->expects($this->once())
79
            ->method('discard');
80
81
        $this->expectException(\Exception::class);
82
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function processThrowsSessionExceptionWhenConnectionIsNotUsingHttps()
89
    {
90
        $this->setUpSessionMock();
91
        $this->setUpRequestMock(false);
92
        $this->expectException(SessionException::class);
93
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function processGetsDomainFromRequestWhenDomainCookieParameterNotProvided()
100
    {
101
        $this->setUpSessionMock(false);
102
        $this->setUpRequestMock();
103
104
        $this->uriMock
105
            ->expects($this->once())
106
            ->method('getHost')
107
            ->willReturn('domain');
108
109
        $response = new Response();
110
        $this->setUpRequestHandlerMock($response);
111
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function processDoesNotAlterResponseIfSessionIsNotActive()
118
    {
119
        $this->setUpSessionMock(true, false);
120
        $this->setUpRequestMock();
121
122
        $response = new Response();
123
        $this->setUpRequestHandlerMock($response);
124
125
        $result = $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
126
        $this->assertEquals($response, $result);
127
    }
128
129
    private function setUpRequestHandlerMock(ResponseInterface $response)
130
    {
131
        $this->requestHandlerMock
132
            ->expects($this->once())
133
            ->method('handle')
134
            ->willReturn($response);
135
    }
136
137
    private function setUpSessionMock(bool $cookieDomainProvided = true, bool $isActive = true)
138
    {
139
        $this->sessionMock
140
            ->expects($this->any())
141
            ->method('isActive')
142
            ->willReturn($isActive);
143
144
        $this->sessionMock
145
            ->expects($this->any())
146
            ->method('getName')
147
            ->willReturn(self::SESSION_NAME);
148
149
        $this->sessionMock
150
            ->expects($this->any())
151
            ->method('getID')
152
            ->willReturn(self::CURRENT_SID);
153
154
        $cookieParams = self::COOKIE_PARAMETERS;
155
        if (!$cookieDomainProvided) {
156
            $cookieParams['domain'] = '';
157
        }
158
159
        $this->sessionMock
160
            ->expects($this->any())
161
            ->method('getCookieParameters')
162
            ->willReturn($cookieParams);
163
    }
164
165
    private function setUpRequestMock(bool $isConnectionSecure = true)
166
    {
167
        $uriScheme = $isConnectionSecure ? 'https' : 'http';
168
        $this->setUpUriMock($uriScheme);
169
170
        $this->requestMock
171
            ->expects($this->any())
172
            ->method('getUri')
173
            ->willReturn($this->uriMock);
174
175
        $cookieParams[self::SESSION_NAME] = self::CURRENT_SID;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$cookieParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cookieParams = array(); before regardless.
Loading history...
176
        $this->requestMock
177
            ->expects($this->any())
178
            ->method('getCookieParams')
179
            ->willReturn(self::REQUEST_SID);
180
    }
181
182
    private function setUpUriMock(string $uriScheme)
183
    {
184
        $this->uriMock
185
            ->expects($this->any())
186
            ->method('getScheme')
187
            ->willReturn($uriScheme);
188
    }
189
}
190