Skip to content

Commit

Permalink
Add exception handling to IntegrationTestCase.
Browse files Browse the repository at this point in the history
Rendering exceptions is part of the 'integration' aspect of integration
testing.
  • Loading branch information
markstory committed Sep 3, 2014
1 parent 00ed67e commit 45b93ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -212,8 +212,33 @@ protected function _sendRequest($url, $method, $data = null) {
$request = $this->_buildRequest($url, $method, $data);
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->dispatch($request, $response);
$this->_response = $response;
try {
$dispatcher->dispatch($request, $response);
$this->_response = $response;
} catch (\PHPUnit_Exception $e) {
throw $e;
} catch (\Exception $e) {
$this->_handleError($e);
}
}

/**
* Attempt to render an error response for a given exception.
*
* This method will attempt to use the configured exception renderer.
* If that class does not exist, the built-in renderer will be used.
*
* @param \Exception $exception Exception to handle.
* @return void
* @throws \Exception
*/
protected function _handleError($exception) {
$class = Configure::read('Error.exceptionRenderer');
if (empty($class) || !class_exists($class)) {
$class = 'Cake\Error\ExceptionRenderer';
}
$instance = new $class($exception);
$this->_response = $instance->render();
}

/**
Expand Down
11 changes: 10 additions & 1 deletion tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -66,7 +66,7 @@ public function testRequestBuilding() {
*
* @return void
*/
public function testSendingGet() {
public function testGet() {
$this->assertNull($this->_response);

$this->get('/request_action/test_request_action');
Expand All @@ -75,6 +75,15 @@ public function testSendingGet() {
$this->assertEquals('This is a test', $this->_response->body());
}

/**
*
*/
public function testPostAndErrorHandling() {
$this->post('/request_action/error_method');
$this->assertResponseContains('Not there or here');
$this->assertResponseContains('<!DOCTYPE html>');
}

/**
* Test the responseOk status assertion
*
Expand Down
12 changes: 12 additions & 0 deletions tests/test_app/TestApp/Controller/RequestActionController.php
Expand Up @@ -13,6 +13,8 @@
*/
namespace TestApp\Controller;

use Cake\Network\Exception\NotFoundException;

/**
* RequestActionController class
*
Expand Down Expand Up @@ -133,4 +135,14 @@ public function session_test() {
return $this->response;
}

/**
* Tests exception handling
*
* @throws \Cake\Network\Exception\NotFoundException
* @return void
*/
public function error_method() {
throw new NotFoundException('Not there or here.');
}

}

0 comments on commit 45b93ef

Please sign in to comment.