1111
1212namespace Symfony \Component \HttpFoundation \Tests ;
1313
14+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
1415use Symfony \Component \HttpFoundation \BinaryFileResponse ;
1516use Symfony \Component \HttpFoundation \File \Stream ;
1617use Symfony \Component \HttpFoundation \Request ;
1920
2021class BinaryFileResponseTest extends ResponseTestCase
2122{
23+ use ExpectDeprecationTrait;
24+
2225 public function testConstruction ()
2326 {
2427 $ file = __DIR__ .'/../README.md ' ;
@@ -29,6 +32,26 @@ public function testConstruction()
2932 $ this ->assertTrue ($ response ->headers ->has ('Last-Modified ' ));
3033 $ this ->assertFalse ($ response ->headers ->has ('Content-Disposition ' ));
3134
35+ $ response = new BinaryFileResponse ($ file , 404 , [], true , ResponseHeaderBag::DISPOSITION_INLINE );
36+ $ this ->assertEquals (404 , $ response ->getStatusCode ());
37+ $ this ->assertFalse ($ response ->headers ->has ('ETag ' ));
38+ $ this ->assertEquals ('inline; filename=README.md ' , $ response ->headers ->get ('Content-Disposition ' ));
39+ }
40+
41+ /**
42+ * @group legacy
43+ */
44+ public function testConstructionLegacy ()
45+ {
46+ $ file = __DIR__ .'/../README.md ' ;
47+ $ this ->expectDeprecation ('Since symfony/http-foundation 5.2: The "Symfony\Component\HttpFoundation\BinaryFileResponse::create()" method is deprecated, use "new Symfony\Component\HttpFoundation\BinaryFileResponse()" instead. ' );
48+ $ response = BinaryFileResponse::create ($ file , 404 , ['X-Header ' => 'Foo ' ], true , null , true , true );
49+ $ this ->assertEquals (404 , $ response ->getStatusCode ());
50+ $ this ->assertEquals ('Foo ' , $ response ->headers ->get ('X-Header ' ));
51+ $ this ->assertTrue ($ response ->headers ->has ('ETag ' ));
52+ $ this ->assertTrue ($ response ->headers ->has ('Last-Modified ' ));
53+ $ this ->assertFalse ($ response ->headers ->has ('Content-Disposition ' ));
54+
3255 $ response = BinaryFileResponse::create ($ file , 404 , [], true , ResponseHeaderBag::DISPOSITION_INLINE );
3356 $ this ->assertEquals (404 , $ response ->getStatusCode ());
3457 $ this ->assertFalse ($ response ->headers ->has ('ETag ' ));
@@ -83,7 +106,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
83106 */
84107 public function testRequests ($ requestRange , $ offset , $ length , $ responseRange )
85108 {
86- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
109+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
87110
88111 // do a request to get the ETag
89112 $ request = Request::create ('/ ' );
@@ -115,7 +138,7 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
115138 */
116139 public function testRequestsWithoutEtag ($ requestRange , $ offset , $ length , $ responseRange )
117140 {
118- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
141+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
119142
120143 // do a request to get the LastModified
121144 $ request = Request::create ('/ ' );
@@ -156,7 +179,7 @@ public function provideRanges()
156179 public function testRangeRequestsWithoutLastModifiedDate ()
157180 {
158181 // prevent auto last modified
159- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ], true , null , false , false );
182+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ], true , null , false , false );
160183
161184 // prepare a request for a range of the testing file
162185 $ request = Request::create ('/ ' );
@@ -177,7 +200,7 @@ public function testRangeRequestsWithoutLastModifiedDate()
177200 */
178201 public function testFullFileRequests ($ requestRange )
179202 {
180- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
203+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
181204
182205 // prepare a request for a range of the testing file
183206 $ request = Request::create ('/ ' );
@@ -213,7 +236,7 @@ public function testRangeOnPostMethod()
213236 {
214237 $ request = Request::create ('/ ' , 'POST ' );
215238 $ request ->headers ->set ('Range ' , 'bytes=10-20 ' );
216- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
239+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
217240
218241 $ file = fopen (__DIR__ .'/File/Fixtures/test.gif ' , 'r ' );
219242 $ data = fread ($ file , 35 );
@@ -231,7 +254,7 @@ public function testRangeOnPostMethod()
231254
232255 public function testUnpreparedResponseSendsFullFile ()
233256 {
234- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 );
257+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 );
235258
236259 $ data = file_get_contents (__DIR__ .'/File/Fixtures/test.gif ' );
237260
@@ -247,7 +270,7 @@ public function testUnpreparedResponseSendsFullFile()
247270 */
248271 public function testInvalidRequests ($ requestRange )
249272 {
250- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
273+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
251274
252275 // prepare a request for a range of the testing file
253276 $ request = Request::create ('/ ' );
@@ -278,7 +301,7 @@ public function testXSendfile($file)
278301 $ request ->headers ->set ('X-Sendfile-Type ' , 'X-Sendfile ' );
279302
280303 BinaryFileResponse::trustXSendfileTypeHeader ();
281- $ response = BinaryFileResponse:: create ($ file , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
304+ $ response = new BinaryFileResponse ($ file , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
282305 $ response ->prepare ($ request );
283306
284307 $ this ->expectOutputString ('' );
@@ -338,7 +361,7 @@ public function testDeleteFileAfterSend()
338361 public function testAcceptRangeOnUnsafeMethods ()
339362 {
340363 $ request = Request::create ('/ ' , 'POST ' );
341- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
364+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
342365 $ response ->prepare ($ request );
343366
344367 $ this ->assertEquals ('none ' , $ response ->headers ->get ('Accept-Ranges ' ));
@@ -347,7 +370,7 @@ public function testAcceptRangeOnUnsafeMethods()
347370 public function testAcceptRangeNotOverriden ()
348371 {
349372 $ request = Request::create ('/ ' , 'POST ' );
350- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
373+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
351374 $ response ->headers ->set ('Accept-Ranges ' , 'foo ' );
352375 $ response ->prepare ($ request );
353376
0 commit comments