diff --git a/src/TestSuite/IntegrationTestCase.php b/src/TestSuite/IntegrationTestCase.php index e29640a561f..b0de7539e93 100644 --- a/src/TestSuite/IntegrationTestCase.php +++ b/src/TestSuite/IntegrationTestCase.php @@ -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); + } + } diff --git a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php index 97d0b49cb38..2613921220a 100644 --- a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php +++ b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php @@ -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; @@ -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(); + } + }