1
1
<?php
2
2
/**
3
- * Test for \Magento\Checkout\Controller\Index\Index
4
- *
5
3
* Copyright © Magento, Inc. All rights reserved.
6
4
* See COPYING.txt for license details.
7
5
*/
6
+ declare (strict_types=1 );
8
7
9
8
namespace Magento \Checkout \Test \Unit \Controller \Index ;
10
9
11
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManager ;
10
+ use Magento \Customer \Model \Session ;
11
+ use Magento \Framework \App \Request \Http ;
12
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13
+ use PHPUnit_Framework_MockObject_Builder_InvocationMocker as InvocationMocker ;
14
+ use PHPUnit_Framework_MockObject_Matcher_InvokedCount as InvokedCount ;
15
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
16
+ use Magento \Checkout \Helper \Data ;
17
+ use Magento \Quote \Model \Quote ;
18
+ use Magento \Framework \View \Result \Page ;
19
+ use Magento \Checkout \Controller \Index \Index ;
20
+ use Magento \Framework \ObjectManagerInterface ;
12
21
13
22
/**
14
23
* @SuppressWarnings(PHPMD.TooManyFields)
17
26
class IndexTest extends \PHPUnit \Framework \TestCase
18
27
{
19
28
/**
20
- * @var \Magento\Framework\TestFramework\Unit\Helper\ ObjectManager
29
+ * @var ObjectManager
21
30
*/
22
31
private $ objectManager ;
23
32
24
33
/**
25
- * @var \PHPUnit_Framework_MockObject_MockObject
34
+ * @var MockObject
26
35
*/
27
36
private $ objectManagerMock ;
28
37
29
38
/**
30
- * @var \PHPUnit_Framework_MockObject_MockObject
39
+ * @var Data|MockObject
31
40
*/
32
- private $ dataMock ;
41
+ private $ data ;
33
42
34
43
/**
35
- * @var \PHPUnit_Framework_MockObject_MockObject
44
+ * @var MockObject
36
45
*/
37
- private $ quoteMock ;
46
+ private $ quote ;
38
47
39
48
/**
40
- * @var \PHPUnit_Framework_MockObject_MockObject
49
+ * @var MockObject
41
50
*/
42
51
private $ contextMock ;
43
52
44
53
/**
45
- * @var \PHPUnit_Framework_MockObject_MockObject
54
+ * @var Session|MockObject
46
55
*/
47
- private $ sessionMock ;
56
+ private $ session ;
48
57
49
58
/**
50
- * @var \PHPUnit_Framework_MockObject_MockObject
59
+ * @var MockObject
51
60
*/
52
61
private $ onepageMock ;
53
62
54
63
/**
55
- * @var \PHPUnit_Framework_MockObject_MockObject
64
+ * @var MockObject
56
65
*/
57
66
private $ layoutMock ;
58
67
59
68
/**
60
- * @var \PHPUnit_Framework_MockObject_MockObject
69
+ * @var Http|MockObject
61
70
*/
62
- private $ requestMock ;
71
+ private $ request ;
63
72
64
73
/**
65
- * @var \PHPUnit_Framework_MockObject_MockObject
74
+ * @var MockObject
66
75
*/
67
76
private $ responseMock ;
68
77
69
78
/**
70
- * @var \PHPUnit_Framework_MockObject_MockObject
79
+ * @var MockObject
71
80
*/
72
81
private $ redirectMock ;
73
82
74
83
/**
75
- * @var \Magento\Checkout\Controller\Index\ Index
84
+ * @var Index
76
85
*/
77
86
private $ model ;
78
87
79
88
/**
80
- * @var \Magento\Framework\View\Result\ Page|\PHPUnit_Framework_MockObject_MockObject
89
+ * @var Page|MockObject
81
90
*/
82
- protected $ resultPageMock ;
91
+ private $ resultPage ;
83
92
84
93
/**
85
94
* @var \Magento\Framework\View\Page\Config
86
95
*/
87
- protected $ pageConfigMock ;
96
+ private $ pageConfigMock ;
88
97
89
98
/**
90
99
* @var \Magento\Framework\View\Page\Title
91
100
*/
92
- protected $ titleMock ;
101
+ private $ titleMock ;
93
102
94
103
/**
95
104
* @var \Magento\Framework\UrlInterface
96
105
*/
97
- protected $ url ;
106
+ private $ url ;
98
107
99
108
/**
100
- * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
109
+ * @var \Magento\Framework\Controller\Result\Redirect|MockObject
101
110
*/
102
- protected $ resultRedirectMock ;
111
+ private $ resultRedirectMock ;
103
112
104
113
protected function setUp ()
105
114
{
106
115
// mock objects
107
116
$ this ->objectManager = new ObjectManager ($ this );
108
- $ this ->objectManagerMock = $ this ->basicMock (\ Magento \ Framework \ ObjectManagerInterface::class);
109
- $ this ->dataMock = $ this ->basicMock (\ Magento \ Checkout \ Helper \ Data::class);
110
- $ this ->quoteMock = $ this ->createPartialMock (
111
- \ Magento \ Quote \ Model \ Quote::class,
117
+ $ this ->objectManagerMock = $ this ->basicMock (ObjectManagerInterface::class);
118
+ $ this ->data = $ this ->basicMock (Data::class);
119
+ $ this ->quote = $ this ->createPartialMock (
120
+ Quote::class,
112
121
['getHasError ' , 'hasItems ' , 'validateMinimumAmount ' , 'hasError ' ]
113
122
);
114
123
$ this ->contextMock = $ this ->basicMock (\Magento \Framework \App \Action \Context::class);
115
- $ this ->sessionMock = $ this ->basicMock (\ Magento \ Customer \ Model \ Session::class);
124
+ $ this ->session = $ this ->basicMock (Session::class);
116
125
$ this ->onepageMock = $ this ->basicMock (\Magento \Checkout \Model \Type \Onepage::class);
117
126
$ this ->layoutMock = $ this ->basicMock (\Magento \Framework \View \Layout::class);
118
- $ this ->requestMock = $ this ->basicMock (\Magento \Framework \App \RequestInterface::class);
127
+ $ this ->request = $ this ->getMockBuilder (Http::class)
128
+ ->disableOriginalConstructor ()
129
+ ->setMethods (['isSecure ' , 'getHeader ' ])
130
+ ->getMock ();
119
131
$ this ->responseMock = $ this ->basicMock (\Magento \Framework \App \ResponseInterface::class);
120
132
$ this ->redirectMock = $ this ->basicMock (\Magento \Framework \App \Response \RedirectInterface::class);
121
- $ this ->resultPageMock = $ this ->basicMock (\ Magento \ Framework \ View \ Result \ Page::class);
133
+ $ this ->resultPage = $ this ->basicMock (Page::class);
122
134
$ this ->pageConfigMock = $ this ->basicMock (\Magento \Framework \View \Page \Config::class);
123
135
$ this ->titleMock = $ this ->basicMock (\Magento \Framework \View \Page \Title::class);
124
136
$ this ->url = $ this ->createMock (\Magento \Framework \UrlInterface::class);
@@ -130,7 +142,7 @@ protected function setUp()
130
142
->getMock ();
131
143
$ resultPageFactoryMock ->expects ($ this ->any ())
132
144
->method ('create ' )
133
- ->willReturn ($ this ->resultPageMock );
145
+ ->willReturn ($ this ->resultPage );
134
146
135
147
$ resultRedirectFactoryMock = $ this ->getMockBuilder (\Magento \Framework \Controller \Result \RedirectFactory::class)
136
148
->disableOriginalConstructor ()
@@ -141,21 +153,21 @@ protected function setUp()
141
153
->willReturn ($ this ->resultRedirectMock );
142
154
143
155
// stubs
144
- $ this ->basicStub ($ this ->onepageMock , 'getQuote ' )->willReturn ($ this ->quoteMock );
145
- $ this ->basicStub ($ this ->resultPageMock , 'getLayout ' )->willReturn ($ this ->layoutMock );
156
+ $ this ->basicStub ($ this ->onepageMock , 'getQuote ' )->willReturn ($ this ->quote );
157
+ $ this ->basicStub ($ this ->resultPage , 'getLayout ' )->willReturn ($ this ->layoutMock );
146
158
147
159
$ this ->basicStub ($ this ->layoutMock , 'getBlock ' )
148
160
->willReturn ($ this ->basicMock (\Magento \Theme \Block \Html \Header::class));
149
- $ this ->basicStub ($ this ->resultPageMock , 'getConfig ' )->willReturn ($ this ->pageConfigMock );
161
+ $ this ->basicStub ($ this ->resultPage , 'getConfig ' )->willReturn ($ this ->pageConfigMock );
150
162
$ this ->basicStub ($ this ->pageConfigMock , 'getTitle ' )->willReturn ($ this ->titleMock );
151
163
$ this ->basicStub ($ this ->titleMock , 'set ' )->willReturn ($ this ->titleMock );
152
164
153
165
// objectManagerMock
154
166
$ objectManagerReturns = [
155
- [\ Magento \ Checkout \ Helper \ Data::class, $ this ->dataMock ],
167
+ [Data::class, $ this ->data ],
156
168
[\Magento \Checkout \Model \Type \Onepage::class, $ this ->onepageMock ],
157
169
[\Magento \Checkout \Model \Session::class, $ this ->basicMock (\Magento \Checkout \Model \Session::class)],
158
- [\ Magento \ Customer \ Model \ Session::class, $ this ->basicMock (\ Magento \ Customer \ Model \ Session::class)],
170
+ [Session::class, $ this ->basicMock (Session::class)],
159
171
160
172
];
161
173
$ this ->objectManagerMock ->expects ($ this ->any ())
@@ -165,7 +177,7 @@ protected function setUp()
165
177
->willReturn ($ this ->basicMock (\Magento \Framework \UrlInterface::class));
166
178
// context stubs
167
179
$ this ->basicStub ($ this ->contextMock , 'getObjectManager ' )->willReturn ($ this ->objectManagerMock );
168
- $ this ->basicStub ($ this ->contextMock , 'getRequest ' )->willReturn ($ this ->requestMock );
180
+ $ this ->basicStub ($ this ->contextMock , 'getRequest ' )->willReturn ($ this ->request );
169
181
$ this ->basicStub ($ this ->contextMock , 'getResponse ' )->willReturn ($ this ->responseMock );
170
182
$ this ->basicStub ($ this ->contextMock , 'getMessageManager ' )
171
183
->willReturn ($ this ->basicMock (\Magento \Framework \Message \ManagerInterface::class));
@@ -175,33 +187,82 @@ protected function setUp()
175
187
176
188
// SUT
177
189
$ this ->model = $ this ->objectManager ->getObject (
178
- \ Magento \ Checkout \ Controller \ Index \ Index::class,
190
+ Index::class,
179
191
[
180
192
'context ' => $ this ->contextMock ,
181
- 'customerSession ' => $ this ->sessionMock ,
193
+ 'customerSession ' => $ this ->session ,
182
194
'resultPageFactory ' => $ resultPageFactoryMock ,
183
195
'resultRedirectFactory ' => $ resultRedirectFactoryMock
184
196
]
185
197
);
186
198
}
187
199
188
- public function testRegenerateSessionIdOnExecute ()
200
+ /**
201
+ * Checks a case when session should be or not regenerated during the request.
202
+ *
203
+ * @param bool $secure
204
+ * @param string $referer
205
+ * @param InvokedCount $expectedCall
206
+ * @dataProvider sessionRegenerationDataProvider
207
+ */
208
+ public function testRegenerateSessionIdOnExecute (bool $ secure , string $ referer , InvokedCount $ expectedCall )
209
+ {
210
+ $ this ->data ->method ('canOnepageCheckout ' )
211
+ ->willReturn (true );
212
+ $ this ->quote ->method ('hasItems ' )
213
+ ->willReturn (true );
214
+ $ this ->quote ->method ('getHasError ' )
215
+ ->willReturn (false );
216
+ $ this ->quote ->method ('validateMinimumAmount ' )
217
+ ->willReturn (true );
218
+ $ this ->session ->method ('isLoggedIn ' )
219
+ ->willReturn (true );
220
+ $ this ->request ->method ('isSecure ' )
221
+ ->willReturn ($ secure );
222
+ $ this ->request ->method ('getHeader ' )
223
+ ->with ('referer ' )
224
+ ->willReturn ($ referer );
225
+
226
+ $ this ->session ->expects ($ expectedCall )
227
+ ->method ('regenerateId ' );
228
+ $ this ->assertSame ($ this ->resultPage , $ this ->model ->execute ());
229
+ }
230
+
231
+ /**
232
+ * Gets list of variations for generating new session.
233
+ *
234
+ * @return array
235
+ */
236
+ public function sessionRegenerationDataProvider (): array
189
237
{
190
- //Stubs to control execution flow
191
- $ this ->basicStub ($ this ->dataMock , 'canOnepageCheckout ' )->willReturn (true );
192
- $ this ->basicStub ($ this ->quoteMock , 'hasItems ' )->willReturn (true );
193
- $ this ->basicStub ($ this ->quoteMock , 'getHasError ' )->willReturn (false );
194
- $ this ->basicStub ($ this ->quoteMock , 'validateMinimumAmount ' )->willReturn (true );
195
- $ this ->basicStub ($ this ->sessionMock , 'isLoggedIn ' )->willReturn (true );
196
-
197
- //Expected outcomes
198
- $ this ->sessionMock ->expects ($ this ->once ())->method ('regenerateId ' );
199
- $ this ->assertSame ($ this ->resultPageMock , $ this ->model ->execute ());
238
+ return [
239
+ [
240
+ 'secure ' => false ,
241
+ 'referer ' => 'https://test.domain.com/ ' ,
242
+ 'expectedCall ' => self ::once ()
243
+ ],
244
+ [
245
+ 'secure ' => true ,
246
+ 'referer ' => false ,
247
+ 'expectedCall ' => self ::once ()
248
+ ],
249
+ [
250
+ 'secure ' => true ,
251
+ 'referer ' => 'http://test.domain.com/ ' ,
252
+ 'expectedCall ' => self ::once ()
253
+ ],
254
+ // This is the only case in which session regeneration can be skipped
255
+ [
256
+ 'secure ' => true ,
257
+ 'referer ' => 'https://test.domain.com/ ' ,
258
+ 'expectedCall ' => self ::never ()
259
+ ],
260
+ ];
200
261
}
201
262
202
263
public function testOnepageCheckoutNotAvailable ()
203
264
{
204
- $ this ->basicStub ($ this ->dataMock , 'canOnepageCheckout ' )->willReturn (false );
265
+ $ this ->basicStub ($ this ->data , 'canOnepageCheckout ' )->willReturn (false );
205
266
$ expectedPath = 'checkout/cart ' ;
206
267
207
268
$ this ->resultRedirectMock ->expects ($ this ->once ())
@@ -214,7 +275,7 @@ public function testOnepageCheckoutNotAvailable()
214
275
215
276
public function testInvalidQuote ()
216
277
{
217
- $ this ->basicStub ($ this ->quoteMock , 'hasError ' )->willReturn (true );
278
+ $ this ->basicStub ($ this ->quote , 'hasError ' )->willReturn (true );
218
279
219
280
$ expectedPath = 'checkout/cart ' ;
220
281
$ this ->resultRedirectMock ->expects ($ this ->once ())
@@ -226,23 +287,22 @@ public function testInvalidQuote()
226
287
}
227
288
228
289
/**
229
- * @param \PHPUnit_Framework_MockObject_MockObject $mock
290
+ * @param MockObject $mock
230
291
* @param string $method
231
292
*
232
- * @return \PHPUnit\Framework\MockObject_Builder_InvocationMocker
293
+ * @return InvocationMocker
233
294
*/
234
- private function basicStub ($ mock , $ method )
295
+ private function basicStub ($ mock , $ method ): InvocationMocker
235
296
{
236
- return $ mock ->expects ($ this ->any ())
237
- ->method ($ method )
238
- ->withAnyParameters ();
297
+ return $ mock ->method ($ method )
298
+ ->withAnyParameters ();
239
299
}
240
300
241
301
/**
242
302
* @param string $className
243
- * @return \PHPUnit_Framework_MockObject_MockObject
303
+ * @return MockObject
244
304
*/
245
- private function basicMock ($ className )
305
+ private function basicMock (string $ className ): MockObject
246
306
{
247
307
return $ this ->getMockBuilder ($ className )
248
308
->disableOriginalConstructor ()
0 commit comments