|
14 | 14 | use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
15 | 15 | use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
16 | 16 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
| 17 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 18 | +use Symfony\Component\HttpFoundation\File\File; |
17 | 19 | use Symfony\Component\HttpFoundation\JsonResponse;
|
18 | 20 | use Symfony\Component\HttpFoundation\Request;
|
19 | 21 | use Symfony\Component\HttpFoundation\RequestStack;
|
20 | 22 | use Symfony\Component\HttpFoundation\Response;
|
| 23 | +use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
21 | 24 | use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
22 | 25 | use Symfony\Component\HttpFoundation\StreamedResponse;
|
23 | 26 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
@@ -209,6 +212,126 @@ public function testJsonWithSerializerContextOverride()
|
209 | 212 | $this->assertEquals('{}', $response->getContent());
|
210 | 213 | }
|
211 | 214 |
|
| 215 | + public function testFile() |
| 216 | + { |
| 217 | + /* @var ContainerInterface $container */ |
| 218 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 219 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 220 | + $container->set('kernel', $kernel); |
| 221 | + |
| 222 | + $controller = new TestController(); |
| 223 | + $controller->setContainer($container); |
| 224 | + |
| 225 | + /* @var BinaryFileResponse $response */ |
| 226 | + $response = $controller->file(new File(__FILE__)); |
| 227 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 228 | + $this->assertSame(200, $response->getStatusCode()); |
| 229 | + if ($response->headers->get('content-type')) { |
| 230 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 231 | + } |
| 232 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition')); |
| 233 | + $this->assertContains(basename(__FILE__), $response->headers->get('content-disposition')); |
| 234 | + } |
| 235 | + |
| 236 | + public function testFileAsInline() |
| 237 | + { |
| 238 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 239 | + $controller = new TestController(); |
| 240 | + $controller->setContainer($container); |
| 241 | + |
| 242 | + /* @var BinaryFileResponse $response */ |
| 243 | + $response = $controller->file(new File(__FILE__), null, ResponseHeaderBag::DISPOSITION_INLINE); |
| 244 | + |
| 245 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 246 | + $this->assertSame(200, $response->getStatusCode()); |
| 247 | + if ($response->headers->get('content-type')) { |
| 248 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 249 | + } |
| 250 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition')); |
| 251 | + $this->assertContains(basename(__FILE__), $response->headers->get('content-disposition')); |
| 252 | + } |
| 253 | + |
| 254 | + public function testFileWithOwnFileName() |
| 255 | + { |
| 256 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 257 | + $controller = new TestController(); |
| 258 | + $controller->setContainer($container); |
| 259 | + |
| 260 | + /* @var BinaryFileResponse $response */ |
| 261 | + $fileName = 'test.php'; |
| 262 | + $response = $controller->file(new File(__FILE__), $fileName); |
| 263 | + |
| 264 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 265 | + $this->assertSame(200, $response->getStatusCode()); |
| 266 | + if ($response->headers->get('content-type')) { |
| 267 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 268 | + } |
| 269 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition')); |
| 270 | + $this->assertContains($fileName, $response->headers->get('content-disposition')); |
| 271 | + } |
| 272 | + |
| 273 | + public function testFileWithOwnFileNameAsInline() |
| 274 | + { |
| 275 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 276 | + $controller = new TestController(); |
| 277 | + $controller->setContainer($container); |
| 278 | + |
| 279 | + /* @var BinaryFileResponse $response */ |
| 280 | + $fileName = 'test.php'; |
| 281 | + $response = $controller->file(new File(__FILE__), $fileName, ResponseHeaderBag::DISPOSITION_INLINE); |
| 282 | + |
| 283 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 284 | + $this->assertSame(200, $response->getStatusCode()); |
| 285 | + if ($response->headers->get('content-type')) { |
| 286 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 287 | + } |
| 288 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition')); |
| 289 | + $this->assertContains($fileName, $response->headers->get('content-disposition')); |
| 290 | + } |
| 291 | + |
| 292 | + public function testFileFromPath() |
| 293 | + { |
| 294 | + $controller = new TestController(); |
| 295 | + |
| 296 | + /* @var BinaryFileResponse $response */ |
| 297 | + $response = $controller->file(__FILE__); |
| 298 | + |
| 299 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 300 | + $this->assertSame(200, $response->getStatusCode()); |
| 301 | + if ($response->headers->get('content-type')) { |
| 302 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 303 | + } |
| 304 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition')); |
| 305 | + $this->assertContains(basename(__FILE__), $response->headers->get('content-disposition')); |
| 306 | + } |
| 307 | + |
| 308 | + public function testFileFromPathWithCustomizedFileName() |
| 309 | + { |
| 310 | + $controller = new TestController(); |
| 311 | + |
| 312 | + /* @var BinaryFileResponse $response */ |
| 313 | + $response = $controller->file(__FILE__, 'test.php'); |
| 314 | + |
| 315 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 316 | + $this->assertSame(200, $response->getStatusCode()); |
| 317 | + if ($response->headers->get('content-type')) { |
| 318 | + $this->assertSame('text/x-php', $response->headers->get('content-type')); |
| 319 | + } |
| 320 | + $this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition')); |
| 321 | + $this->assertContains('test.php', $response->headers->get('content-disposition')); |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException |
| 326 | + */ |
| 327 | + public function testFileWhichDoesNotExist() |
| 328 | + { |
| 329 | + $controller = new TestController(); |
| 330 | + |
| 331 | + /* @var BinaryFileResponse $response */ |
| 332 | + $response = $controller->file('some-file.txt', 'test.php'); |
| 333 | + } |
| 334 | + |
212 | 335 | public function testIsGranted()
|
213 | 336 | {
|
214 | 337 | $authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
|
@@ -494,6 +617,11 @@ public function json($data, $status = 200, $headers = array(), $context = array(
|
494 | 617 | return parent::json($data, $status, $headers, $context);
|
495 | 618 | }
|
496 | 619 |
|
| 620 | + public function file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT) |
| 621 | + { |
| 622 | + return parent::file($file, $fileName, $disposition); |
| 623 | + } |
| 624 | + |
497 | 625 | public function isGranted($attributes, $object = null)
|
498 | 626 | {
|
499 | 627 | return parent::isGranted($attributes, $object);
|
|
0 commit comments