Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / ExceptionListenerTest.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\HttpKernelInterface;
16 use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
17 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
18 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpKernel\Tests\Logger;
22
23 /**
24  * ExceptionListenerTest.
25  *
26  * @author Robert Schönthal <seroscho@googlemail.com>
27  *
28  * @group time-sensitive
29  */
30 class ExceptionListenerTest extends TestCase
31 {
32     public function testConstruct()
33     {
34         $logger = new TestLogger();
35         $l = new ExceptionListener('foo', $logger);
36
37         $_logger = new \ReflectionProperty(get_class($l), 'logger');
38         $_logger->setAccessible(true);
39         $_controller = new \ReflectionProperty(get_class($l), 'controller');
40         $_controller->setAccessible(true);
41
42         $this->assertSame($logger, $_logger->getValue($l));
43         $this->assertSame('foo', $_controller->getValue($l));
44     }
45
46     /**
47      * @dataProvider provider
48      */
49     public function testHandleWithoutLogger($event, $event2)
50     {
51         $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
52
53         $l = new ExceptionListener('foo');
54         $l->onKernelException($event);
55
56         $this->assertEquals(new Response('foo'), $event->getResponse());
57
58         try {
59             $l->onKernelException($event2);
60             $this->fail('RuntimeException expected');
61         } catch (\RuntimeException $e) {
62             $this->assertSame('bar', $e->getMessage());
63             $this->assertSame('foo', $e->getPrevious()->getMessage());
64         }
65     }
66
67     /**
68      * @dataProvider provider
69      */
70     public function testHandleWithLogger($event, $event2)
71     {
72         $logger = new TestLogger();
73
74         $l = new ExceptionListener('foo', $logger);
75         $l->onKernelException($event);
76
77         $this->assertEquals(new Response('foo'), $event->getResponse());
78
79         try {
80             $l->onKernelException($event2);
81             $this->fail('RuntimeException expected');
82         } catch (\RuntimeException $e) {
83             $this->assertSame('bar', $e->getMessage());
84             $this->assertSame('foo', $e->getPrevious()->getMessage());
85         }
86
87         $this->assertEquals(3, $logger->countErrors());
88         $this->assertCount(3, $logger->getLogs('critical'));
89     }
90
91     public function provider()
92     {
93         if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
94             return array(array(null, null));
95         }
96
97         $request = new Request();
98         $exception = new \Exception('foo');
99         $event = new GetResponseForExceptionEvent(new TestKernel(), $request, 'foo', $exception);
100         $event2 = new GetResponseForExceptionEvent(new TestKernelThatThrowsException(), $request, 'foo', $exception);
101
102         return array(
103             array($event, $event2),
104         );
105     }
106
107     public function testSubRequestFormat()
108     {
109         $listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock());
110
111         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
112         $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
113             return new Response($request->getRequestFormat());
114         }));
115
116         $request = Request::create('/');
117         $request->setRequestFormat('xml');
118
119         $event = new GetResponseForExceptionEvent($kernel, $request, 'foo', new \Exception('foo'));
120         $listener->onKernelException($event);
121
122         $response = $event->getResponse();
123         $this->assertEquals('xml', $response->getContent());
124     }
125 }
126
127 class TestLogger extends Logger implements DebugLoggerInterface
128 {
129     public function countErrors()
130     {
131         return count($this->logs['critical']);
132     }
133 }
134
135 class TestKernel implements HttpKernelInterface
136 {
137     public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
138     {
139         return new Response('foo');
140     }
141 }
142
143 class TestKernelThatThrowsException implements HttpKernelInterface
144 {
145     public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
146     {
147         throw new \RuntimeException('bar');
148     }
149 }