Browse Source

Merge pull request #12464 from cakephp/issue-12447

Add assertRedirectNotContains & assertHeaderNotContains
Mark Story 7 years ago
parent
commit
d26203bf63

+ 44 - 0
src/TestSuite/Constraint/Response/HeaderNotContains.php

@@ -0,0 +1,44 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @since         3.7.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Constraint\Response;
+
+/**
+ * Constraint for ensuring a header does not contain a value.
+ *
+ * @internal
+ */
+class HeaderNotContains extends HeaderContains
+{
+
+    /**
+     * Checks assertion
+     *
+     * @param mixed $other Expected content
+     * @return bool
+     */
+    public function matches($other)
+    {
+        return parent::matches($other) === false;
+    }
+
+    /**
+     * Assertion message
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return sprintf("is not in header '%s'", $this->headerName);
+    }
+}

+ 28 - 0
src/TestSuite/IntegrationTestTrait.php

@@ -35,6 +35,7 @@ use Cake\TestSuite\Constraint\Response\FileSent;
 use Cake\TestSuite\Constraint\Response\FileSentAs;
 use Cake\TestSuite\Constraint\Response\HeaderContains;
 use Cake\TestSuite\Constraint\Response\HeaderEquals;
+use Cake\TestSuite\Constraint\Response\HeaderNotContains;
 use Cake\TestSuite\Constraint\Response\HeaderNotSet;
 use Cake\TestSuite\Constraint\Response\HeaderSet;
 use Cake\TestSuite\Constraint\Response\StatusCode;
@@ -856,6 +857,19 @@ trait IntegrationTestTrait
     }
 
     /**
+     * Asserts that the Location header does not contain a substring
+     *
+     * @param string $url The URL you expected the client to go to.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertRedirectNotContains($url, $message = '')
+    {
+        $this->assertThat(null, new HeaderSet($this->_response, 'Location'), $message);
+        $this->assertThat($url, new HeaderNotContains($this->_response, 'Location'), $message);
+    }
+
+    /**
      * Asserts that the Location header is not set.
      *
      * @param string $message The failure message that will be appended to the generated message.
@@ -895,6 +909,20 @@ trait IntegrationTestTrait
     }
 
     /**
+     * Asserts response header does not contain a string
+     *
+     * @param string $header The header to check
+     * @param string $content The content to check for.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertHeaderNotContains($header, $content, $message = '')
+    {
+        $this->assertThat(null, new HeaderSet($this->_response, $header), $message);
+        $this->assertThat($content, new HeaderNotContains($this->_response, $header), $message);
+    }
+
+    /**
      * Asserts content type
      *
      * @param string $type The content-type to check for.

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

@@ -942,6 +942,18 @@ class IntegrationTestTraitTest extends IntegrationTestCase
     }
 
     /**
+     * Test the location header assertion string not contains
+     *
+     * @return void
+     */
+    public function testAssertRedirectNotContains()
+    {
+        $this->_response = new Response();
+        $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
+        $this->assertRedirectNotContains('test');
+    }
+
+    /**
      * Test the location header assertion.
      *
      * @return void
@@ -1007,6 +1019,19 @@ class IntegrationTestTraitTest extends IntegrationTestCase
     }
 
     /**
+     * Test the header not contains assertion.
+     *
+     * @return void
+     */
+    public function testAssertHeaderNotContains()
+    {
+        $this->_response = new Response();
+        $this->_response = $this->_response->withHeader('Etag', 'abc123');
+
+        $this->assertHeaderNotContains('Etag', 'xyz');
+    }
+
+    /**
      * Test the content type assertion.
      *
      * @return void