No Description

ErrorHandlerTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 Psr\Log\LogLevel;
  12. use Symfony\Component\Debug\ErrorHandler;
  13. use Symfony\Component\Debug\Exception\ContextErrorException;
  14. /**
  15. * ErrorHandlerTest.
  16. *
  17. * @author Robert Schönthal <seroscho@googlemail.com>
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
  21. {
  22. public function testRegister()
  23. {
  24. $handler = ErrorHandler::register();
  25. try {
  26. $this->assertInstanceOf('Symfony\Component\Debug\ErrorHandler', $handler);
  27. $this->assertSame($handler, ErrorHandler::register());
  28. $newHandler = new ErrorHandler();
  29. $this->assertSame($newHandler, ErrorHandler::register($newHandler, false));
  30. $h = set_error_handler('var_dump');
  31. restore_error_handler();
  32. $this->assertSame(array($handler, 'handleError'), $h);
  33. try {
  34. $this->assertSame($newHandler, ErrorHandler::register($newHandler, true));
  35. $h = set_error_handler('var_dump');
  36. restore_error_handler();
  37. $this->assertSame(array($newHandler, 'handleError'), $h);
  38. } catch (\Exception $e) {
  39. }
  40. restore_error_handler();
  41. restore_exception_handler();
  42. if (isset($e)) {
  43. throw $e;
  44. }
  45. } catch (\Exception $e) {
  46. }
  47. restore_error_handler();
  48. restore_exception_handler();
  49. if (isset($e)) {
  50. throw $e;
  51. }
  52. }
  53. public function testNotice()
  54. {
  55. ErrorHandler::register();
  56. try {
  57. self::triggerNotice($this);
  58. $this->fail('ContextErrorException expected');
  59. } catch (ContextErrorException $exception) {
  60. // if an exception is thrown, the test passed
  61. restore_error_handler();
  62. restore_exception_handler();
  63. $this->assertEquals(E_NOTICE, $exception->getSeverity());
  64. $this->assertEquals(__FILE__, $exception->getFile());
  65. $this->assertRegexp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage());
  66. $this->assertArrayHasKey('foobar', $exception->getContext());
  67. $trace = $exception->getTrace();
  68. $this->assertEquals(__FILE__, $trace[0]['file']);
  69. $this->assertEquals('Symfony\Component\Debug\ErrorHandler', $trace[0]['class']);
  70. $this->assertEquals('handleError', $trace[0]['function']);
  71. $this->assertEquals('->', $trace[0]['type']);
  72. $this->assertEquals(__FILE__, $trace[1]['file']);
  73. $this->assertEquals(__CLASS__, $trace[1]['class']);
  74. $this->assertEquals('triggerNotice', $trace[1]['function']);
  75. $this->assertEquals('::', $trace[1]['type']);
  76. $this->assertEquals(__FILE__, $trace[1]['file']);
  77. $this->assertEquals(__CLASS__, $trace[2]['class']);
  78. $this->assertEquals(__FUNCTION__, $trace[2]['function']);
  79. $this->assertEquals('->', $trace[2]['type']);
  80. } catch (\Exception $e) {
  81. restore_error_handler();
  82. restore_exception_handler();
  83. throw $e;
  84. }
  85. }
  86. // dummy function to test trace in error handler.
  87. private static function triggerNotice($that)
  88. {
  89. // dummy variable to check for in error handler.
  90. $foobar = 123;
  91. $that->assertSame('', $foo.$foo.$bar);
  92. }
  93. public function testConstruct()
  94. {
  95. try {
  96. $handler = ErrorHandler::register();
  97. $handler->throwAt(3, true);
  98. $this->assertEquals(3 | E_RECOVERABLE_ERROR | E_USER_ERROR, $handler->throwAt(0));
  99. restore_error_handler();
  100. restore_exception_handler();
  101. } catch (\Exception $e) {
  102. restore_error_handler();
  103. restore_exception_handler();
  104. throw $e;
  105. }
  106. }
  107. public function testDefaultLogger()
  108. {
  109. try {
  110. $handler = ErrorHandler::register();
  111. $logger = $this->getMock('Psr\Log\LoggerInterface');
  112. $handler->setDefaultLogger($logger, E_NOTICE);
  113. $handler->setDefaultLogger($logger, array(E_USER_NOTICE => LogLevel::CRITICAL));
  114. $loggers = array(
  115. E_DEPRECATED => array(null, LogLevel::INFO),
  116. E_USER_DEPRECATED => array(null, LogLevel::INFO),
  117. E_NOTICE => array($logger, LogLevel::NOTICE),
  118. E_USER_NOTICE => array($logger, LogLevel::CRITICAL),
  119. E_STRICT => array(null, LogLevel::NOTICE),
  120. E_WARNING => array(null, LogLevel::WARNING),
  121. E_USER_WARNING => array(null, LogLevel::WARNING),
  122. E_COMPILE_WARNING => array(null, LogLevel::WARNING),
  123. E_CORE_WARNING => array(null, LogLevel::WARNING),
  124. E_USER_ERROR => array(null, LogLevel::ERROR),
  125. E_RECOVERABLE_ERROR => array(null, LogLevel::ERROR),
  126. E_COMPILE_ERROR => array(null, LogLevel::EMERGENCY),
  127. E_PARSE => array(null, LogLevel::EMERGENCY),
  128. E_ERROR => array(null, LogLevel::EMERGENCY),
  129. E_CORE_ERROR => array(null, LogLevel::EMERGENCY),
  130. );
  131. $this->assertSame($loggers, $handler->setLoggers(array()));
  132. restore_error_handler();
  133. restore_exception_handler();
  134. } catch (\Exception $e) {
  135. restore_error_handler();
  136. restore_exception_handler();
  137. throw $e;
  138. }
  139. }
  140. public function testHandleError()
  141. {
  142. $this->iniSet('error_reporting', -1);
  143. try {
  144. $handler = ErrorHandler::register();
  145. $handler->throwAt(0, true);
  146. $this->assertFalse($handler->handleError(0, 'foo', 'foo.php', 12, array()));
  147. restore_error_handler();
  148. restore_exception_handler();
  149. $handler = ErrorHandler::register();
  150. $handler->throwAt(3, true);
  151. $this->assertFalse($handler->handleError(4, 'foo', 'foo.php', 12, array()));
  152. restore_error_handler();
  153. restore_exception_handler();
  154. $handler = ErrorHandler::register();
  155. $handler->throwAt(3, true);
  156. try {
  157. $handler->handleError(4, 'foo', 'foo.php', 12, array());
  158. } catch (\ErrorException $e) {
  159. $this->assertSame('Parse Error: foo', $e->getMessage());
  160. $this->assertSame(4, $e->getSeverity());
  161. $this->assertSame('foo.php', $e->getFile());
  162. $this->assertSame(12, $e->getLine());
  163. }
  164. restore_error_handler();
  165. restore_exception_handler();
  166. $handler = ErrorHandler::register();
  167. $handler->throwAt(E_USER_DEPRECATED, true);
  168. $this->assertFalse($handler->handleError(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
  169. restore_error_handler();
  170. restore_exception_handler();
  171. $handler = ErrorHandler::register();
  172. $handler->throwAt(E_DEPRECATED, true);
  173. $this->assertFalse($handler->handleError(E_DEPRECATED, 'foo', 'foo.php', 12, array()));
  174. restore_error_handler();
  175. restore_exception_handler();
  176. $logger = $this->getMock('Psr\Log\LoggerInterface');
  177. $that = $this;
  178. $warnArgCheck = function ($logLevel, $message, $context) use ($that) {
  179. $that->assertEquals('info', $logLevel);
  180. $that->assertEquals('foo', $message);
  181. $that->assertArrayHasKey('type', $context);
  182. $that->assertEquals($context['type'], E_USER_DEPRECATED);
  183. $that->assertArrayHasKey('stack', $context);
  184. $that->assertInternalType('array', $context['stack']);
  185. };
  186. $logger
  187. ->expects($this->once())
  188. ->method('log')
  189. ->will($this->returnCallback($warnArgCheck))
  190. ;
  191. $handler = ErrorHandler::register();
  192. $handler->setDefaultLogger($logger, E_USER_DEPRECATED);
  193. $this->assertTrue($handler->handleError(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
  194. restore_error_handler();
  195. restore_exception_handler();
  196. $logger = $this->getMock('Psr\Log\LoggerInterface');
  197. $that = $this;
  198. $logArgCheck = function ($level, $message, $context) use ($that) {
  199. $that->assertEquals('Undefined variable: undefVar', $message);
  200. $that->assertArrayHasKey('type', $context);
  201. $that->assertEquals($context['type'], E_NOTICE);
  202. };
  203. $logger
  204. ->expects($this->once())
  205. ->method('log')
  206. ->will($this->returnCallback($logArgCheck))
  207. ;
  208. $handler = ErrorHandler::register();
  209. $handler->setDefaultLogger($logger, E_NOTICE);
  210. $handler->screamAt(E_NOTICE);
  211. unset($undefVar);
  212. @$undefVar++;
  213. restore_error_handler();
  214. restore_exception_handler();
  215. } catch (\Exception $e) {
  216. restore_error_handler();
  217. restore_exception_handler();
  218. throw $e;
  219. }
  220. }
  221. public function testHandleException()
  222. {
  223. try {
  224. $handler = ErrorHandler::register();
  225. $exception = new \Exception('foo');
  226. $logger = $this->getMock('Psr\Log\LoggerInterface');
  227. $that = $this;
  228. $logArgCheck = function ($level, $message, $context) use ($that) {
  229. $that->assertEquals('Uncaught Exception: foo', $message);
  230. $that->assertArrayHasKey('type', $context);
  231. $that->assertEquals($context['type'], E_ERROR);
  232. };
  233. $logger
  234. ->expects($this->exactly(2))
  235. ->method('log')
  236. ->will($this->returnCallback($logArgCheck))
  237. ;
  238. $handler->setDefaultLogger($logger, E_ERROR);
  239. try {
  240. $handler->handleException($exception);
  241. $this->fail('Exception expected');
  242. } catch (\Exception $e) {
  243. $this->assertSame($exception, $e);
  244. }
  245. $that = $this;
  246. $handler->setExceptionHandler(function ($e) use ($exception, $that) {
  247. $that->assertSame($exception, $e);
  248. });
  249. $handler->handleException($exception);
  250. restore_error_handler();
  251. restore_exception_handler();
  252. } catch (\Exception $e) {
  253. restore_error_handler();
  254. restore_exception_handler();
  255. throw $e;
  256. }
  257. }
  258. public function testHandleFatalError()
  259. {
  260. try {
  261. $handler = ErrorHandler::register();
  262. $error = array(
  263. 'type' => E_PARSE,
  264. 'message' => 'foo',
  265. 'file' => 'bar',
  266. 'line' => 123,
  267. );
  268. $logger = $this->getMock('Psr\Log\LoggerInterface');
  269. $that = $this;
  270. $logArgCheck = function ($level, $message, $context) use ($that) {
  271. $that->assertEquals('Fatal Parse Error: foo', $message);
  272. $that->assertArrayHasKey('type', $context);
  273. $that->assertEquals($context['type'], E_ERROR);
  274. };
  275. $logger
  276. ->expects($this->once())
  277. ->method('log')
  278. ->will($this->returnCallback($logArgCheck))
  279. ;
  280. $handler->setDefaultLogger($logger, E_ERROR);
  281. $handler->handleFatalError($error);
  282. restore_error_handler();
  283. restore_exception_handler();
  284. } catch (\Exception $e) {
  285. restore_error_handler();
  286. restore_exception_handler();
  287. throw $e;
  288. }
  289. }
  290. public function testLegacyInterface()
  291. {
  292. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  293. try {
  294. $handler = ErrorHandler::register(0);
  295. $this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, array()));
  296. restore_error_handler();
  297. restore_exception_handler();
  298. $logger = $this->getMock('Psr\Log\LoggerInterface');
  299. $that = $this;
  300. $logArgCheck = function ($level, $message, $context) use ($that) {
  301. $that->assertEquals('Undefined variable: undefVar', $message);
  302. $that->assertArrayHasKey('type', $context);
  303. $that->assertEquals($context['type'], E_NOTICE);
  304. };
  305. $logger
  306. ->expects($this->once())
  307. ->method('log')
  308. ->will($this->returnCallback($logArgCheck))
  309. ;
  310. $handler = ErrorHandler::register(E_NOTICE);
  311. $handler->setLogger($logger, 'scream');
  312. unset($undefVar);
  313. @$undefVar++;
  314. restore_error_handler();
  315. restore_exception_handler();
  316. } catch (\Exception $e) {
  317. restore_error_handler();
  318. restore_exception_handler();
  319. throw $e;
  320. }
  321. }
  322. }