Skip to content

Commit

Permalink
Implement assertResponseXX methods.
Browse files Browse the repository at this point in the history
Add positive tests for assertResponseXX methods, the negative tests seem
less useful right now.
  • Loading branch information
markstory committed Sep 2, 2014
1 parent 210fd89 commit 7038e6a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -237,4 +237,48 @@ protected function _buildRequest($url, $method, $data) {
return new Request($props);
}

/**
* Assert that the response status code is in the 2xx range.
*
* @return void
*/
public function assertResponseOk() {
$this->_assertStatus(200, 204, 'Status code is not between 200 and 204');
}

/**
* Assert that the response status code is in the 4xx range.
*
* @return void
*/
public function assertResponseError() {
$this->_assertStatus(400, 417, 'Status code is not between 400 and 417');
}

/**
* Assert that the response status code is in the 5xx range.
*
* @return void
*/
public function assertResponseFailure() {
$this->_assertStatus(500, 505, 'Status code is not between 500 and 505');
}

/**
* Helper method for status assertions.
*
* @param int $min Min status code.
* @param int $max Max status code.
* @param string $message The error message.
* @return void
*/
protected function _assertStatus($min, $max, $message) {
if (!$this->_response) {
$this->fail('No response set, cannot assert status code.');
}
$status = $this->_response->statusCode();
$this->assertGreaterThanOrEqual($min, $status, $message);
$this->assertLessThanOrEqual($max, $status, $message);
}

}
31 changes: 31 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\TestSuite;

use Cake\Core\Configure;
use Cake\Network\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestCase;
Expand Down Expand Up @@ -74,4 +75,34 @@ public function testSendingGet() {
$this->assertEquals('This is a test', $this->_response->body());
}

/**
* Test the responseOk status assertion
*
* @return void
*/
public function testAssertResponseStatusCodes() {
$this->_response = new Response();

$this->_response->statusCode(200);
$this->assertResponseOk();

$this->_response->statusCode(201);
$this->assertResponseOk();

$this->_response->statusCode(204);
$this->assertResponseOk();

$this->_response->statusCode(400);
$this->assertResponseError();

$this->_response->statusCode(417);
$this->assertResponseError();

$this->_response->statusCode(500);
$this->assertResponseFailure();

$this->_response->statusCode(505);
$this->assertResponseFailure();
}

}

0 comments on commit 7038e6a

Please sign in to comment.