Skip to content
Permalink
Browse files

Add exception handling to IntegrationTestCase.

Rendering exceptions is part of the 'integration' aspect of integration
testing.
  • Loading branch information...
markstory committed Sep 3, 2014
1 parent 00ed67e commit 45b93ef78e72c34d4ead6dba976cebaa4a9d1ee3
@@ -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();
}
/**
@@ -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');
@@ -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
*
@@ -13,6 +13,8 @@
*/
namespace TestApp\Controller;
use Cake\Network\Exception\NotFoundException;
/**
* RequestActionController class
*
@@ -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.
You can’t perform that action at this time.