Browse Source

Add Cookie:getId()

Having identifiers will allow collections to replace cookie values
when updating collections from responses.
Mark Story 9 years ago
parent
commit
449a020123
2 changed files with 26 additions and 0 deletions
  1. 12 0
      src/Http/Cookie/Cookie.php
  2. 14 0
      tests/TestCase/Http/Cookie/CookieTest.php

+ 12 - 0
src/Http/Cookie/Cookie.php

@@ -212,6 +212,18 @@ class Cookie implements CookieInterface
     }
 
     /**
+     * Get the id for a cookie
+     *
+     * Cookies are unique across name, domain, path tuples.
+     *
+     * @return string
+     */
+    public function getId()
+    {
+        return "{$this->name};{$this->domain};{$this->path}";
+    }
+
+    /**
      * Gets the cookie name
      *
      * @return string

+ 14 - 0
tests/TestCase/Http/Cookie/CookieTest.php

@@ -504,4 +504,18 @@ class CookieTest extends TestCase
         $expected = '{"username":"florian","profile":{"profession":"developer"}}';
         $this->assertContains(urlencode($expected), $cookie->toHeaderValue());
     }
+
+    /**
+     * Tests getting the id
+     *
+     * @return void
+     */
+    public function testGetId()
+    {
+        $cookie = new Cookie('cakephp', 'cakephp-rocks');
+        $this->assertEquals('cakephp;;', $cookie->getId());
+
+        $cookie = new Cookie('test', 'val', null, '/path', 'example.com');
+        $this->assertEquals('test;example.com;/path', $cookie->getId());
+    }
 }