Permalink
Browse files

Make exception rendererer more resiliant

Exceptions in beforeRender() should render correct error pages.

Fixes #3235
  • Loading branch information...
1 parent 99edef0 commit 393849a9bac3bae94cc91be73e1cb8df7e9d9276 @markstory markstory committed Sep 28, 2012
Showing with 42 additions and 19 deletions.
  1. +2 −2 lib/Cake/Error/ExceptionRenderer.php
  2. +40 −17 lib/Cake/Test/Case/Error/ExceptionRendererTest.php
@@ -290,11 +290,11 @@ protected function _outputMessageSafe($template) {
$this->controller->layoutPath = null;
$this->controller->subDir = null;
$this->controller->viewPath = 'Errors/';
- $this->controller->viewClass = 'View';
$this->controller->layout = 'error';
$this->controller->helpers = array('Form', 'Html', 'Session');
- $this->controller->render($template);
+ $view = new View($this->controller);
+ $this->controller->response->body($view->render($template, 'error'));
$this->controller->response->type('html');
$this->controller->response->send();
}
@@ -670,26 +670,50 @@ public function testMissingRenderSafe() {
$exception = new MissingHelperException(array('class' => 'Fail'));
$ExceptionRenderer = new ExceptionRenderer($exception);
- $ExceptionRenderer->controller = $this->getMock('Controller');
+ $ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
- $ExceptionRenderer->controller->expects($this->at(2))
+ $ExceptionRenderer->controller->expects($this->at(0))
->method('render')
->with('missingHelper')
->will($this->throwException($exception));
- $ExceptionRenderer->controller->expects($this->at(4))
- ->method('render')
- ->with('error500')
- ->will($this->returnValue(true));
+ $response = $this->getMock('CakeResponse');
+ $response->expects($this->once())
+ ->method('body')
+ ->with($this->stringContains('Helper class Fail'));
- $ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
+ $ExceptionRenderer->controller->response = $response;
$ExceptionRenderer->render();
sort($ExceptionRenderer->controller->helpers);
$this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
}
/**
+ * Test that exceptions in beforeRender() are handled by outputMessageSafe
+ *
+ * @return void
+ */
+ public function testRenderExceptionInBeforeRender() {
+ $exception = new NotFoundException('Not there, sorry');
+ $ExceptionRenderer = new ExceptionRenderer($exception);
+
+ $ExceptionRenderer->controller = $this->getMock('Controller', array('beforeRender'));
+ $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
+ $ExceptionRenderer->controller->expects($this->any())
+ ->method('beforeRender')
+ ->will($this->throwException($exception));
+
+ $response = $this->getMock('CakeResponse');
+ $response->expects($this->once())
+ ->method('body')
+ ->with($this->stringContains('Not there, sorry'));
+
+ $ExceptionRenderer->controller->response = $response;
+ $ExceptionRenderer->render();
+ }
+
+/**
* Test that missing subDir/layoutPath don't cause other fatal errors.
*
* @return void
@@ -698,32 +722,31 @@ public function testMissingSubdirRenderSafe() {
$exception = new NotFoundException();
$ExceptionRenderer = new ExceptionRenderer($exception);
- $ExceptionRenderer->controller = $this->getMock('Controller');
+ $ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->layoutPath = 'json';
$ExceptionRenderer->controller->subDir = 'json';
$ExceptionRenderer->controller->viewClass = 'Json';
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
- $ExceptionRenderer->controller->expects($this->at(1))
+ $ExceptionRenderer->controller->expects($this->once())
->method('render')
->with('error400')
->will($this->throwException($exception));
- $ExceptionRenderer->controller->expects($this->at(3))
- ->method('render')
- ->with('error500')
- ->will($this->returnValue(true));
-
- $ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
- $ExceptionRenderer->controller->response->expects($this->once())
+ $response = $this->getMock('CakeResponse');
+ $response->expects($this->once())
+ ->method('body')
+ ->with($this->stringContains('Not Found'));
+ $response->expects($this->once())
->method('type')
->with('html');
+ $ExceptionRenderer->controller->response = $response;
+
$ExceptionRenderer->render();
$this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
$this->assertEquals('', $ExceptionRenderer->controller->subDir);
- $this->assertEquals('View', $ExceptionRenderer->controller->viewClass);
$this->assertEquals('Errors/', $ExceptionRenderer->controller->viewPath);
}

0 comments on commit 393849a

Please sign in to comment.