PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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