Passed
Push — master ( dda347...d192b8 )
by Alexander
04:54
created

testProcessDiscardsSessionWhenRequestHandlerThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests\Session;
5
6
use Nyholm\Psr7\Response;
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_MockObject_MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_MockObject_MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Message\UriInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
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
    private const COOKIE_PARAMETERS = [
20
        'path' => 'examplePath',
21
        'domain' => 'exampleDomain',
22
        'httponly' => 'httponly',
23
        'samesite' => 'Strict',
24
        'lifetime' => 3600,
25
        'secure' => true,
26
    ];
27
28
    private const CURRENT_SID = 'exampleCurrentSidValue';
29
    private const REQUEST_SID = 'exampleRequestSidValue';
30
    private 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(): void
58
    {
59
        parent::setUp();
60
        $this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...andlerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...RequestHandlerInterface of property $requestHandlerMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->sessionMock = $this->createMock(SessionInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Yiisof...essionInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...ession\SessionInterface of property $sessionMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        $this->sessionMiddleware = new SessionMiddleware($this->sessionMock);
63
        $this->requestMock = $this->createMock(ServerRequestInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...equestInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...\ServerRequestInterface of property $requestMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        $this->uriMock = $this->createMock(UriInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...ge\UriInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...tp\Message\UriInterface of property $uriMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
    }
66
67
    public function testProcessDiscardsSessionWhenRequestHandlerThrowsException(): void
68
    {
69
        $this->requestHandlerMock
70
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Server\RequestHandlerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            ->/** @scrutinizer ignore-call */ 
71
              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            ->method('handle')
72
            ->willThrowException(new \Exception());
73
74
        $this->sessionMock
75
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Yiisoft\Yii\Web\Session\SessionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            ->/** @scrutinizer ignore-call */ 
76
              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            ->method('discard');
77
78
        $this->expectException(\Exception::class);
79
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
80
    }
81
82
    public function testProcessThrowsSessionExceptionWhenConnectionIsNotUsingHttps(): void
83
    {
84
        $this->setUpSessionMock();
85
        $this->setUpRequestMock(false);
86
        $this->expectException(SessionException::class);
87
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
88
    }
89
90
    public function testProcessGetsDomainFromRequestWhenDomainCookieParameterNotProvided(): void
91
    {
92
        $this->setUpSessionMock(false);
93
        $this->setUpRequestMock();
94
95
        $this->uriMock
96
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Message\UriInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            ->/** @scrutinizer ignore-call */ 
97
              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            ->method('getHost')
98
            ->willReturn('domain');
99
100
        $response = new Response();
101
        $this->setUpRequestHandlerMock($response);
102
        $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
103
    }
104
105
    public function testProcessDoesNotAlterResponseIfSessionIsNotActive(): void
106
    {
107
        $this->setUpSessionMock(true, false);
108
        $this->setUpRequestMock();
109
110
        $response = new Response();
111
        $this->setUpRequestHandlerMock($response);
112
113
        $result = $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock);
114
        $this->assertEquals($response, $result);
115
    }
116
117
    private function setUpRequestHandlerMock(ResponseInterface $response): void
118
    {
119
        $this->requestHandlerMock
120
            ->expects($this->once())
121
            ->method('handle')
122
            ->willReturn($response);
123
    }
124
125
    private function setUpSessionMock(bool $cookieDomainProvided = true, bool $isActive = true): void
126
    {
127
        $this->sessionMock
128
            ->expects($this->any())
129
            ->method('isActive')
130
            ->willReturn($isActive);
131
132
        $this->sessionMock
133
            ->expects($this->any())
134
            ->method('getName')
135
            ->willReturn(self::SESSION_NAME);
136
137
        $this->sessionMock
138
            ->expects($this->any())
139
            ->method('getID')
140
            ->willReturn(self::CURRENT_SID);
141
142
        $cookieParams = self::COOKIE_PARAMETERS;
143
        if (!$cookieDomainProvided) {
144
            $cookieParams['domain'] = '';
145
        }
146
147
        $this->sessionMock
148
            ->expects($this->any())
149
            ->method('getCookieParameters')
150
            ->willReturn($cookieParams);
151
    }
152
153
    private function setUpRequestMock(bool $isConnectionSecure = true): void
154
    {
155
        $uriScheme = $isConnectionSecure ? 'https' : 'http';
156
        $this->setUpUriMock($uriScheme);
157
158
        $this->requestMock
159
            ->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Message\ServerRequestInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
            ->/** @scrutinizer ignore-call */ 
160
              expects($this->any())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
            ->method('getUri')
161
            ->willReturn($this->uriMock);
162
163
        $requestCookieParams = [
164
            self::SESSION_NAME => self::REQUEST_SID,
165
        ];
166
        $this->requestMock
167
            ->expects($this->any())
168
            ->method('getCookieParams')
169
            ->willReturn($requestCookieParams);
170
    }
171
172
    private function setUpUriMock(string $uriScheme): void
173
    {
174
        $this->uriMock
175
            ->expects($this->any())
176
            ->method('getScheme')
177
            ->willReturn($uriScheme);
178
    }
179
}
180