ErrorHandlerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * ErrorHandlerTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Error
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ErrorHandler', 'Error');
  20. App::uses('Controller', 'Controller');
  21. App::uses('Router', 'Routing');
  22. /**
  23. * ErrorHandlerTest class
  24. *
  25. * @package Cake.Test.Case.Error
  26. */
  27. class ErrorHandlerTest extends CakeTestCase {
  28. protected $_restoreError = false;
  29. /**
  30. * setup create a request object to get out of router later.
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. App::build(array(
  37. 'View' => array(
  38. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  39. )
  40. ), App::RESET);
  41. Router::reload();
  42. $request = new CakeRequest(null, false);
  43. $request->base = '';
  44. Router::setRequestInfo($request);
  45. Configure::write('debug', 2);
  46. CakeLog::disable('stdout');
  47. CakeLog::disable('stderr');
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown() {
  55. parent::tearDown();
  56. if ($this->_restoreError) {
  57. restore_error_handler();
  58. }
  59. CakeLog::enable('stdout');
  60. CakeLog::enable('stderr');
  61. }
  62. /**
  63. * test error handling when debug is on, an error should be printed from Debugger.
  64. *
  65. * @return void
  66. */
  67. public function testHandleErrorDebugOn() {
  68. set_error_handler('ErrorHandler::handleError');
  69. $this->_restoreError = true;
  70. ob_start();
  71. $wrong .= '';
  72. $result = ob_get_clean();
  73. $this->assertRegExp('/<pre class="cake-error">/', $result);
  74. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  75. $this->assertRegExp('/variable:\s+wrong/', $result);
  76. }
  77. /**
  78. * provides errors for mapping tests.
  79. *
  80. * @return void
  81. */
  82. public static function errorProvider() {
  83. return array(
  84. array(E_USER_NOTICE, 'Notice'),
  85. array(E_USER_WARNING, 'Warning'),
  86. );
  87. }
  88. /**
  89. * test error mappings
  90. *
  91. * @dataProvider errorProvider
  92. * @return void
  93. */
  94. public function testErrorMapping($error, $expected) {
  95. set_error_handler('ErrorHandler::handleError');
  96. $this->_restoreError = true;
  97. ob_start();
  98. trigger_error('Test error', $error);
  99. $result = ob_get_clean();
  100. $this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
  101. }
  102. /**
  103. * test error prepended by @
  104. *
  105. * @return void
  106. */
  107. public function testErrorSuppressed() {
  108. set_error_handler('ErrorHandler::handleError');
  109. $this->_restoreError = true;
  110. ob_start();
  111. @include 'invalid.file';
  112. $result = ob_get_clean();
  113. $this->assertTrue(empty($result));
  114. }
  115. /**
  116. * Test that errors go into CakeLog when debug = 0.
  117. *
  118. * @return void
  119. */
  120. public function testHandleErrorDebugOff() {
  121. Configure::write('debug', 0);
  122. Configure::write('Error.trace', false);
  123. if (file_exists(LOGS . 'debug.log')) {
  124. @unlink(LOGS . 'debug.log');
  125. }
  126. set_error_handler('ErrorHandler::handleError');
  127. $this->_restoreError = true;
  128. $out .= '';
  129. $result = file(LOGS . 'debug.log');
  130. $this->assertEquals(1, count($result));
  131. $this->assertRegExp(
  132. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  133. $result[0]
  134. );
  135. @unlink(LOGS . 'debug.log');
  136. }
  137. /**
  138. * Test that errors going into CakeLog include traces.
  139. *
  140. * @return void
  141. */
  142. public function testHandleErrorLoggingTrace() {
  143. Configure::write('debug', 0);
  144. Configure::write('Error.trace', true);
  145. if (file_exists(LOGS . 'debug.log')) {
  146. @unlink(LOGS . 'debug.log');
  147. }
  148. set_error_handler('ErrorHandler::handleError');
  149. $this->_restoreError = true;
  150. $out .= '';
  151. $result = file(LOGS . 'debug.log');
  152. $this->assertRegExp(
  153. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  154. $result[0]
  155. );
  156. $this->assertRegExp('/^Trace:/', $result[1]);
  157. $this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
  158. @unlink(LOGS . 'debug.log');
  159. }
  160. /**
  161. * test handleException generating a page.
  162. *
  163. * @return void
  164. */
  165. public function testHandleException() {
  166. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  167. $error = new NotFoundException('Kaboom!');
  168. ob_start();
  169. ErrorHandler::handleException($error);
  170. $result = ob_get_clean();
  171. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  172. }
  173. /**
  174. * test handleException generating log.
  175. *
  176. * @return void
  177. */
  178. public function testHandleExceptionLog() {
  179. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  180. if (file_exists(LOGS . 'error.log')) {
  181. unlink(LOGS . 'error.log');
  182. }
  183. Configure::write('Exception.log', true);
  184. $error = new NotFoundException('Kaboom!');
  185. ob_start();
  186. ErrorHandler::handleException($error);
  187. $result = ob_get_clean();
  188. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  189. $log = file(LOGS . 'error.log');
  190. $this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
  191. $this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
  192. }
  193. /**
  194. * tests it is possible to load a plugin exception renderer
  195. *
  196. * @return void
  197. */
  198. public function testLoadPluginHandler() {
  199. App::build(array(
  200. 'Plugin' => array(
  201. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  202. )
  203. ), App::RESET);
  204. CakePlugin::load('TestPlugin');
  205. Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
  206. $error = new NotFoundException('Kaboom!');
  207. ob_start();
  208. ErrorHandler::handleException($error);
  209. $result = ob_get_clean();
  210. $this->assertEquals('Rendered by test plugin', $result);
  211. CakePlugin::unload();
  212. }
  213. /**
  214. * test handleFatalError generating a page.
  215. *
  216. * @return void
  217. */
  218. public function testHandleFatalErrorPage() {
  219. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  220. $originalDebugLevel = Configure::read('debug');
  221. $line = __LINE__;
  222. ob_start();
  223. Configure::write('debug', 1);
  224. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  225. $result = ob_get_clean();
  226. $this->assertContains('Something wrong', $result, 'message missing.');
  227. $this->assertContains(__FILE__, $result, 'filename missing.');
  228. $this->assertContains((string)$line, $result, 'line missing.');
  229. ob_start();
  230. Configure::write('debug', 0);
  231. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  232. $result = ob_get_clean();
  233. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  234. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  235. $this->assertContains('An Internal Error Has Occurred', $result);
  236. Configure::write('debug', $originalDebugLevel);
  237. }
  238. /**
  239. * test handleException generating log.
  240. *
  241. * @return void
  242. */
  243. public function testHandleFatalErrorLog() {
  244. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  245. if (file_exists(LOGS . 'error.log')) {
  246. unlink(LOGS . 'error.log');
  247. }
  248. ob_start();
  249. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  250. ob_clean();
  251. $log = file(LOGS . 'error.log');
  252. $this->assertContains(__FILE__, $log[0], 'missing filename');
  253. $this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
  254. }
  255. }