d1349906bbe6991ac419d5db2d4a78e0eece6060
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / DebugHandlersListenerTest.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 Psr\Log\LogLevel;
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;
30
31 /**
32  * DebugHandlersListenerTest.
33  *
34  * @author Nicolas Grekas <p@tchwork.com>
35  */
36 class DebugHandlersListenerTest extends TestCase
37 {
38     public function testConfigure()
39     {
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'));
46
47         $exception = null;
48         set_error_handler(array($eHandler, 'handleError'));
49         set_exception_handler(array($eHandler, 'handleException'));
50         try {
51             $listener->configure();
52         } catch (\Exception $exception) {
53         }
54         restore_exception_handler();
55         restore_error_handler();
56
57         if (null !== $exception) {
58             throw $exception;
59         }
60
61         $this->assertSame($userHandler, $xHandler->setHandler('var_dump'));
62
63         $loggers = $eHandler->setLoggers(array());
64
65         $this->assertArrayHasKey(E_DEPRECATED, $loggers);
66         $this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
67     }
68
69     public function testConfigureForHttpKernelWithNoTerminateWithException()
70     {
71         $listener = new DebugHandlersListener(null);
72         $eHandler = new ErrorHandler();
73         $event = new KernelEvent(
74             $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(),
75             Request::create('/'),
76             HttpKernelInterface::MASTER_REQUEST
77         );
78
79         $exception = null;
80         $h = set_exception_handler(array($eHandler, 'handleException'));
81         try {
82             $listener->configure($event);
83         } catch (\Exception $exception) {
84         }
85         restore_exception_handler();
86
87         if (null !== $exception) {
88             throw $exception;
89         }
90
91         $this->assertNull($h);
92     }
93
94     public function testConsoleEvent()
95     {
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());
103
104         $dispatcher->addSubscriber($listener);
105
106         $xListeners = array(
107             KernelEvents::REQUEST => array(array($listener, 'configure')),
108             ConsoleEvents::COMMAND => array(array($listener, 'configure')),
109         );
110         $this->assertSame($xListeners, $dispatcher->getListeners());
111
112         $exception = null;
113         $eHandler = new ErrorHandler();
114         set_error_handler(array($eHandler, 'handleError'));
115         set_exception_handler(array($eHandler, 'handleException'));
116         try {
117             $dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
118         } catch (\Exception $exception) {
119         }
120         restore_exception_handler();
121         restore_error_handler();
122
123         if (null !== $exception) {
124             throw $exception;
125         }
126
127         $xHandler = $eHandler->setExceptionHandler('var_dump');
128         $this->assertInstanceOf('Closure', $xHandler);
129
130         $app->expects($this->once())
131             ->method('renderException');
132
133         $xHandler(new \Exception());
134     }
135 }