PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/debug/Symfony/Component/Debug/Tests/ErrorHandlerTest.php

https://gitlab.com/HatemProjects/hatem
PHP | 242 lines | 172 code | 45 blank | 25 comment | 0 complexity | f9f8426de1e144e06d2e53f89745b942 MD5 | raw file
  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. use Symfony\Component\Debug\Exception\ContextErrorException;
  13. /**
  14. * ErrorHandlerTest.
  15. *
  16. * @author Robert Schönthal <seroscho@googlemail.com>
  17. */
  18. class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var int Error reporting level before running tests.
  22. */
  23. protected $errorReporting;
  24. public function setUp()
  25. {
  26. $this->errorReporting = error_reporting(E_ALL | E_STRICT);
  27. $this->iniSet('display_errors', '1');
  28. }
  29. public function tearDown()
  30. {
  31. error_reporting($this->errorReporting);
  32. }
  33. public function testNotice()
  34. {
  35. ErrorHandler::register();
  36. try {
  37. self::triggerNotice($this);
  38. $this->fail('ContextErrorException expected');
  39. } catch (ContextErrorException $exception) {
  40. // if an exception is thrown, the test passed
  41. restore_error_handler();
  42. $this->assertEquals(E_NOTICE, $exception->getSeverity());
  43. $this->assertEquals(__FILE__, $exception->getFile());
  44. $this->assertRegexp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage());
  45. $this->assertArrayHasKey('foobar', $exception->getContext());
  46. $trace = $exception->getTrace();
  47. $this->assertEquals(__FILE__, $trace[0]['file']);
  48. $this->assertEquals('Symfony\Component\Debug\ErrorHandler', $trace[0]['class']);
  49. $this->assertEquals('handle', $trace[0]['function']);
  50. $this->assertEquals('->', $trace[0]['type']);
  51. $this->assertEquals(__FILE__, $trace[1]['file']);
  52. $this->assertEquals(__CLASS__, $trace[1]['class']);
  53. $this->assertEquals('triggerNotice', $trace[1]['function']);
  54. $this->assertEquals('::', $trace[1]['type']);
  55. $this->assertEquals(__CLASS__, $trace[2]['class']);
  56. $this->assertEquals('testNotice', $trace[2]['function']);
  57. $this->assertEquals('->', $trace[2]['type']);
  58. } catch (\Exception $e) {
  59. restore_error_handler();
  60. throw $e;
  61. }
  62. restore_error_handler();
  63. }
  64. // dummy function to test trace in error handler.
  65. private static function triggerNotice($that)
  66. {
  67. // dummy variable to check for in error handler.
  68. $foobar = 123;
  69. $that->assertSame('', $foo.$foo.$bar);
  70. }
  71. public function testConstruct()
  72. {
  73. try {
  74. $handler = ErrorHandler::register(3);
  75. $level = new \ReflectionProperty($handler, 'level');
  76. $level->setAccessible(true);
  77. $this->assertEquals(3, $level->getValue($handler));
  78. restore_error_handler();
  79. } catch (\Exception $e) {
  80. restore_error_handler();
  81. throw $e;
  82. }
  83. }
  84. public function testHandle()
  85. {
  86. try {
  87. $handler = ErrorHandler::register(0);
  88. $this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, array()));
  89. restore_error_handler();
  90. $handler = ErrorHandler::register(3);
  91. $this->assertFalse($handler->handle(4, 'foo', 'foo.php', 12, array()));
  92. restore_error_handler();
  93. $handler = ErrorHandler::register(3);
  94. try {
  95. $handler->handle(111, 'foo', 'foo.php', 12, array());
  96. } catch (\ErrorException $e) {
  97. $this->assertSame('111: foo in foo.php line 12', $e->getMessage());
  98. $this->assertSame(111, $e->getSeverity());
  99. $this->assertSame('foo.php', $e->getFile());
  100. $this->assertSame(12, $e->getLine());
  101. }
  102. restore_error_handler();
  103. $handler = ErrorHandler::register(E_USER_DEPRECATED);
  104. $this->assertFalse($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
  105. restore_error_handler();
  106. $handler = ErrorHandler::register(E_DEPRECATED);
  107. $this->assertFalse($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, array()));
  108. restore_error_handler();
  109. $logger = $this->getMock('Psr\Log\LoggerInterface');
  110. $that = $this;
  111. $warnArgCheck = function ($message, $context) use ($that) {
  112. $that->assertEquals('foo', $message);
  113. $that->assertArrayHasKey('type', $context);
  114. $that->assertEquals($context['type'], ErrorHandler::TYPE_DEPRECATION);
  115. $that->assertArrayHasKey('stack', $context);
  116. $that->assertInternalType('array', $context['stack']);
  117. };
  118. $logger
  119. ->expects($this->once())
  120. ->method('warning')
  121. ->will($this->returnCallback($warnArgCheck))
  122. ;
  123. $handler = ErrorHandler::register(E_USER_DEPRECATED);
  124. $handler->setLogger($logger);
  125. $this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
  126. restore_error_handler();
  127. $logger = $this->getMock('Psr\Log\LoggerInterface');
  128. $that = $this;
  129. $logArgCheck = function ($level, $message, $context) use ($that) {
  130. $that->assertEquals('Undefined variable: undefVar', $message);
  131. $that->assertArrayHasKey('type', $context);
  132. $that->assertEquals($context['type'], E_NOTICE);
  133. };
  134. $logger
  135. ->expects($this->once())
  136. ->method('log')
  137. ->will($this->returnCallback($logArgCheck))
  138. ;
  139. $handler = ErrorHandler::register(E_NOTICE);
  140. $handler->setLogger($logger, 'scream');
  141. unset($undefVar);
  142. @$undefVar++;
  143. restore_error_handler();
  144. } catch (\Exception $e) {
  145. restore_error_handler();
  146. throw $e;
  147. }
  148. }
  149. /**
  150. * @dataProvider provideFatalErrorHandlersData
  151. */
  152. public function testFatalErrorHandlers($error, $class, $translatedMessage)
  153. {
  154. $handler = new ErrorHandler();
  155. $exceptionHandler = new MockExceptionHandler();
  156. $m = new \ReflectionMethod($handler, 'handleFatalError');
  157. $m->setAccessible(true);
  158. $m->invoke($handler, array($exceptionHandler, 'handle'), $error);
  159. restore_error_handler();
  160. $this->assertInstanceof($class, $exceptionHandler->e);
  161. // class names are case insensitive and PHP/HHVM do not return the same
  162. $this->assertSame(strtolower($translatedMessage), strtolower($exceptionHandler->e->getMessage()));
  163. $this->assertSame($error['type'], $exceptionHandler->e->getSeverity());
  164. $this->assertSame($error['file'], $exceptionHandler->e->getFile());
  165. $this->assertSame($error['line'], $exceptionHandler->e->getLine());
  166. }
  167. public function provideFatalErrorHandlersData()
  168. {
  169. return array(
  170. // undefined function
  171. array(
  172. array(
  173. 'type' => 1,
  174. 'line' => 12,
  175. 'file' => 'foo.php',
  176. 'message' => 'Call to undefined function test_namespaced_function_again()',
  177. ),
  178. 'Symfony\Component\Debug\Exception\UndefinedFunctionException',
  179. 'Attempted to call function "test_namespaced_function_again" from the global namespace in foo.php line 12. Did you mean to call: "\\symfony\\component\\debug\\tests\\test_namespaced_function_again"?',
  180. ),
  181. // class not found
  182. array(
  183. array(
  184. 'type' => 1,
  185. 'line' => 12,
  186. 'file' => 'foo.php',
  187. 'message' => 'Class \'WhizBangFactory\' not found',
  188. ),
  189. 'Symfony\Component\Debug\Exception\ClassNotFoundException',
  190. 'Attempted to load class "WhizBangFactory" from the global namespace in foo.php line 12. Did you forget a use statement for this class?',
  191. ),
  192. );
  193. }
  194. }
  195. function test_namespaced_function_again()
  196. {
  197. }