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 * DebugHandlersListenerTest.
34 * @author Nicolas Grekas <p@tchwork.com>
36 class DebugHandlersListenerTest extends TestCase
38 public function testConfigure()
40 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
41 $userHandler = function () {};
42 $listener = new DebugHandlersListener($userHandler, $logger);
43 $xHandler = new ExceptionHandler();
44 $eHandler = new ErrorHandler();
45 $eHandler->setExceptionHandler(array($xHandler, 'handle'));
48 set_error_handler(array($eHandler, 'handleError'));
49 set_exception_handler(array($eHandler, 'handleException'));
51 $listener->configure();
52 } catch (\Exception $exception) {
54 restore_exception_handler();
55 restore_error_handler();
57 if (null !== $exception) {
61 $this->assertSame($userHandler, $xHandler->setHandler('var_dump'));
63 $loggers = $eHandler->setLoggers(array());
65 $this->assertArrayHasKey(E_DEPRECATED, $loggers);
66 $this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
69 public function testConfigureForHttpKernelWithNoTerminateWithException()
71 $listener = new DebugHandlersListener(null);
72 $eHandler = new ErrorHandler();
73 $event = new KernelEvent(
74 $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(),
76 HttpKernelInterface::MASTER_REQUEST
80 $h = set_exception_handler(array($eHandler, 'handleException'));
82 $listener->configure($event);
83 } catch (\Exception $exception) {
85 restore_exception_handler();
87 if (null !== $exception) {
91 $this->assertNull($h);
94 public function testConsoleEvent()
96 $dispatcher = new EventDispatcher();
97 $listener = new DebugHandlersListener(null);
98 $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
99 $app->expects($this->once())->method('getHelperSet')->will($this->returnValue(new HelperSet()));
100 $command = new Command(__FUNCTION__);
101 $command->setApplication($app);
102 $event = new ConsoleEvent($command, new ArgvInput(), new ConsoleOutput());
104 $dispatcher->addSubscriber($listener);
107 KernelEvents::REQUEST => array(array($listener, 'configure')),
108 ConsoleEvents::COMMAND => array(array($listener, 'configure')),
110 $this->assertSame($xListeners, $dispatcher->getListeners());
113 $eHandler = new ErrorHandler();
114 set_error_handler(array($eHandler, 'handleError'));
115 set_exception_handler(array($eHandler, 'handleException'));
117 $dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
118 } catch (\Exception $exception) {
120 restore_exception_handler();
121 restore_error_handler();
123 if (null !== $exception) {
127 $xHandler = $eHandler->setExceptionHandler('var_dump');
128 $this->assertInstanceOf('Closure', $xHandler);
130 $app->expects($this->once())
131 ->method('renderException');
133 $xHandler(new \Exception());