PageRenderTime 67ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/toolbag/cake-serialized-feeds-example
PHP | 799 lines | 477 code | 104 blank | 218 comment | 1 complexity | 0e5e348202e2e86b90e0da12c78af31d MD5 | raw file
  1. <?php
  2. /**
  3. * ExceptionRendererTest 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 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ExceptionRenderer', 'Error');
  20. App::uses('Controller', 'Controller');
  21. App::uses('AppController', 'Controller');
  22. App::uses('Component', 'Controller');
  23. App::uses('Router', 'Routing');
  24. /**
  25. * Short description for class.
  26. *
  27. * @package Cake.Test.Case.Error
  28. */
  29. class AuthBlueberryUser extends CakeTestModel {
  30. /**
  31. * name property
  32. *
  33. * @var string 'AuthBlueberryUser'
  34. */
  35. public $name = 'AuthBlueberryUser';
  36. /**
  37. * useTable property
  38. *
  39. * @var string
  40. */
  41. public $useTable = false;
  42. }
  43. /**
  44. * BlueberryComponent class
  45. *
  46. * @package Cake.Test.Case.Error
  47. */
  48. class BlueberryComponent extends Component {
  49. /**
  50. * testName property
  51. *
  52. * @return void
  53. */
  54. public $testName = null;
  55. /**
  56. * initialize method
  57. *
  58. * @return void
  59. */
  60. public function initialize(Controller $controller) {
  61. $this->testName = 'BlueberryComponent';
  62. }
  63. }
  64. /**
  65. * TestErrorController class
  66. *
  67. * @package Cake.Test.Case.Error
  68. */
  69. class TestErrorController extends Controller {
  70. /**
  71. * uses property
  72. *
  73. * @var array
  74. */
  75. public $uses = array();
  76. /**
  77. * components property
  78. *
  79. * @return void
  80. */
  81. public $components = array('Blueberry');
  82. /**
  83. * beforeRender method
  84. *
  85. * @return void
  86. */
  87. public function beforeRender() {
  88. echo $this->Blueberry->testName;
  89. }
  90. /**
  91. * index method
  92. *
  93. * @return void
  94. */
  95. public function index() {
  96. $this->autoRender = false;
  97. return 'what up';
  98. }
  99. }
  100. /**
  101. * MyCustomExceptionRenderer class
  102. *
  103. * @package Cake.Test.Case.Error
  104. */
  105. class MyCustomExceptionRenderer extends ExceptionRenderer {
  106. /**
  107. * custom error message type.
  108. *
  109. * @return void
  110. */
  111. public function missingWidgetThing() {
  112. echo 'widget thing is missing';
  113. }
  114. }
  115. /**
  116. * Exception class for testing app error handlers and custom errors.
  117. *
  118. * @package Cake.Test.Case.Error
  119. */
  120. class MissingWidgetThingException extends NotFoundException {
  121. }
  122. /**
  123. * ExceptionRendererTest class
  124. *
  125. * @package Cake.Test.Case.Error
  126. */
  127. class ExceptionRendererTest extends CakeTestCase {
  128. protected $_restoreError = false;
  129. /**
  130. * setup create a request object to get out of router later.
  131. *
  132. * @return void
  133. */
  134. public function setUp() {
  135. parent::setUp();
  136. App::build(array(
  137. 'View' => array(
  138. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  139. )
  140. ), App::RESET);
  141. Router::reload();
  142. $request = new CakeRequest(null, false);
  143. $request->base = '';
  144. Router::setRequestInfo($request);
  145. Configure::write('debug', 2);
  146. }
  147. /**
  148. * tearDown
  149. *
  150. * @return void
  151. */
  152. public function tearDown() {
  153. parent::tearDown();
  154. if ($this->_restoreError) {
  155. restore_error_handler();
  156. }
  157. }
  158. /**
  159. * Mocks out the response on the ExceptionRenderer object so headers aren't modified.
  160. *
  161. * @return void
  162. */
  163. protected function _mockResponse($error) {
  164. $error->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  165. return $error;
  166. }
  167. /**
  168. * test that methods declared in an ExceptionRenderer subclass are not converted
  169. * into error400 when debug > 0
  170. *
  171. * @return void
  172. */
  173. public function testSubclassMethodsNotBeingConvertedToError() {
  174. Configure::write('debug', 2);
  175. $exception = new MissingWidgetThingException('Widget not found');
  176. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  177. ob_start();
  178. $ExceptionRenderer->render();
  179. $result = ob_get_clean();
  180. $this->assertEquals('widget thing is missing', $result);
  181. }
  182. /**
  183. * test that subclass methods are not converted when debug = 0
  184. *
  185. * @return void
  186. */
  187. public function testSubclassMethodsNotBeingConvertedDebug0() {
  188. Configure::write('debug', 0);
  189. $exception = new MissingWidgetThingException('Widget not found');
  190. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  191. $this->assertEquals('missingWidgetThing', $ExceptionRenderer->method);
  192. ob_start();
  193. $ExceptionRenderer->render();
  194. $result = ob_get_clean();
  195. $this->assertEquals('widget thing is missing', $result, 'Method declared in subclass converted to error400');
  196. }
  197. /**
  198. * test that ExceptionRenderer subclasses properly convert framework errors.
  199. *
  200. * @return void
  201. */
  202. public function testSubclassConvertingFrameworkErrors() {
  203. Configure::write('debug', 0);
  204. $exception = new MissingControllerException('PostsController');
  205. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  206. $this->assertEquals('error400', $ExceptionRenderer->method);
  207. ob_start();
  208. $ExceptionRenderer->render();
  209. $result = ob_get_clean();
  210. $this->assertRegExp('/Not Found/', $result, 'Method declared in error handler not converted to error400. %s');
  211. }
  212. /**
  213. * test things in the constructor.
  214. *
  215. * @return void
  216. */
  217. public function testConstruction() {
  218. $exception = new NotFoundException('Page not found');
  219. $ExceptionRenderer = new ExceptionRenderer($exception);
  220. $this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
  221. $this->assertEquals('error400', $ExceptionRenderer->method);
  222. $this->assertEquals($exception, $ExceptionRenderer->error);
  223. }
  224. /**
  225. * test that method gets coerced when debug = 0
  226. *
  227. * @return void
  228. */
  229. public function testErrorMethodCoercion() {
  230. Configure::write('debug', 0);
  231. $exception = new MissingActionException('Page not found');
  232. $ExceptionRenderer = new ExceptionRenderer($exception);
  233. $this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
  234. $this->assertEquals('error400', $ExceptionRenderer->method);
  235. $this->assertEquals($exception, $ExceptionRenderer->error);
  236. }
  237. /**
  238. * test that helpers in custom CakeErrorController are not lost
  239. */
  240. public function testCakeErrorHelpersNotLost() {
  241. $testApp = CAKE . 'Test' . DS . 'test_app' . DS;
  242. App::build(array(
  243. 'Controller' => array(
  244. $testApp . 'Controller' . DS
  245. ),
  246. 'View/Helper' => array(
  247. $testApp . 'View' . DS . 'Helper' . DS
  248. ),
  249. 'View/Layouts' => array(
  250. $testApp . 'View' . DS . 'Layouts' . DS
  251. ),
  252. 'Error' => array(
  253. $testApp . 'Error' . DS
  254. ),
  255. ), App::RESET);
  256. App::uses('TestAppsExceptionRenderer', 'Error');
  257. $exception = new SocketException('socket exception');
  258. $renderer = new TestAppsExceptionRenderer($exception);
  259. ob_start();
  260. $renderer->render();
  261. $result = ob_get_clean();
  262. $this->assertContains('<b>peeled</b>', $result);
  263. }
  264. /**
  265. * test that unknown exception types with valid status codes are treated correctly.
  266. *
  267. * @return void
  268. */
  269. public function testUnknownExceptionTypeWithExceptionThatHasA400Code() {
  270. $exception = new MissingWidgetThingException('coding fail.');
  271. $ExceptionRenderer = new ExceptionRenderer($exception);
  272. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  273. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
  274. ob_start();
  275. $ExceptionRenderer->render();
  276. $result = ob_get_clean();
  277. $this->assertFalse(method_exists($ExceptionRenderer, 'missingWidgetThing'), 'no method should exist.');
  278. $this->assertEquals('error400', $ExceptionRenderer->method, 'incorrect method coercion.');
  279. $this->assertContains('coding fail', $result, 'Text should show up.');
  280. }
  281. /**
  282. * test that unknown exception types with valid status codes are treated correctly.
  283. *
  284. * @return void
  285. */
  286. public function testUnknownExceptionTypeWithNoCodeIsA500() {
  287. $exception = new OutOfBoundsException('foul ball.');
  288. $ExceptionRenderer = new ExceptionRenderer($exception);
  289. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  290. $ExceptionRenderer->controller->response->expects($this->once())
  291. ->method('statusCode')
  292. ->with(500);
  293. ob_start();
  294. $ExceptionRenderer->render();
  295. $result = ob_get_clean();
  296. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  297. $this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
  298. }
  299. /**
  300. * test that unknown exceptions have messages ignored.
  301. *
  302. * @return void
  303. */
  304. public function testUnknownExceptionInProduction() {
  305. Configure::write('debug', 0);
  306. $exception = new OutOfBoundsException('foul ball.');
  307. $ExceptionRenderer = new ExceptionRenderer($exception);
  308. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  309. $ExceptionRenderer->controller->response->expects($this->once())
  310. ->method('statusCode')
  311. ->with(500);
  312. ob_start();
  313. $ExceptionRenderer->render();
  314. $result = ob_get_clean();
  315. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  316. $this->assertNotContains('foul ball.', $result, 'Text should no show up.');
  317. $this->assertContains('Internal Error', $result, 'Generic message only.');
  318. }
  319. /**
  320. * test that unknown exception types with valid status codes are treated correctly.
  321. *
  322. * @return void
  323. */
  324. public function testUnknownExceptionTypeWithCodeHigherThan500() {
  325. $exception = new OutOfBoundsException('foul ball.', 501);
  326. $ExceptionRenderer = new ExceptionRenderer($exception);
  327. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  328. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(501);
  329. ob_start();
  330. $ExceptionRenderer->render();
  331. $result = ob_get_clean();
  332. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  333. $this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
  334. }
  335. /**
  336. * testerror400 method
  337. *
  338. * @return void
  339. */
  340. public function testError400() {
  341. Router::reload();
  342. $request = new CakeRequest('posts/view/1000', false);
  343. Router::setRequestInfo($request);
  344. $exception = new NotFoundException('Custom message');
  345. $ExceptionRenderer = new ExceptionRenderer($exception);
  346. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  347. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
  348. ob_start();
  349. $ExceptionRenderer->render();
  350. $result = ob_get_clean();
  351. $this->assertRegExp('/<h2>Custom message<\/h2>/', $result);
  352. $this->assertRegExp("/<strong>'.*?\/posts\/view\/1000'<\/strong>/", $result);
  353. }
  354. /**
  355. * test that error400 only modifies the messages on CakeExceptions.
  356. *
  357. * @return void
  358. */
  359. public function testerror400OnlyChangingCakeException() {
  360. Configure::write('debug', 0);
  361. $exception = new NotFoundException('Custom message');
  362. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  363. ob_start();
  364. $ExceptionRenderer->render();
  365. $result = ob_get_clean();
  366. $this->assertContains('Custom message', $result);
  367. $exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
  368. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  369. ob_start();
  370. $ExceptionRenderer->render();
  371. $result = ob_get_clean();
  372. $this->assertContains('Not Found', $result);
  373. }
  374. /**
  375. * test that error400 doesn't expose XSS
  376. *
  377. * @return void
  378. */
  379. public function testError400NoInjection() {
  380. Router::reload();
  381. $request = new CakeRequest('pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>', false);
  382. Router::setRequestInfo($request);
  383. $exception = new NotFoundException('Custom message');
  384. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  385. ob_start();
  386. $ExceptionRenderer->render();
  387. $result = ob_get_clean();
  388. $this->assertNotRegExp('#<script>document#', $result);
  389. $this->assertNotRegExp('#alert\(t\);</script>#', $result);
  390. }
  391. /**
  392. * testError500 method
  393. *
  394. * @return void
  395. */
  396. public function testError500Message() {
  397. $exception = new InternalErrorException('An Internal Error Has Occurred');
  398. $ExceptionRenderer = new ExceptionRenderer($exception);
  399. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  400. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
  401. ob_start();
  402. $ExceptionRenderer->render();
  403. $result = ob_get_clean();
  404. $this->assertRegExp('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  405. }
  406. /**
  407. * testMissingController method
  408. *
  409. * @return void
  410. */
  411. public function testMissingController() {
  412. $exception = new MissingControllerException(array('class' => 'PostsController'));
  413. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  414. ob_start();
  415. $ExceptionRenderer->render();
  416. $result = ob_get_clean();
  417. $this->assertRegExp('/<h2>Missing Controller<\/h2>/', $result);
  418. $this->assertRegExp('/<em>PostsController<\/em>/', $result);
  419. }
  420. /**
  421. * Returns an array of tests to run for the various CakeException classes.
  422. *
  423. * @return void
  424. */
  425. public static function testProvider() {
  426. return array(
  427. array(
  428. new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')),
  429. array(
  430. '/<h2>Missing Method in PostsController<\/h2>/',
  431. '/<em>PostsController::<\/em><em>index\(\)<\/em>/'
  432. ),
  433. 404
  434. ),
  435. array(
  436. new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce')),
  437. array(
  438. '/<h2>Private Method in PostsController<\/h2>/',
  439. '/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
  440. ),
  441. 404
  442. ),
  443. array(
  444. new MissingTableException(array('table' => 'articles', 'class' => 'Article', 'ds' => 'test')),
  445. array(
  446. '/<h2>Missing Database Table<\/h2>/',
  447. '/Table <em>articles<\/em> for model <em>Article<\/em> was not found in datasource <em>test<\/em>/'
  448. ),
  449. 500
  450. ),
  451. array(
  452. new MissingDatabaseException(array('connection' => 'default')),
  453. array(
  454. '/<h2>Missing Database Connection<\/h2>/',
  455. '/Confirm you have created the file/'
  456. ),
  457. 500
  458. ),
  459. array(
  460. new MissingViewException(array('file' => '/posts/about.ctp')),
  461. array(
  462. "/posts\/about.ctp/"
  463. ),
  464. 500
  465. ),
  466. array(
  467. new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
  468. array(
  469. "/Missing Layout/",
  470. "/layouts\/my_layout.ctp/"
  471. ),
  472. 500
  473. ),
  474. array(
  475. new MissingConnectionException(array('class' => 'Mysql')),
  476. array(
  477. '/<h2>Missing Database Connection<\/h2>/',
  478. '/A Database connection using "Mysql" was missing or unable to connect./',
  479. ),
  480. 500
  481. ),
  482. array(
  483. new MissingConnectionException(array('class' => 'Mysql', 'enabled' => false)),
  484. array(
  485. '/<h2>Missing Database Connection<\/h2>/',
  486. '/A Database connection using "Mysql" was missing or unable to connect./',
  487. '/Mysql driver is NOT enabled/'
  488. ),
  489. 500
  490. ),
  491. array(
  492. new MissingDatasourceConfigException(array('config' => 'default')),
  493. array(
  494. '/<h2>Missing Datasource Configuration<\/h2>/',
  495. '/The datasource configuration <em>default<\/em> was not found in database.php/'
  496. ),
  497. 500
  498. ),
  499. array(
  500. new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
  501. array(
  502. '/<h2>Missing Datasource<\/h2>/',
  503. '/Datasource class <em>MyPlugin.MyDatasource<\/em> could not be found/'
  504. ),
  505. 500
  506. ),
  507. array(
  508. new MissingHelperException(array('class' => 'MyCustomHelper')),
  509. array(
  510. '/<h2>Missing Helper<\/h2>/',
  511. '/<em>MyCustomHelper<\/em> could not be found./',
  512. '/Create the class <em>MyCustomHelper<\/em> below in file:/',
  513. '/(\/|\\\)MyCustomHelper.php/'
  514. ),
  515. 500
  516. ),
  517. array(
  518. new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
  519. array(
  520. '/<h2>Missing Behavior<\/h2>/',
  521. '/Create the class <em>MyCustomBehavior<\/em> below in file:/',
  522. '/(\/|\\\)MyCustomBehavior.php/'
  523. ),
  524. 500
  525. ),
  526. array(
  527. new MissingComponentException(array('class' => 'SideboxComponent')),
  528. array(
  529. '/<h2>Missing Component<\/h2>/',
  530. '/Create the class <em>SideboxComponent<\/em> below in file:/',
  531. '/(\/|\\\)SideboxComponent.php/'
  532. ),
  533. 500
  534. ),
  535. array(
  536. new Exception('boom'),
  537. array(
  538. '/Internal Error/'
  539. ),
  540. 500
  541. ),
  542. array(
  543. new RuntimeException('another boom'),
  544. array(
  545. '/Internal Error/'
  546. ),
  547. 500
  548. ),
  549. array(
  550. new CakeException('base class'),
  551. array('/Internal Error/'),
  552. 500
  553. ),
  554. array(
  555. new ConfigureException('No file'),
  556. array('/Internal Error/'),
  557. 500
  558. )
  559. );
  560. }
  561. /**
  562. * Test the various CakeException sub classes
  563. *
  564. * @dataProvider testProvider
  565. * @return void
  566. */
  567. public function testCakeExceptionHandling($exception, $patterns, $code) {
  568. $ExceptionRenderer = new ExceptionRenderer($exception);
  569. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  570. $ExceptionRenderer->controller->response->expects($this->once())
  571. ->method('statusCode')
  572. ->with($code);
  573. ob_start();
  574. $ExceptionRenderer->render();
  575. $result = ob_get_clean();
  576. foreach ($patterns as $pattern) {
  577. $this->assertRegExp($pattern, $result);
  578. }
  579. }
  580. /**
  581. * Test exceptions being raised when helpers are missing.
  582. *
  583. * @return void
  584. */
  585. public function testMissingRenderSafe() {
  586. $exception = new MissingHelperException(array('class' => 'Fail'));
  587. $ExceptionRenderer = new ExceptionRenderer($exception);
  588. $ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
  589. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  590. $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
  591. $ExceptionRenderer->controller->expects($this->at(0))
  592. ->method('render')
  593. ->with('missingHelper')
  594. ->will($this->throwException($exception));
  595. $response = $this->getMock('CakeResponse');
  596. $response->expects($this->once())
  597. ->method('body')
  598. ->with($this->stringContains('Helper class Fail'));
  599. $ExceptionRenderer->controller->response = $response;
  600. $ExceptionRenderer->render();
  601. sort($ExceptionRenderer->controller->helpers);
  602. $this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
  603. }
  604. /**
  605. * Test that exceptions in beforeRender() are handled by outputMessageSafe
  606. *
  607. * @return void
  608. */
  609. public function testRenderExceptionInBeforeRender() {
  610. $exception = new NotFoundException('Not there, sorry');
  611. $ExceptionRenderer = new ExceptionRenderer($exception);
  612. $ExceptionRenderer->controller = $this->getMock('Controller', array('beforeRender'));
  613. $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
  614. $ExceptionRenderer->controller->expects($this->any())
  615. ->method('beforeRender')
  616. ->will($this->throwException($exception));
  617. $response = $this->getMock('CakeResponse');
  618. $response->expects($this->once())
  619. ->method('body')
  620. ->with($this->stringContains('Not there, sorry'));
  621. $ExceptionRenderer->controller->response = $response;
  622. $ExceptionRenderer->render();
  623. }
  624. /**
  625. * Test that missing subDir/layoutPath don't cause other fatal errors.
  626. *
  627. * @return void
  628. */
  629. public function testMissingSubdirRenderSafe() {
  630. $exception = new NotFoundException();
  631. $ExceptionRenderer = new ExceptionRenderer($exception);
  632. $ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
  633. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  634. $ExceptionRenderer->controller->layoutPath = 'json';
  635. $ExceptionRenderer->controller->subDir = 'json';
  636. $ExceptionRenderer->controller->viewClass = 'Json';
  637. $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
  638. $ExceptionRenderer->controller->expects($this->once())
  639. ->method('render')
  640. ->with('error400')
  641. ->will($this->throwException($exception));
  642. $response = $this->getMock('CakeResponse');
  643. $response->expects($this->once())
  644. ->method('body')
  645. ->with($this->stringContains('Not Found'));
  646. $response->expects($this->once())
  647. ->method('type')
  648. ->with('html');
  649. $ExceptionRenderer->controller->response = $response;
  650. $ExceptionRenderer->render();
  651. $this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
  652. $this->assertEquals('', $ExceptionRenderer->controller->subDir);
  653. $this->assertEquals('Errors/', $ExceptionRenderer->controller->viewPath);
  654. }
  655. /**
  656. * Test that exceptions can be rendered when an request hasn't been registered
  657. * with Router
  658. *
  659. * @return void
  660. */
  661. public function testRenderWithNoRequest() {
  662. Router::reload();
  663. $this->assertNull(Router::getRequest(false));
  664. $exception = new Exception('Terrible');
  665. $ExceptionRenderer = new ExceptionRenderer($exception);
  666. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  667. $ExceptionRenderer->controller->response->expects($this->once())
  668. ->method('statusCode')
  669. ->with(500);
  670. ob_start();
  671. $ExceptionRenderer->render();
  672. $result = ob_get_clean();
  673. $this->assertContains('Internal Error', $result);
  674. }
  675. /**
  676. * Tests the output of rendering a PDOException
  677. *
  678. * @return void
  679. */
  680. public function testPDOException() {
  681. $exception = new PDOException('There was an error in the SQL query');
  682. $exception->queryString = 'SELECT * from poo_query < 5 and :seven';
  683. $exception->params = array('seven' => 7);
  684. $ExceptionRenderer = new ExceptionRenderer($exception);
  685. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  686. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
  687. ob_start();
  688. $ExceptionRenderer->render();
  689. $result = ob_get_clean();
  690. $this->assertContains('<h2>Database Error</h2>', $result);
  691. $this->assertContains('There was an error in the SQL query', $result);
  692. $this->assertContains('SELECT * from poo_query < 5 and :seven', $result);
  693. $this->assertContains("'seven' => (int) 7", $result);
  694. }
  695. }