@@ -92,7 +92,7 @@ protected function setUp()
92
92
'pathInfoProcessor ' => $ pathInfoProcessorMock ,
93
93
'objectManager ' => $ objectManagerMock
94
94
])
95
- ->setMethods (['getFrontName ' ])
95
+ ->setMethods (['getFrontName ' , ' isHead ' ])
96
96
->getMock ();
97
97
$ this ->areaListMock = $ this ->getMockBuilder (\Magento \Framework \App \AreaList::class)
98
98
->disableOriginalConstructor ()
@@ -135,12 +135,17 @@ private function setUpLaunch()
135
135
{
136
136
$ frontName = 'frontName ' ;
137
137
$ areaCode = 'areaCode ' ;
138
- $ this ->requestMock ->expects ($ this ->once ())->method ('getFrontName ' )->will ($ this ->returnValue ($ frontName ));
138
+ $ this ->requestMock ->expects ($ this ->once ())
139
+ ->method ('getFrontName ' )
140
+ ->willReturn ($ frontName );
139
141
$ this ->areaListMock ->expects ($ this ->once ())
140
142
->method ('getCodeByFrontName ' )
141
- ->with ($ frontName )->will ($ this ->returnValue ($ areaCode ));
143
+ ->with ($ frontName )
144
+ ->willReturn ($ areaCode );
142
145
$ this ->configLoaderMock ->expects ($ this ->once ())
143
- ->method ('load ' )->with ($ areaCode )->will ($ this ->returnValue ([]));
146
+ ->method ('load ' )
147
+ ->with ($ areaCode )
148
+ ->willReturn ([]);
144
149
$ this ->objectManagerMock ->expects ($ this ->once ())->method ('configure ' )->with ([]);
145
150
$ this ->objectManagerMock ->expects ($ this ->once ())
146
151
->method ('get ' )
@@ -149,12 +154,15 @@ private function setUpLaunch()
149
154
$ this ->frontControllerMock ->expects ($ this ->once ())
150
155
->method ('dispatch ' )
151
156
->with ($ this ->requestMock )
152
- ->will ($ this ->returnValue ( $ this -> responseMock ) );
157
+ ->willReturn ($ this ->responseMock );
153
158
}
154
159
155
160
public function testLaunchSuccess ()
156
161
{
157
162
$ this ->setUpLaunch ();
163
+ $ this ->requestMock ->expects ($ this ->once ())
164
+ ->method ('isHead ' )
165
+ ->willReturn (false );
158
166
$ this ->eventManagerMock ->expects ($ this ->once ())
159
167
->method ('dispatch ' )
160
168
->with (
@@ -171,33 +179,101 @@ public function testLaunchSuccess()
171
179
public function testLaunchException ()
172
180
{
173
181
$ this ->setUpLaunch ();
174
- $ this ->frontControllerMock ->expects ($ this ->once ())->method ('dispatch ' )->with ($ this ->requestMock )->will (
175
- $ this ->returnCallback (
176
- function () {
177
- throw new \Exception ('Message ' );
178
- }
179
- )
180
- );
182
+ $ this ->frontControllerMock ->expects ($ this ->once ())
183
+ ->method ('dispatch ' )
184
+ ->with ($ this ->requestMock )
185
+ ->willThrowException (
186
+ new \Exception ('Message ' )
187
+ );
181
188
$ this ->http ->launch ();
182
189
}
183
190
191
+ /**
192
+ * Test that HEAD requests lead to an empty body and a Content-Length header matching the original body size.
193
+ * @dataProvider dataProviderForTestLaunchHeadRequest
194
+ * @param string $body
195
+ * @param int $expectedLength
196
+ */
197
+ public function testLaunchHeadRequest ($ body , $ expectedLength )
198
+ {
199
+ $ this ->setUpLaunch ();
200
+ $ this ->requestMock ->expects ($ this ->once ())
201
+ ->method ('isHead ' )
202
+ ->willReturn (true );
203
+ $ this ->responseMock ->expects ($ this ->once ())
204
+ ->method ('getHttpResponseCode ' )
205
+ ->willReturn (200 );
206
+ $ this ->responseMock ->expects ($ this ->once ())
207
+ ->method ('getContent ' )
208
+ ->willReturn ($ body );
209
+ $ this ->responseMock ->expects ($ this ->once ())
210
+ ->method ('clearBody ' )
211
+ ->willReturn ($ this ->responseMock );
212
+ $ this ->responseMock ->expects ($ this ->once ())
213
+ ->method ('setHeader ' )
214
+ ->with ('Content-Length ' , $ expectedLength )
215
+ ->willReturn ($ this ->responseMock );
216
+ $ this ->eventManagerMock ->expects ($ this ->once ())
217
+ ->method ('dispatch ' )
218
+ ->with (
219
+ 'controller_front_send_response_before ' ,
220
+ ['request ' => $ this ->requestMock , 'response ' => $ this ->responseMock ]
221
+ );
222
+ $ this ->assertSame ($ this ->responseMock , $ this ->http ->launch ());
223
+ }
224
+
225
+ /**
226
+ * Different test content for responseMock with their expected lengths in bytes.
227
+ * @return array
228
+ */
229
+ public function dataProviderForTestLaunchHeadRequest (): array
230
+ {
231
+ return [
232
+ [
233
+ "<html><head></head><body>Test</body></html> " , // Ascii text
234
+ 43 // Expected Content-Length
235
+ ],
236
+ [
237
+ "<html><head></head><body>部落格</body></html> " , // Multi-byte characters
238
+ 48 // Expected Content-Length
239
+ ],
240
+ [
241
+ "<html><head></head><body> \0</body></html> " , // Null byte
242
+ 40 // Expected Content-Length
243
+ ],
244
+ [
245
+ "<html><head></head>خرید<body></body></html> " , // LTR text
246
+ 47 // Expected Content-Length
247
+ ]
248
+ ];
249
+ }
250
+
184
251
public function testHandleDeveloperModeNotInstalled ()
185
252
{
186
253
$ dir = $ this ->getMockForAbstractClass (\Magento \Framework \Filesystem \Directory \ReadInterface::class);
187
- $ dir ->expects ($ this ->once ())->method ('getAbsolutePath ' )->willReturn (__DIR__ );
254
+ $ dir ->expects ($ this ->once ())
255
+ ->method ('getAbsolutePath ' )
256
+ ->willReturn (__DIR__ );
188
257
$ this ->filesystemMock ->expects ($ this ->once ())
189
258
->method ('getDirectoryRead ' )
190
259
->with (DirectoryList::ROOT )
191
260
->willReturn ($ dir );
192
- $ this ->responseMock ->expects ($ this ->once ())->method ('setRedirect ' )->with ('/_files/ ' );
193
- $ this ->responseMock ->expects ($ this ->once ())->method ('sendHeaders ' );
261
+ $ this ->responseMock ->expects ($ this ->once ())
262
+ ->method ('setRedirect ' )
263
+ ->with ('/_files/ ' );
264
+ $ this ->responseMock ->expects ($ this ->once ())
265
+ ->method ('sendHeaders ' );
194
266
$ bootstrap = $ this ->getBootstrapNotInstalled ();
195
- $ bootstrap ->expects ($ this ->once ())->method ('getParams ' )->willReturn ([
196
- 'SCRIPT_NAME ' => '/index.php ' ,
197
- 'DOCUMENT_ROOT ' => __DIR__ ,
198
- 'SCRIPT_FILENAME ' => __DIR__ . '/index.php ' ,
199
- SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files ' ,
200
- ]);
267
+ $ bootstrap ->expects ($ this ->once ())
268
+ ->method ('getParams ' )
269
+ ->willReturn (
270
+ [
271
+ 'SCRIPT_NAME ' => '/index.php ' ,
272
+ 'DOCUMENT_ROOT ' => __DIR__ ,
273
+ 'SCRIPT_FILENAME ' => __DIR__ . '/index.php ' ,
274
+ SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files ' ,
275
+ ]
276
+ );
201
277
$ this ->assertTrue ($ this ->http ->catchException ($ bootstrap , new \Exception ('Test Message ' )));
202
278
}
203
279
@@ -206,24 +282,37 @@ public function testHandleDeveloperMode()
206
282
$ this ->filesystemMock ->expects ($ this ->once ())
207
283
->method ('getDirectoryRead ' )
208
284
->will ($ this ->throwException (new \Exception ('strange error ' )));
209
- $ this ->responseMock ->expects ($ this ->once ())->method ('setHttpResponseCode ' )->with (500 );
210
- $ this ->responseMock ->expects ($ this ->once ())->method ('setHeader ' )->with ('Content-Type ' , 'text/plain ' );
285
+ $ this ->responseMock ->expects ($ this ->once ())
286
+ ->method ('setHttpResponseCode ' )
287
+ ->with (500 );
288
+ $ this ->responseMock ->expects ($ this ->once ())
289
+ ->method ('setHeader ' )
290
+ ->with ('Content-Type ' , 'text/plain ' );
211
291
$ constraint = new \PHPUnit \Framework \Constraint \StringStartsWith ('1 exception(s): ' );
212
- $ this ->responseMock ->expects ($ this ->once ())->method ('setBody ' )->with ($ constraint );
213
- $ this ->responseMock ->expects ($ this ->once ())->method ('sendResponse ' );
292
+ $ this ->responseMock ->expects ($ this ->once ())
293
+ ->method ('setBody ' )
294
+ ->with ($ constraint );
295
+ $ this ->responseMock ->expects ($ this ->once ())
296
+ ->method ('sendResponse ' );
214
297
$ bootstrap = $ this ->getBootstrapNotInstalled ();
215
- $ bootstrap ->expects ($ this ->once ())->method ('getParams ' )->willReturn (
216
- ['DOCUMENT_ROOT ' => 'something ' , 'SCRIPT_FILENAME ' => 'something/else ' ]
217
- );
298
+ $ bootstrap ->expects ($ this ->once ())
299
+ ->method ('getParams ' )
300
+ ->willReturn (
301
+ ['DOCUMENT_ROOT ' => 'something ' , 'SCRIPT_FILENAME ' => 'something/else ' ]
302
+ );
218
303
$ this ->assertTrue ($ this ->http ->catchException ($ bootstrap , new \Exception ('Test ' )));
219
304
}
220
305
221
306
public function testCatchExceptionSessionException ()
222
307
{
223
- $ this ->responseMock ->expects ($ this ->once ())->method ('setRedirect ' );
224
- $ this ->responseMock ->expects ($ this ->once ())->method ('sendHeaders ' );
308
+ $ this ->responseMock ->expects ($ this ->once ())
309
+ ->method ('setRedirect ' );
310
+ $ this ->responseMock ->expects ($ this ->once ())
311
+ ->method ('sendHeaders ' );
225
312
$ bootstrap = $ this ->createMock (\Magento \Framework \App \Bootstrap::class);
226
- $ bootstrap ->expects ($ this ->once ())->method ('isDeveloperMode ' )->willReturn (false );
313
+ $ bootstrap ->expects ($ this ->once ())
314
+ ->method ('isDeveloperMode ' )
315
+ ->willReturn (false );
227
316
$ this ->assertTrue ($ this ->http ->catchException (
228
317
$ bootstrap ,
229
318
new \Magento \Framework \Exception \SessionException (new \Magento \Framework \Phrase ('Test ' ))
@@ -238,8 +327,12 @@ public function testCatchExceptionSessionException()
238
327
private function getBootstrapNotInstalled ()
239
328
{
240
329
$ bootstrap = $ this ->createMock (\Magento \Framework \App \Bootstrap::class);
241
- $ bootstrap ->expects ($ this ->once ())->method ('isDeveloperMode ' )->willReturn (true );
242
- $ bootstrap ->expects ($ this ->once ())->method ('getErrorCode ' )->willReturn (Bootstrap::ERR_IS_INSTALLED );
330
+ $ bootstrap ->expects ($ this ->once ())
331
+ ->method ('isDeveloperMode ' )
332
+ ->willReturn (true );
333
+ $ bootstrap ->expects ($ this ->once ())
334
+ ->method ('getErrorCode ' )
335
+ ->willReturn (Bootstrap::ERR_IS_INSTALLED );
243
336
return $ bootstrap ;
244
337
}
245
338
}
0 commit comments