Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / ResponseListenerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\EventListener\ResponseListener;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 use Symfony\Component\HttpKernel\HttpKernelInterface;
19 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
20 use Symfony\Component\HttpKernel\KernelEvents;
21 use Symfony\Component\EventDispatcher\EventDispatcher;
22
23 class ResponseListenerTest extends TestCase
24 {
25     private $dispatcher;
26
27     private $kernel;
28
29     protected function setUp()
30     {
31         $this->dispatcher = new EventDispatcher();
32         $listener = new ResponseListener('UTF-8');
33         $this->dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'));
34
35         $this->kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
36     }
37
38     protected function tearDown()
39     {
40         $this->dispatcher = null;
41         $this->kernel = null;
42     }
43
44     public function testFilterDoesNothingForSubRequests()
45     {
46         $response = new Response('foo');
47
48         $event = new FilterResponseEvent($this->kernel, new Request(), HttpKernelInterface::SUB_REQUEST, $response);
49         $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
50
51         $this->assertEquals('', $event->getResponse()->headers->get('content-type'));
52     }
53
54     public function testFilterSetsNonDefaultCharsetIfNotOverridden()
55     {
56         $listener = new ResponseListener('ISO-8859-15');
57         $this->dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'), 1);
58
59         $response = new Response('foo');
60
61         $event = new FilterResponseEvent($this->kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $response);
62         $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
63
64         $this->assertEquals('ISO-8859-15', $response->getCharset());
65     }
66
67     public function testFilterDoesNothingIfCharsetIsOverridden()
68     {
69         $listener = new ResponseListener('ISO-8859-15');
70         $this->dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'), 1);
71
72         $response = new Response('foo');
73         $response->setCharset('ISO-8859-1');
74
75         $event = new FilterResponseEvent($this->kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $response);
76         $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
77
78         $this->assertEquals('ISO-8859-1', $response->getCharset());
79     }
80
81     public function testFiltersSetsNonDefaultCharsetIfNotOverriddenOnNonTextContentType()
82     {
83         $listener = new ResponseListener('ISO-8859-15');
84         $this->dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'), 1);
85
86         $response = new Response('foo');
87         $request = Request::create('/');
88         $request->setRequestFormat('application/json');
89
90         $event = new FilterResponseEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
91         $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
92
93         $this->assertEquals('ISO-8859-15', $response->getCharset());
94     }
95 }