PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/eryx/php-framework-benchmark
PHP | 302 lines | 171 code | 41 blank | 90 comment | 7 complexity | fd3541bd8336a92b114ce8e94f914c83 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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. //@codingStandardsIgnoreStart
  112. @include 'invalid.file';
  113. //@codingStandardsIgnoreEnd
  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(1, count($result));
  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. if (file_exists(LOGS . 'debug.log')) {
  138. unlink(LOGS . 'debug.log');
  139. }
  140. }
  141. /**
  142. * Test that errors going into CakeLog include traces.
  143. *
  144. * @return void
  145. */
  146. public function testHandleErrorLoggingTrace() {
  147. Configure::write('debug', 0);
  148. Configure::write('Error.trace', true);
  149. if (file_exists(LOGS . 'debug.log')) {
  150. unlink(LOGS . 'debug.log');
  151. }
  152. set_error_handler('ErrorHandler::handleError');
  153. $this->_restoreError = true;
  154. $out .= '';
  155. $result = file(LOGS . 'debug.log');
  156. $this->assertRegExp(
  157. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  158. $result[0]
  159. );
  160. $this->assertRegExp('/^Trace:/', $result[1]);
  161. $this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
  162. if (file_exists(LOGS . 'debug.log')) {
  163. unlink(LOGS . 'debug.log');
  164. }
  165. }
  166. /**
  167. * test handleException generating a page.
  168. *
  169. * @return void
  170. */
  171. public function testHandleException() {
  172. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  173. $error = new NotFoundException('Kaboom!');
  174. ob_start();
  175. ErrorHandler::handleException($error);
  176. $result = ob_get_clean();
  177. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  178. }
  179. /**
  180. * test handleException generating log.
  181. *
  182. * @return void
  183. */
  184. public function testHandleExceptionLog() {
  185. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  186. if (file_exists(LOGS . 'error.log')) {
  187. unlink(LOGS . 'error.log');
  188. }
  189. Configure::write('Exception.log', true);
  190. $error = new NotFoundException('Kaboom!');
  191. ob_start();
  192. ErrorHandler::handleException($error);
  193. $result = ob_get_clean();
  194. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  195. $log = file(LOGS . 'error.log');
  196. $this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
  197. $this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
  198. }
  199. /**
  200. * tests it is possible to load a plugin exception renderer
  201. *
  202. * @return void
  203. */
  204. public function testLoadPluginHandler() {
  205. App::build(array(
  206. 'Plugin' => array(
  207. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  208. )
  209. ), App::RESET);
  210. CakePlugin::load('TestPlugin');
  211. Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
  212. $error = new NotFoundException('Kaboom!');
  213. ob_start();
  214. ErrorHandler::handleException($error);
  215. $result = ob_get_clean();
  216. $this->assertEquals('Rendered by test plugin', $result);
  217. CakePlugin::unload();
  218. }
  219. /**
  220. * test handleFatalError generating a page.
  221. *
  222. * @return void
  223. */
  224. public function testHandleFatalErrorPage() {
  225. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  226. $originalDebugLevel = Configure::read('debug');
  227. $line = __LINE__;
  228. ob_start();
  229. Configure::write('debug', 1);
  230. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  231. $result = ob_get_clean();
  232. $this->assertContains('Something wrong', $result, 'message missing.');
  233. $this->assertContains(__FILE__, $result, 'filename missing.');
  234. $this->assertContains((string)$line, $result, 'line missing.');
  235. ob_start();
  236. Configure::write('debug', 0);
  237. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  238. $result = ob_get_clean();
  239. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  240. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  241. $this->assertContains('An Internal Error Has Occurred', $result);
  242. Configure::write('debug', $originalDebugLevel);
  243. }
  244. /**
  245. * test handleException generating log.
  246. *
  247. * @return void
  248. */
  249. public function testHandleFatalErrorLog() {
  250. $this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
  251. if (file_exists(LOGS . 'error.log')) {
  252. unlink(LOGS . 'error.log');
  253. }
  254. ob_start();
  255. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  256. ob_clean();
  257. $log = file(LOGS . 'error.log');
  258. $this->assertContains(__FILE__, $log[0], 'missing filename');
  259. $this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
  260. }
  261. }