4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
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;
24 * ExceptionListenerTest.
26 * @author Robert Schönthal <seroscho@googlemail.com>
28 * @group time-sensitive
30 class ExceptionListenerTest extends TestCase
32 public function testConstruct()
34 $logger = new TestLogger();
35 $l = new ExceptionListener('foo', $logger);
37 $_logger = new \ReflectionProperty(get_class($l), 'logger');
38 $_logger->setAccessible(true);
39 $_controller = new \ReflectionProperty(get_class($l), 'controller');
40 $_controller->setAccessible(true);
42 $this->assertSame($logger, $_logger->getValue($l));
43 $this->assertSame('foo', $_controller->getValue($l));
47 * @dataProvider provider
49 public function testHandleWithoutLogger($event, $event2)
51 $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
53 $l = new ExceptionListener('foo');
54 $l->onKernelException($event);
56 $this->assertEquals(new Response('foo'), $event->getResponse());
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());
68 * @dataProvider provider
70 public function testHandleWithLogger($event, $event2)
72 $logger = new TestLogger();
74 $l = new ExceptionListener('foo', $logger);
75 $l->onKernelException($event);
77 $this->assertEquals(new Response('foo'), $event->getResponse());
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());
87 $this->assertEquals(3, $logger->countErrors());
88 $this->assertCount(3, $logger->getLogs('critical'));
91 public function provider()
93 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
94 return array(array(null, null));
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);
103 array($event, $event2),
107 public function testSubRequestFormat()
109 $listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock());
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());
116 $request = Request::create('/');
117 $request->setRequestFormat('xml');
119 $event = new GetResponseForExceptionEvent($kernel, $request, 'foo', new \Exception('foo'));
120 $listener->onKernelException($event);
122 $response = $event->getResponse();
123 $this->assertEquals('xml', $response->getContent());
127 class TestLogger extends Logger implements DebugLoggerInterface
129 public function countErrors()
131 return count($this->logs['critical']);
135 class TestKernel implements HttpKernelInterface
137 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
139 return new Response('foo');
143 class TestKernelThatThrowsException implements HttpKernelInterface
145 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
147 throw new \RuntimeException('bar');