Updating Media dependent modules to versions compatible with core Media.
[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  * @author Nicolas Grekas <p@tchwork.com>
33  */
34 class DebugHandlersListenerTest extends TestCase
35 {
36     public function testConfigure()
37     {
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'));
44
45         $exception = null;
46         set_error_handler(array($eHandler, 'handleError'));
47         set_exception_handler(array($eHandler, 'handleException'));
48         try {
49             $listener->configure();
50         } catch (\Exception $exception) {
51         }
52         restore_exception_handler();
53         restore_error_handler();
54
55         if (null !== $exception) {
56             throw $exception;
57         }
58
59         $this->assertSame($userHandler, $xHandler->setHandler('var_dump'));
60
61         $loggers = $eHandler->setLoggers(array());
62
63         $this->assertArrayHasKey(E_DEPRECATED, $loggers);
64         $this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
65     }
66
67     public function testConfigureForHttpKernelWithNoTerminateWithException()
68     {
69         $listener = new DebugHandlersListener(null);
70         $eHandler = new ErrorHandler();
71         $event = new KernelEvent(
72             $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(),
73             Request::create('/'),
74             HttpKernelInterface::MASTER_REQUEST
75         );
76
77         $exception = null;
78         $h = set_exception_handler(array($eHandler, 'handleException'));
79         try {
80             $listener->configure($event);
81         } catch (\Exception $exception) {
82         }
83         restore_exception_handler();
84
85         if (null !== $exception) {
86             throw $exception;
87         }
88
89         $this->assertNull($h);
90     }
91
92     public function testConsoleEvent()
93     {
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());
101
102         $dispatcher->addSubscriber($listener);
103
104         $xListeners = array(
105             KernelEvents::REQUEST => array(array($listener, 'configure')),
106             ConsoleEvents::COMMAND => array(array($listener, 'configure')),
107         );
108         $this->assertSame($xListeners, $dispatcher->getListeners());
109
110         $exception = null;
111         $eHandler = new ErrorHandler();
112         set_error_handler(array($eHandler, 'handleError'));
113         set_exception_handler(array($eHandler, 'handleException'));
114         try {
115             $dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
116         } catch (\Exception $exception) {
117         }
118         restore_exception_handler();
119         restore_error_handler();
120
121         if (null !== $exception) {
122             throw $exception;
123         }
124
125         $xHandler = $eHandler->setExceptionHandler('var_dump');
126         $this->assertInstanceOf('Closure', $xHandler);
127
128         $app->expects($this->once())
129             ->method('renderException');
130
131         $xHandler(new \Exception());
132     }
133
134     public function testReplaceExistingExceptionHandler()
135     {
136         $userHandler = function () {};
137         $listener = new DebugHandlersListener($userHandler);
138         $eHandler = new ErrorHandler();
139         $eHandler->setExceptionHandler('var_dump');
140
141         $exception = null;
142         set_exception_handler(array($eHandler, 'handleException'));
143         try {
144             $listener->configure();
145         } catch (\Exception $exception) {
146         }
147         restore_exception_handler();
148
149         if (null !== $exception) {
150             throw $exception;
151         }
152
153         $this->assertSame($userHandler, $eHandler->setExceptionHandler('var_dump'));
154     }
155 }