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;
16 use Symfony\Component\Console\Event\ConsoleEvent;
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Console\ConsoleEvents;
19 use Symfony\Component\Console\Helper\HelperSet;
20 use Symfony\Component\Console\Input\ArgvInput;
21 use Symfony\Component\Console\Output\ConsoleOutput;
22 use Symfony\Component\Debug\ErrorHandler;
23 use Symfony\Component\Debug\ExceptionHandler;
24 use Symfony\Component\EventDispatcher\EventDispatcher;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\HttpKernel\Event\KernelEvent;
27 use Symfony\Component\HttpKernel\EventListener\DebugHandlersListener;
28 use Symfony\Component\HttpKernel\HttpKernelInterface;
29 use Symfony\Component\HttpKernel\KernelEvents;
32 * @author Nicolas Grekas <p@tchwork.com>
34 class DebugHandlersListenerTest extends TestCase
36 public function testConfigure()
38 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
39 $userHandler = function () {};
40 $listener = new DebugHandlersListener($userHandler, $logger);
41 $xHandler = new ExceptionHandler();
42 $eHandler = new ErrorHandler();
43 $eHandler->setExceptionHandler(array($xHandler, 'handle'));
46 set_error_handler(array($eHandler, 'handleError'));
47 set_exception_handler(array($eHandler, 'handleException'));
49 $listener->configure();
50 } catch (\Exception $exception) {
52 restore_exception_handler();
53 restore_error_handler();
55 if (null !== $exception) {
59 $this->assertSame($userHandler, $xHandler->setHandler('var_dump'));
61 $loggers = $eHandler->setLoggers(array());
63 $this->assertArrayHasKey(E_DEPRECATED, $loggers);
64 $this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
67 public function testConfigureForHttpKernelWithNoTerminateWithException()
69 $listener = new DebugHandlersListener(null);
70 $eHandler = new ErrorHandler();
71 $event = new KernelEvent(
72 $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(),
74 HttpKernelInterface::MASTER_REQUEST
78 $h = set_exception_handler(array($eHandler, 'handleException'));
80 $listener->configure($event);
81 } catch (\Exception $exception) {
83 restore_exception_handler();
85 if (null !== $exception) {
89 $this->assertNull($h);
92 public function testConsoleEvent()
94 $dispatcher = new EventDispatcher();
95 $listener = new DebugHandlersListener(null);
96 $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
97 $app->expects($this->once())->method('getHelperSet')->will($this->returnValue(new HelperSet()));
98 $command = new Command(__FUNCTION__);
99 $command->setApplication($app);
100 $event = new ConsoleEvent($command, new ArgvInput(), new ConsoleOutput());
102 $dispatcher->addSubscriber($listener);
105 KernelEvents::REQUEST => array(array($listener, 'configure')),
106 ConsoleEvents::COMMAND => array(array($listener, 'configure')),
108 $this->assertSame($xListeners, $dispatcher->getListeners());
111 $eHandler = new ErrorHandler();
112 set_error_handler(array($eHandler, 'handleError'));
113 set_exception_handler(array($eHandler, 'handleException'));
115 $dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
116 } catch (\Exception $exception) {
118 restore_exception_handler();
119 restore_error_handler();
121 if (null !== $exception) {
125 $xHandler = $eHandler->setExceptionHandler('var_dump');
126 $this->assertInstanceOf('Closure', $xHandler);
128 $app->expects($this->once())
129 ->method('renderException');
131 $xHandler(new \Exception());
134 public function testReplaceExistingExceptionHandler()
136 $userHandler = function () {};
137 $listener = new DebugHandlersListener($userHandler);
138 $eHandler = new ErrorHandler();
139 $eHandler->setExceptionHandler('var_dump');
142 set_exception_handler(array($eHandler, 'handleException'));
144 $listener->configure();
145 } catch (\Exception $exception) {
147 restore_exception_handler();
149 if (null !== $exception) {
153 $this->assertSame($userHandler, $eHandler->setExceptionHandler('var_dump'));