Browse Source

Implement assertResponseXX methods.

Add positive tests for assertResponseXX methods, the negative tests seem
less useful right now.
mark_story 11 years ago
parent
commit
7038e6acda

+ 44 - 0
src/TestSuite/IntegrationTestCase.php

@@ -237,4 +237,48 @@ class IntegrationTestCase extends TestCase {
 		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 - 0
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 @@ class IntegrationTestCaseTest extends IntegrationTestCase {
 		$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();
+	}
+
 }