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\Debug\Tests;
14 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\Debug\BufferingLogger;
17 use Symfony\Component\Debug\ErrorHandler;
18 use Symfony\Component\Debug\Exception\SilencedErrorContext;
23 * @author Robert Schönthal <seroscho@googlemail.com>
24 * @author Nicolas Grekas <p@tchwork.com>
26 class ErrorHandlerTest extends TestCase
28 public function testRegister()
30 $handler = ErrorHandler::register();
33 $this->assertInstanceOf('Symfony\Component\Debug\ErrorHandler', $handler);
34 $this->assertSame($handler, ErrorHandler::register());
36 $newHandler = new ErrorHandler();
38 $this->assertSame($handler, ErrorHandler::register($newHandler, false));
39 $h = set_error_handler('var_dump');
40 restore_error_handler();
41 $this->assertSame(array($handler, 'handleError'), $h);
44 $this->assertSame($newHandler, ErrorHandler::register($newHandler, true));
45 $h = set_error_handler('var_dump');
46 restore_error_handler();
47 $this->assertSame(array($newHandler, 'handleError'), $h);
48 } catch (\Exception $e) {
51 restore_error_handler();
52 restore_exception_handler();
57 } catch (\Exception $e) {
60 restore_error_handler();
61 restore_exception_handler();
68 public function testNotice()
70 ErrorHandler::register();
73 self::triggerNotice($this);
74 $this->fail('ErrorException expected');
75 } catch (\ErrorException $exception) {
76 // if an exception is thrown, the test passed
77 $this->assertEquals(E_NOTICE, $exception->getSeverity());
78 $this->assertEquals(__FILE__, $exception->getFile());
79 $this->assertRegExp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage());
81 $trace = $exception->getTrace();
83 $this->assertEquals(__FILE__, $trace[0]['file']);
84 $this->assertEquals(__CLASS__, $trace[0]['class']);
85 $this->assertEquals('triggerNotice', $trace[0]['function']);
86 $this->assertEquals('::', $trace[0]['type']);
88 $this->assertEquals(__FILE__, $trace[0]['file']);
89 $this->assertEquals(__CLASS__, $trace[1]['class']);
90 $this->assertEquals(__FUNCTION__, $trace[1]['function']);
91 $this->assertEquals('->', $trace[1]['type']);
93 restore_error_handler();
94 restore_exception_handler();
98 // dummy function to test trace in error handler.
99 private static function triggerNotice($that)
101 $that->assertSame('', $foo.$foo.$bar);
104 public function testConstruct()
107 $handler = ErrorHandler::register();
108 $handler->throwAt(3, true);
109 $this->assertEquals(3 | E_RECOVERABLE_ERROR | E_USER_ERROR, $handler->throwAt(0));
111 restore_error_handler();
112 restore_exception_handler();
116 public function testDefaultLogger()
119 $handler = ErrorHandler::register();
121 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
123 $handler->setDefaultLogger($logger, E_NOTICE);
124 $handler->setDefaultLogger($logger, array(E_USER_NOTICE => LogLevel::CRITICAL));
127 E_DEPRECATED => array(null, LogLevel::INFO),
128 E_USER_DEPRECATED => array(null, LogLevel::INFO),
129 E_NOTICE => array($logger, LogLevel::WARNING),
130 E_USER_NOTICE => array($logger, LogLevel::CRITICAL),
131 E_STRICT => array(null, LogLevel::WARNING),
132 E_WARNING => array(null, LogLevel::WARNING),
133 E_USER_WARNING => array(null, LogLevel::WARNING),
134 E_COMPILE_WARNING => array(null, LogLevel::WARNING),
135 E_CORE_WARNING => array(null, LogLevel::WARNING),
136 E_USER_ERROR => array(null, LogLevel::CRITICAL),
137 E_RECOVERABLE_ERROR => array(null, LogLevel::CRITICAL),
138 E_COMPILE_ERROR => array(null, LogLevel::CRITICAL),
139 E_PARSE => array(null, LogLevel::CRITICAL),
140 E_ERROR => array(null, LogLevel::CRITICAL),
141 E_CORE_ERROR => array(null, LogLevel::CRITICAL),
143 $this->assertSame($loggers, $handler->setLoggers(array()));
145 restore_error_handler();
146 restore_exception_handler();
150 public function testHandleError()
153 $handler = ErrorHandler::register();
154 $handler->throwAt(0, true);
155 $this->assertFalse($handler->handleError(0, 'foo', 'foo.php', 12, array()));
157 restore_error_handler();
158 restore_exception_handler();
160 $handler = ErrorHandler::register();
161 $handler->throwAt(3, true);
162 $this->assertFalse($handler->handleError(4, 'foo', 'foo.php', 12, array()));
164 restore_error_handler();
165 restore_exception_handler();
167 $handler = ErrorHandler::register();
168 $handler->throwAt(3, true);
170 $handler->handleError(4, 'foo', 'foo.php', 12, array());
171 } catch (\ErrorException $e) {
172 $this->assertSame('Parse Error: foo', $e->getMessage());
173 $this->assertSame(4, $e->getSeverity());
174 $this->assertSame('foo.php', $e->getFile());
175 $this->assertSame(12, $e->getLine());
178 restore_error_handler();
179 restore_exception_handler();
181 $handler = ErrorHandler::register();
182 $handler->throwAt(E_USER_DEPRECATED, true);
183 $this->assertFalse($handler->handleError(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
185 restore_error_handler();
186 restore_exception_handler();
188 $handler = ErrorHandler::register();
189 $handler->throwAt(E_DEPRECATED, true);
190 $this->assertFalse($handler->handleError(E_DEPRECATED, 'foo', 'foo.php', 12, array()));
192 restore_error_handler();
193 restore_exception_handler();
195 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
197 $warnArgCheck = function ($logLevel, $message, $context) {
198 $this->assertEquals('info', $logLevel);
199 $this->assertEquals('User Deprecated: foo', $message);
200 $this->assertArrayHasKey('exception', $context);
201 $exception = $context['exception'];
202 $this->assertInstanceOf(\ErrorException::class, $exception);
203 $this->assertSame('User Deprecated: foo', $exception->getMessage());
204 $this->assertSame(E_USER_DEPRECATED, $exception->getSeverity());
208 ->expects($this->once())
210 ->will($this->returnCallback($warnArgCheck))
213 $handler = ErrorHandler::register();
214 $handler->setDefaultLogger($logger, E_USER_DEPRECATED);
215 $this->assertTrue($handler->handleError(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
217 restore_error_handler();
218 restore_exception_handler();
220 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
223 $logArgCheck = function ($level, $message, $context) use (&$line) {
224 $this->assertEquals('Notice: Undefined variable: undefVar', $message);
225 $this->assertArrayHasKey('exception', $context);
226 $exception = $context['exception'];
227 $this->assertInstanceOf(SilencedErrorContext::class, $exception);
228 $this->assertSame(E_NOTICE, $exception->getSeverity());
229 $this->assertSame(__FILE__, $exception->getFile());
230 $this->assertSame($line, $exception->getLine());
231 $this->assertNotEmpty($exception->getTrace());
232 $this->assertSame(1, $exception->count);
236 ->expects($this->once())
238 ->will($this->returnCallback($logArgCheck))
241 $handler = ErrorHandler::register();
242 $handler->setDefaultLogger($logger, E_NOTICE);
243 $handler->screamAt(E_NOTICE);
245 $line = __LINE__ + 1;
248 restore_error_handler();
249 restore_exception_handler();
250 } catch (\Exception $e) {
251 restore_error_handler();
252 restore_exception_handler();
258 public function testHandleUserError()
261 $handler = ErrorHandler::register();
262 $handler->throwAt(0, true);
265 $x = new \Exception('Foo');
268 $f = new Fixtures\ToStringThrower($x);
269 $f .= ''; // Trigger $f->__toString()
270 } catch (\Exception $e) {
273 $this->assertSame($x, $e);
275 restore_error_handler();
276 restore_exception_handler();
280 public function testHandleDeprecation()
282 $logArgCheck = function ($level, $message, $context) {
283 $this->assertEquals(LogLevel::INFO, $level);
284 $this->assertArrayHasKey('exception', $context);
285 $exception = $context['exception'];
286 $this->assertInstanceOf(\ErrorException::class, $exception);
287 $this->assertSame('User Deprecated: Foo deprecation', $exception->getMessage());
290 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
292 ->expects($this->once())
294 ->will($this->returnCallback($logArgCheck))
297 $handler = new ErrorHandler();
298 $handler->setDefaultLogger($logger);
299 @$handler->handleError(E_USER_DEPRECATED, 'Foo deprecation', __FILE__, __LINE__, array());
305 public function testHandleException()
308 $handler = ErrorHandler::register();
310 $exception = new \Exception('foo');
312 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
314 $logArgCheck = function ($level, $message, $context) {
315 $this->assertSame('Uncaught Exception: foo', $message);
316 $this->assertArrayHasKey('exception', $context);
317 $this->assertInstanceOf(\Exception::class, $context['exception']);
321 ->expects($this->exactly(2))
323 ->will($this->returnCallback($logArgCheck))
326 $handler->setDefaultLogger($logger, E_ERROR);
329 $handler->handleException($exception);
330 $this->fail('Exception expected');
331 } catch (\Exception $e) {
332 $this->assertSame($exception, $e);
335 $handler->setExceptionHandler(function ($e) use ($exception) {
336 $this->assertSame($exception, $e);
339 $handler->handleException($exception);
341 restore_error_handler();
342 restore_exception_handler();
349 public function testErrorStacking()
352 $handler = ErrorHandler::register();
353 $handler->screamAt(E_USER_WARNING);
355 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
358 ->expects($this->exactly(2))
361 array($this->equalTo(LogLevel::WARNING), $this->equalTo('Dummy log')),
362 array($this->equalTo(LogLevel::DEBUG), $this->equalTo('User Warning: Silenced warning'))
366 $handler->setDefaultLogger($logger, array(E_USER_WARNING => LogLevel::WARNING));
368 ErrorHandler::stackErrors();
369 @trigger_error('Silenced warning', E_USER_WARNING);
370 $logger->log(LogLevel::WARNING, 'Dummy log');
371 ErrorHandler::unstackErrors();
373 restore_error_handler();
374 restore_exception_handler();
378 public function testBootstrappingLogger()
380 $bootLogger = new BufferingLogger();
381 $handler = new ErrorHandler($bootLogger);
384 E_DEPRECATED => array($bootLogger, LogLevel::INFO),
385 E_USER_DEPRECATED => array($bootLogger, LogLevel::INFO),
386 E_NOTICE => array($bootLogger, LogLevel::WARNING),
387 E_USER_NOTICE => array($bootLogger, LogLevel::WARNING),
388 E_STRICT => array($bootLogger, LogLevel::WARNING),
389 E_WARNING => array($bootLogger, LogLevel::WARNING),
390 E_USER_WARNING => array($bootLogger, LogLevel::WARNING),
391 E_COMPILE_WARNING => array($bootLogger, LogLevel::WARNING),
392 E_CORE_WARNING => array($bootLogger, LogLevel::WARNING),
393 E_USER_ERROR => array($bootLogger, LogLevel::CRITICAL),
394 E_RECOVERABLE_ERROR => array($bootLogger, LogLevel::CRITICAL),
395 E_COMPILE_ERROR => array($bootLogger, LogLevel::CRITICAL),
396 E_PARSE => array($bootLogger, LogLevel::CRITICAL),
397 E_ERROR => array($bootLogger, LogLevel::CRITICAL),
398 E_CORE_ERROR => array($bootLogger, LogLevel::CRITICAL),
401 $this->assertSame($loggers, $handler->setLoggers(array()));
403 $handler->handleError(E_DEPRECATED, 'Foo message', __FILE__, 123, array());
405 $logs = $bootLogger->cleanLogs();
407 $this->assertCount(1, $logs);
409 $this->assertSame('info', $log[0]);
410 $this->assertSame('Deprecated: Foo message', $log[1]);
411 $this->assertArrayHasKey('exception', $log[2]);
412 $exception = $log[2]['exception'];
413 $this->assertInstanceOf(\ErrorException::class, $exception);
414 $this->assertSame('Deprecated: Foo message', $exception->getMessage());
415 $this->assertSame(__FILE__, $exception->getFile());
416 $this->assertSame(123, $exception->getLine());
417 $this->assertSame(E_DEPRECATED, $exception->getSeverity());
419 $bootLogger->log(LogLevel::WARNING, 'Foo message', array('exception' => $exception));
421 $mockLogger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
422 $mockLogger->expects($this->once())
424 ->with(LogLevel::WARNING, 'Foo message', array('exception' => $exception));
426 $handler->setLoggers(array(E_DEPRECATED => array($mockLogger, LogLevel::WARNING)));
432 public function testSettingLoggerWhenExceptionIsBuffered()
434 $bootLogger = new BufferingLogger();
435 $handler = new ErrorHandler($bootLogger);
437 $exception = new \Exception('Foo message');
439 $mockLogger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
440 $mockLogger->expects($this->once())
442 ->with(LogLevel::CRITICAL, 'Uncaught Exception: Foo message', array('exception' => $exception));
444 $handler->setExceptionHandler(function () use ($handler, $mockLogger) {
445 $handler->setDefaultLogger($mockLogger);
448 $handler->handleException($exception);
454 public function testHandleFatalError()
457 $handler = ErrorHandler::register();
466 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
468 $logArgCheck = function ($level, $message, $context) {
469 $this->assertEquals('Fatal Parse Error: foo', $message);
470 $this->assertArrayHasKey('exception', $context);
471 $this->assertInstanceOf(\Exception::class, $context['exception']);
475 ->expects($this->once())
477 ->will($this->returnCallback($logArgCheck))
480 $handler->setDefaultLogger($logger, E_PARSE);
482 $handler->handleFatalError($error);
484 restore_error_handler();
485 restore_exception_handler();
486 } catch (\Exception $e) {
487 restore_error_handler();
488 restore_exception_handler();
497 public function testHandleErrorException()
499 $exception = new \Error("Class 'Foo' not found");
501 $handler = new ErrorHandler();
502 $handler->setExceptionHandler(function () use (&$args) {
503 $args = func_get_args();
506 $handler->handleException($exception);
508 $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $args[0]);
509 $this->assertStringStartsWith("Attempted to load class \"Foo\" from the global namespace.\nDid you forget a \"use\" statement", $args[0]->getMessage());
515 public function testHandleFatalErrorOnHHVM()
518 $handler = ErrorHandler::register();
520 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
522 ->expects($this->once())
525 $this->equalTo(LogLevel::CRITICAL),
526 $this->equalTo('Fatal Error: foo')
530 $handler->setDefaultLogger($logger, E_ERROR);
533 'type' => E_ERROR + 0x1000000, // This error level is used by HHVM for fatal errors
537 'context' => array(123),
538 'backtrace' => array(456),
541 call_user_func_array(array($handler, 'handleError'), $error);
542 $handler->handleFatalError($error);
544 restore_error_handler();
545 restore_exception_handler();