ErrorHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug\Tests;
  11. use Symfony\Component\Debug\ErrorHandler;
  12. /**
  13. * ErrorHandlerTest
  14. *
  15. * @author Robert Schönthal <seroscho@googlemail.com>
  16. */
  17. class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testCompileTimeError()
  20. {
  21. // the ContextErrorException must not be loaded for this test to work
  22. if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
  23. $this->markTestSkipped('The ContextErrorException class is already loaded.');
  24. }
  25. $handler = ErrorHandler::register(E_ALL | E_STRICT);
  26. $displayErrors = ini_get('display_errors');
  27. ini_set('display_errors', '1');
  28. try {
  29. // trigger compile time error
  30. eval(<<<'PHP'
  31. class _BaseCompileTimeError { function foo() {} }
  32. class _CompileTimeError extends _BaseCompileTimeError { function foo($invalid) {} }
  33. PHP
  34. );
  35. } catch(\Exception $e) {
  36. // if an exception is thrown, the test passed
  37. }
  38. ini_set('display_errors', $displayErrors);
  39. restore_error_handler();
  40. }
  41. public function testConstruct()
  42. {
  43. $handler = ErrorHandler::register(3);
  44. $level = new \ReflectionProperty($handler, 'level');
  45. $level->setAccessible(true);
  46. $this->assertEquals(3, $level->getValue($handler));
  47. restore_error_handler();
  48. }
  49. public function testHandle()
  50. {
  51. $handler = ErrorHandler::register(0);
  52. $this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, 'foo'));
  53. restore_error_handler();
  54. $handler = ErrorHandler::register(3);
  55. $this->assertFalse($handler->handle(4, 'foo', 'foo.php', 12, 'foo'));
  56. restore_error_handler();
  57. $handler = ErrorHandler::register(3);
  58. try {
  59. $handler->handle(111, 'foo', 'foo.php', 12, 'foo');
  60. } catch (\ErrorException $e) {
  61. $this->assertSame('111: foo in foo.php line 12', $e->getMessage());
  62. $this->assertSame(111, $e->getSeverity());
  63. $this->assertSame('foo.php', $e->getFile());
  64. $this->assertSame(12, $e->getLine());
  65. }
  66. restore_error_handler();
  67. $handler = ErrorHandler::register(E_USER_DEPRECATED);
  68. $this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));
  69. restore_error_handler();
  70. $handler = ErrorHandler::register(E_DEPRECATED);
  71. $this->assertTrue($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));
  72. restore_error_handler();
  73. $logger = $this->getMock('Psr\Log\LoggerInterface');
  74. $that = $this;
  75. $warnArgCheck = function($message, $context) use ($that) {
  76. $that->assertEquals('foo', $message);
  77. $that->assertArrayHasKey('type', $context);
  78. $that->assertEquals($context['type'], ErrorHandler::TYPE_DEPRECATION);
  79. $that->assertArrayHasKey('stack', $context);
  80. $that->assertInternalType('array', $context['stack']);
  81. };
  82. $logger
  83. ->expects($this->once())
  84. ->method('warning')
  85. ->will($this->returnCallback($warnArgCheck))
  86. ;
  87. $handler = ErrorHandler::register(E_USER_DEPRECATED);
  88. $handler->setLogger($logger);
  89. $handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo');
  90. restore_error_handler();
  91. }
  92. }