PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Error/ErrorHandlerTest.php

http://github.com/cakephp/cakephp
PHP | 246 lines | 137 code | 31 blank | 78 comment | 4 complexity | 5d60dc053e79140cccaa66a954fdb56d MD5 | raw file
Possible License(s): JSON
  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. parent::setUp();
  36. App::build(array(
  37. 'View' => array(
  38. CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
  39. )
  40. ), true);
  41. Router::reload();
  42. $request = new CakeRequest(null, false);
  43. $request->base = '';
  44. Router::setRequestInfo($request);
  45. $this->_debug = Configure::read('debug');
  46. $this->_error = Configure::read('Error');
  47. Configure::write('debug', 2);
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown() {
  55. Configure::write('debug', $this->_debug);
  56. Configure::write('Error', $this->_error);
  57. App::build();
  58. if ($this->_restoreError) {
  59. restore_error_handler();
  60. }
  61. parent::tearDown();
  62. }
  63. /**
  64. * test error handling when debug is on, an error should be printed from Debugger.
  65. *
  66. * @return void
  67. */
  68. public function testHandleErrorDebugOn() {
  69. set_error_handler('ErrorHandler::handleError');
  70. $this->_restoreError = true;
  71. ob_start();
  72. $wrong .= '';
  73. $result = ob_get_clean();
  74. $this->assertRegExp('/<pre class="cake-error">/', $result);
  75. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  76. $this->assertRegExp('/variable:\s+wrong/', $result);
  77. }
  78. /**
  79. * provides errors for mapping tests.
  80. *
  81. * @return void
  82. */
  83. public static function errorProvider() {
  84. return array(
  85. array(E_USER_NOTICE, 'Notice'),
  86. array(E_USER_WARNING, 'Warning'),
  87. array(E_USER_ERROR, 'Fatal Error'),
  88. );
  89. }
  90. /**
  91. * test error mappings
  92. *
  93. * @dataProvider errorProvider
  94. * @return void
  95. */
  96. public function testErrorMapping($error, $expected) {
  97. set_error_handler('ErrorHandler::handleError');
  98. $this->_restoreError = true;
  99. ob_start();
  100. trigger_error('Test error', $error);
  101. $result = ob_get_clean();
  102. $this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
  103. }
  104. /**
  105. * test error prepended by @
  106. *
  107. * @return void
  108. */
  109. public function testErrorSuppressed() {
  110. set_error_handler('ErrorHandler::handleError');
  111. $this->_restoreError = true;
  112. ob_start();
  113. @include 'invalid.file';
  114. $result = ob_get_clean();
  115. $this->assertTrue(empty($result));
  116. }
  117. /**
  118. * Test that errors go into CakeLog when debug = 0.
  119. *
  120. * @return void
  121. */
  122. public function testHandleErrorDebugOff() {
  123. Configure::write('debug', 0);
  124. Configure::write('Error.trace', false);
  125. if (file_exists(LOGS . 'debug.log')) {
  126. @unlink(LOGS . 'debug.log');
  127. }
  128. set_error_handler('ErrorHandler::handleError');
  129. $this->_restoreError = true;
  130. $out .= '';
  131. $result = file(LOGS . 'debug.log');
  132. $this->assertEquals(count($result), 1);
  133. $this->assertRegExp(
  134. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  135. $result[0]
  136. );
  137. @unlink(LOGS . 'debug.log');
  138. }
  139. /**
  140. * Test that errors going into CakeLog include traces.
  141. *
  142. * @return void
  143. */
  144. public function testHandleErrorLoggingTrace() {
  145. Configure::write('debug', 0);
  146. Configure::write('Error.trace', true);
  147. if (file_exists(LOGS . 'debug.log')) {
  148. @unlink(LOGS . 'debug.log');
  149. }
  150. set_error_handler('ErrorHandler::handleError');
  151. $this->_restoreError = true;
  152. $out .= '';
  153. $result = file(LOGS . 'debug.log');
  154. $this->assertRegExp(
  155. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  156. $result[0]
  157. );
  158. $this->assertRegExp('/^Trace:/', $result[1]);
  159. $this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
  160. @unlink(LOGS . 'debug.log');
  161. }
  162. /**
  163. * test handleException generating a page.
  164. *
  165. * @return void
  166. */
  167. public function testHandleException() {
  168. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  169. $error = new NotFoundException('Kaboom!');
  170. ob_start();
  171. ErrorHandler::handleException($error);
  172. $result = ob_get_clean();
  173. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  174. }
  175. /**
  176. * test handleException generating a page.
  177. *
  178. * @return void
  179. */
  180. public function testHandleExceptionLog() {
  181. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  182. if (file_exists(LOGS . 'error.log')) {
  183. unlink(LOGS . 'error.log');
  184. }
  185. Configure::write('Exception.log', true);
  186. $error = new NotFoundException('Kaboom!');
  187. ob_start();
  188. ErrorHandler::handleException($error);
  189. $result = ob_get_clean();
  190. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  191. $log = file(LOGS . 'error.log');
  192. $this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
  193. $this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
  194. }
  195. /**
  196. * tests it is possible to load a plugin exception renderer
  197. *
  198. * @return void
  199. */
  200. public function testLoadPluginHanlder() {
  201. App::build(array(
  202. 'plugins' => array(
  203. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  204. )
  205. ), true);
  206. CakePlugin::load('TestPlugin');
  207. Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
  208. $error = new NotFoundException('Kaboom!');
  209. ob_start();
  210. ErrorHandler::handleException($error);
  211. $result = ob_get_clean();
  212. $this->assertEquals($result, 'Rendered by test plugin');
  213. CakePlugin::unload();
  214. }
  215. }