Browse Source

Merge pull request #8421 from cakephp/assert-no-cookie

Adding a assertCookieNotSet to the IntegrationTestCase
Mark Story 10 years ago
parent
commit
5a20418dbf

+ 16 - 0
src/TestSuite/IntegrationTestCase.php

@@ -861,6 +861,22 @@ abstract class IntegrationTestCase extends TestCase
     }
 
     /**
+     * Asserts a cookie has not been set in the response
+     *
+     * @param string $cookie The cookie name to check
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertCookieNotSet($cookie, $message = '')
+    {
+        if (!$this->_response) {
+            $this->fail('No response set, cannot assert cookies. ' . $message);
+        }
+
+        $this->assertCookie(null, $cookie, "Cookie '{$cookie}' has been set. " . $message);
+    }
+
+    /**
      * Asserts cookie values which are encrypted by the
      * CookieComponent.
      *

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

@@ -224,6 +224,34 @@ class IntegrationTestCaseTest extends IntegrationTestCase
 
         $this->assertSession('An error message', 'Flash.flash.0.message');
         $this->assertCookie(1, 'remember_me');
+        $this->assertCookieNotSet('user_id');
+    }
+
+    /**
+     * Tests the failure message for assertCookieNotSet
+     *
+     * @expectedException PHPUnit_Framework_AssertionFailedError
+     * @expectedExceptionMessage Cookie 'remember_me' has been set
+     * @return void
+     */
+    public function testCookieNotSetFailure()
+    {
+        $this->post('/posts/index');
+        $this->assertCookieNotSet('remember_me');
+    }
+
+
+    /**
+     * Tests the failure message for assertCookieNotSet when no
+     * response whas generated
+     *
+     * @expectedException PHPUnit_Framework_AssertionFailedError
+     * @expectedExceptionMessage No response set, cannot assert cookies.
+     * @return void
+     */
+    public function testCookieNotSetFailureNoResponse()
+    {
+        $this->assertCookieNotSet('remember_me');
     }
 
     /**