Browse Source

Merge pull request #5461 from cakephp/3.0-testing

Add assertNoRedirect method.
Mark Story 11 years ago
parent
commit
16c36b1143

+ 20 - 0
src/TestSuite/IntegrationTestCase.php

@@ -437,6 +437,26 @@ abstract class IntegrationTestCase extends TestCase {
 	}
 
 /**
+ * Asserts that the Location header is not set.
+ *
+ * @param string $message The failure message that will be appended to the generated message.
+ * @return void
+ */
+	public function assertNoRedirect($message = '') {
+		if (!$this->_response) {
+			$this->fail('No response set, cannot assert location header. ' . $message);
+		}
+		$result = $this->_response->header();
+		if (!$message) {
+			$message = 'Redirect header set';
+		}
+		if (!empty($result['Location'])) {
+			$message .= ': ' . $result['Location'];
+		}
+		$this->assertTrue(empty($result['Location']), $message);
+	}
+
+/**
  * Asserts response headers
  *
  * @param string $header The header to check

+ 1 - 1
tests/Fixture/AssertHtmlTestCase.php

@@ -4,7 +4,7 @@ namespace Cake\Test\Fixture;
 use Cake\TestSuite\TestCase;
 
 /**
- * This class helps in indirectly testing the functionalities of CakeTestCase::assertHtml
+ * This class helps in indirectly testing the functionalities of TestCase::assertHtml
  *
  */
 class AssertHtmlTestCase extends TestCase {

+ 25 - 0
tests/Fixture/AssertIntegrationTestCase.php

@@ -0,0 +1,25 @@
+<?php
+namespace Cake\Test\Fixture;
+
+use Cake\Network\Response;
+use Cake\TestSuite\IntegrationTestCase;
+
+/**
+ * This class helps in indirectly testing the functionalities of IntegrationTestCase
+ *
+ */
+class AssertIntegrationTestCase extends IntegrationTestCase {
+
+/**
+ * testBadAssertNoRedirect
+ *
+ * @return void
+ */
+	public function testBadAssertNoRedirect() {
+		$this->_response = new Response();
+		$this->_response->header('Location', 'http://localhost/tasks/index');
+
+		$this->assertNoRedirect();
+	}
+
+}

+ 25 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -20,6 +20,7 @@ use Cake\Network\Response;
 use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Router;
 use Cake\TestSuite\IntegrationTestCase;
+use Cake\Test\Fixture\AssertIntegrationTestCase;
 
 /**
  * Self test of the IntegrationTestCase
@@ -172,6 +173,30 @@ class IntegrationTestCaseTest extends IntegrationTestCase {
 	}
 
 /**
+ * Test the location header assertion.
+ *
+ * @return void
+ */
+	public function testAssertNoRedirect() {
+		$this->_response = new Response();
+
+		$this->assertNoRedirect();
+	}
+
+/**
+ * Test the location header assertion.
+ *
+ * @return void
+ */
+	public function testAssertNoRedirectFail() {
+		$test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
+		$result = $test->run();
+		ob_start();
+		$this->assertFalse($result->wasSuccessful());
+		$this->assertEquals(1, $result->failureCount());
+	}
+
+/**
  * Test the header assertion.
  *
  * @return void