3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Persistent \Test \Unit \Model ;
7
9
8
- class SessionTest extends \PHPUnit \Framework \TestCase
10
+ use Magento \Framework \App \Request \Http ;
11
+ use Magento \Framework \Model \ActionValidator \RemoveAction ;
12
+ use Magento \Framework \Model \Context ;
13
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
14
+ use Magento \Framework \Session \Config \ConfigInterface ;
15
+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
16
+ use Magento \Framework \Stdlib \Cookie \PublicCookieMetadata ;
17
+ use Magento \Framework \Stdlib \Cookie \SensitiveCookieMetadata ;
18
+ use Magento \Framework \Stdlib \CookieManagerInterface ;
19
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
+ use Magento \Persistent \Model \Session ;
21
+ use PHPUnit \Framework \MockObject \MockObject ;
22
+ use PHPUnit \Framework \TestCase ;
23
+
24
+ class SessionTest extends TestCase
9
25
{
10
26
/**
11
- * @var \Magento\Persistent\Model\ Session
27
+ * @var Session
12
28
*/
13
29
protected $ session ;
14
30
15
31
/**
16
- * @var \Magento\Framework\Session\Config\ ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
32
+ * @var ConfigInterface|MockObject
17
33
*/
18
34
protected $ configMock ;
19
35
20
36
/**
21
- * @var \Magento\Framework\Stdlib\ CookieManagerInterface |\PHPUnit_Framework_MockObject_MockObject
37
+ * @var CookieManagerInterface|MockObject
22
38
*/
23
39
protected $ cookieManagerMock ;
24
40
25
41
/**
26
- * @var \Magento\Framework\Stdlib\Cookie\ CookieMetadataFactory |\PHPUnit_Framework_MockObject_MockObject
42
+ * @var CookieMetadataFactory|MockObject
27
43
*/
28
44
protected $ cookieMetadataFactoryMock ;
29
45
30
- protected function setUp ()
46
+ protected function setUp (): void
31
47
{
32
- $ helper = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
33
- $ this ->configMock = $ this ->createMock (\ Magento \ Framework \ Session \ Config \ ConfigInterface::class);
34
- $ this ->cookieManagerMock = $ this ->createMock (\ Magento \ Framework \ Stdlib \ CookieManagerInterface::class);
48
+ $ helper = new ObjectManager ($ this );
49
+ $ this ->configMock = $ this ->getMockForAbstractClass ( ConfigInterface::class);
50
+ $ this ->cookieManagerMock = $ this ->getMockForAbstractClass ( CookieManagerInterface::class);
35
51
$ this ->cookieMetadataFactoryMock = $ this ->getMockBuilder (
36
- \ Magento \ Framework \ Stdlib \ Cookie \ CookieMetadataFactory::class
52
+ CookieMetadataFactory::class
37
53
)->disableOriginalConstructor ()
38
54
->getMock ();
39
55
40
56
$ resourceMock = $ this ->getMockForAbstractClass (
41
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb::class,
57
+ AbstractDb::class,
42
58
[],
43
59
'' ,
44
60
false ,
@@ -47,24 +63,24 @@ protected function setUp()
47
63
['__wakeup ' , 'getIdFieldName ' , 'getConnection ' , 'beginTransaction ' , 'delete ' , 'commit ' , 'rollBack ' ]
48
64
);
49
65
50
- $ actionValidatorMock = $ this ->createMock (\ Magento \ Framework \ Model \ ActionValidator \ RemoveAction::class);
51
- $ actionValidatorMock ->expects ($ this ->any ())->method ('isAllowed ' )->will ( $ this -> returnValue ( true ) );
66
+ $ actionValidatorMock = $ this ->createMock (RemoveAction::class);
67
+ $ actionValidatorMock ->expects ($ this ->any ())->method ('isAllowed ' )->willReturn ( true );
52
68
53
69
$ context = $ helper ->getObject (
54
- \ Magento \ Framework \ Model \ Context::class,
70
+ Context::class,
55
71
[
56
72
'actionValidator ' => $ actionValidatorMock ,
57
73
]
58
74
);
59
75
60
76
$ this ->session = $ helper ->getObject (
61
- \ Magento \ Persistent \ Model \ Session::class,
77
+ Session::class,
62
78
[
63
79
'sessionConfig ' => $ this ->configMock ,
64
80
'cookieManager ' => $ this ->cookieManagerMock ,
65
81
'context ' => $ context ,
66
82
'cookieMetadataFactory ' => $ this ->cookieMetadataFactoryMock ,
67
- 'request ' => $ this ->createMock (\ Magento \ Framework \ App \ Request \ Http::class),
83
+ 'request ' => $ this ->createMock (Http::class),
68
84
'resource ' => $ resourceMock ,
69
85
]
70
86
);
@@ -74,8 +90,8 @@ public function testLoadByCookieKeyWithNull()
74
90
{
75
91
$ this ->cookieManagerMock ->expects ($ this ->once ())
76
92
->method ('getCookie ' )
77
- ->with (\ Magento \ Persistent \ Model \ Session::COOKIE_NAME )
78
- ->will ( $ this -> returnValue ( null ) );
93
+ ->with (Session::COOKIE_NAME )
94
+ ->willReturn ( null );
79
95
$ this ->session ->loadByCookieKey (null );
80
96
}
81
97
@@ -85,23 +101,22 @@ public function testLoadByCookieKeyWithNull()
85
101
public function testAfterDeleteCommit ()
86
102
{
87
103
$ cookiePath = 'some_path ' ;
88
- $ this ->configMock ->expects ($ this ->once ())->method ('getCookiePath ' )->will ( $ this -> returnValue ( $ cookiePath) );
89
- $ cookieMetadataMock = $ this ->getMockBuilder (\ Magento \ Framework \ Stdlib \ Cookie \ SensitiveCookieMetadata::class)
104
+ $ this ->configMock ->expects ($ this ->once ())->method ('getCookiePath ' )->willReturn ( $ cookiePath );
105
+ $ cookieMetadataMock = $ this ->getMockBuilder (SensitiveCookieMetadata::class)
90
106
->disableOriginalConstructor ()
91
107
->getMock ();
92
108
$ cookieMetadataMock ->expects ($ this ->once ())
93
109
->method ('setPath ' )
94
- ->with ($ cookiePath )
95
- ->will ($ this ->returnSelf ());
110
+ ->with ($ cookiePath )->willReturnSelf ();
96
111
$ this ->cookieMetadataFactoryMock ->expects ($ this ->once ())
97
112
->method ('createSensitiveCookieMetadata ' )
98
- ->will ( $ this -> returnValue ( $ cookieMetadataMock) );
113
+ ->willReturn ( $ cookieMetadataMock );
99
114
$ this ->cookieManagerMock ->expects (
100
115
$ this ->once ()
101
116
)->method (
102
117
'deleteCookie '
103
118
)->with (
104
- \ Magento \ Persistent \ Model \ Session::COOKIE_NAME ,
119
+ Session::COOKIE_NAME ,
105
120
$ cookieMetadataMock
106
121
);
107
122
$ this ->session ->afterDeleteCommit ();
@@ -113,32 +128,31 @@ public function testSetPersistentCookie()
113
128
$ duration = 1000 ;
114
129
$ key = 'sessionKey ' ;
115
130
$ this ->session ->setKey ($ key );
116
- $ cookieMetadataMock = $ this ->getMockBuilder (\ Magento \ Framework \ Stdlib \ Cookie \ PublicCookieMetadata::class)
131
+ $ cookieMetadataMock = $ this ->getMockBuilder (PublicCookieMetadata::class)
117
132
->disableOriginalConstructor ()
118
133
->getMock ();
119
134
$ cookieMetadataMock ->expects ($ this ->once ())
120
135
->method ('setPath ' )
121
- ->with ($ cookiePath )
122
- ->will ($ this ->returnSelf ());
136
+ ->with ($ cookiePath )->willReturnSelf ();
123
137
$ cookieMetadataMock ->expects ($ this ->once ())
124
138
->method ('setDuration ' )
125
- ->with ($ duration )
126
- ->will ($ this ->returnSelf ());
139
+ ->with ($ duration )->willReturnSelf ();
127
140
$ cookieMetadataMock ->expects ($ this ->once ())
128
141
->method ('setSecure ' )
129
- ->with (false )
130
- ->will ($ this ->returnSelf ());
142
+ ->with (false )->willReturnSelf ();
131
143
$ cookieMetadataMock ->expects ($ this ->once ())
132
144
->method ('setHttpOnly ' )
133
- ->with (true )
134
- ->will ($ this ->returnSelf ());
145
+ ->with (true )->willReturnSelf ();
146
+ $ cookieMetadataMock ->expects ($ this ->once ())
147
+ ->method ('setSameSite ' )
148
+ ->with ('Lax ' )->willReturnSelf ();
135
149
$ this ->cookieMetadataFactoryMock ->expects ($ this ->once ())
136
150
->method ('createPublicCookieMetadata ' )
137
- ->will ( $ this -> returnValue ( $ cookieMetadataMock) );
151
+ ->willReturn ( $ cookieMetadataMock );
138
152
$ this ->cookieManagerMock ->expects ($ this ->once ())
139
153
->method ('setPublicCookie ' )
140
154
->with (
141
- \ Magento \ Persistent \ Model \ Session::COOKIE_NAME ,
155
+ Session::COOKIE_NAME ,
142
156
$ key ,
143
157
$ cookieMetadataMock
144
158
);
@@ -160,36 +174,35 @@ public function testRenewPersistentCookie(
160
174
$ cookieValue = 'cookieValue ' ,
161
175
$ cookiePath = 'cookiePath '
162
176
) {
163
- $ cookieMetadataMock = $ this ->getMockBuilder (\ Magento \ Framework \ Stdlib \ Cookie \ PublicCookieMetadata::class)
177
+ $ cookieMetadataMock = $ this ->getMockBuilder (PublicCookieMetadata::class)
164
178
->disableOriginalConstructor ()
165
179
->getMock ();
166
180
$ cookieMetadataMock ->expects ($ this ->exactly ($ numCalls ))
167
181
->method ('setPath ' )
168
- ->with ($ cookiePath )
169
- ->will ($ this ->returnSelf ());
182
+ ->with ($ cookiePath )->willReturnSelf ();
170
183
$ cookieMetadataMock ->expects ($ this ->exactly ($ numCalls ))
171
184
->method ('setDuration ' )
172
- ->with ($ cookieDuration )
173
- ->will ($ this ->returnSelf ());
185
+ ->with ($ cookieDuration )->willReturnSelf ();
174
186
$ cookieMetadataMock ->expects ($ this ->exactly ($ numCalls ))
175
187
->method ('setSecure ' )
176
- ->with (false )
177
- ->will ($ this ->returnSelf ());
188
+ ->with (false )->willReturnSelf ();
178
189
$ cookieMetadataMock ->expects ($ this ->exactly ($ numCalls ))
179
190
->method ('setHttpOnly ' )
180
- ->with (true )
181
- ->will ($ this ->returnSelf ());
191
+ ->with (true )->willReturnSelf ();
192
+ $ cookieMetadataMock ->expects ($ this ->exactly ($ numCalls ))
193
+ ->method ('setSameSite ' )
194
+ ->with ('Lax ' )->willReturnSelf ();
182
195
$ this ->cookieMetadataFactoryMock ->expects ($ this ->exactly ($ numCalls ))
183
196
->method ('createPublicCookieMetadata ' )
184
- ->will ( $ this -> returnValue ( $ cookieMetadataMock) );
197
+ ->willReturn ( $ cookieMetadataMock );
185
198
$ this ->cookieManagerMock ->expects ($ this ->exactly ($ numGetCookieCalls ))
186
199
->method ('getCookie ' )
187
- ->with (\ Magento \ Persistent \ Model \ Session::COOKIE_NAME )
188
- ->will ( $ this -> returnValue ( $ cookieValue) );
200
+ ->with (Session::COOKIE_NAME )
201
+ ->willReturn ( $ cookieValue );
189
202
$ this ->cookieManagerMock ->expects ($ this ->exactly ($ numCalls ))
190
203
->method ('setPublicCookie ' )
191
204
->with (
192
- \ Magento \ Persistent \ Model \ Session::COOKIE_NAME ,
205
+ Session::COOKIE_NAME ,
193
206
$ cookieValue ,
194
207
$ cookieMetadataMock
195
208
);
0 commit comments