ErrorHandlerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * ErrorHandlerTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. public $_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. App::build(array(
  36. 'View' => array(
  37. CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
  38. )
  39. ), true);
  40. Router::reload();
  41. $request = new CakeRequest(null, false);
  42. $request->base = '';
  43. Router::setRequestInfo($request);
  44. $this->_debug = Configure::read('debug');
  45. $this->_error = Configure::read('Error');
  46. Configure::write('debug', 2);
  47. }
  48. /**
  49. * teardown
  50. *
  51. * @return void
  52. */
  53. public function teardown() {
  54. Configure::write('debug', $this->_debug);
  55. Configure::write('Error', $this->_error);
  56. App::build();
  57. if ($this->_restoreError) {
  58. restore_error_handler();
  59. }
  60. }
  61. /**
  62. * test error handling when debug is on, an error should be printed from Debugger.
  63. *
  64. * @return void
  65. */
  66. public function testHandleErrorDebugOn() {
  67. set_error_handler('ErrorHandler::handleError');
  68. $this->_restoreError = true;
  69. ob_start();
  70. $wrong .= '';
  71. $result = ob_get_clean();
  72. $this->assertPattern('/<pre class="cake-error">/', $result);
  73. $this->assertPattern('/<b>Notice<\/b>/', $result);
  74. $this->assertPattern('/variable:\s+wrong/', $result);
  75. }
  76. /**
  77. * provides errors for mapping tests.
  78. *
  79. * @return void
  80. */
  81. public static function errorProvider() {
  82. return array(
  83. array(E_USER_NOTICE, 'Notice'),
  84. array(E_USER_WARNING, 'Warning'),
  85. array(E_USER_ERROR, 'Fatal Error'),
  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->assertPattern('/<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->assertEqual(count($result), 1);
  131. $this->assertPattern(
  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->assertPattern(
  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->assertPattern('/^Trace:/', $result[1]);
  157. $this->assertPattern('/^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->assertPattern('/Kaboom!/', $result, 'message missing.');
  172. }
  173. /**
  174. * test handleException generating a page.
  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->assertPattern('/Kaboom!/', $result, 'message missing.');
  189. $log = file(LOGS . 'error.log');
  190. $this->assertPattern('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
  191. $this->assertPattern('/\#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 testLoadPluginHanlder() {
  199. App::build(array(
  200. 'plugins' => array(
  201. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  202. )
  203. ), true);
  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($result, 'Rendered by test plugin');
  211. CakePlugin::unload();
  212. }
  213. }