Browse Source

Add get() and has() to CookieCollection.

Also add missing tests for iterating the collection.
Mark Story 9 years ago
parent
commit
8890344f27

+ 32 - 0
src/Http/Cookie/CookieCollection.php

@@ -46,6 +46,38 @@ class CookieCollection implements IteratorAggregate
     }
 
     /**
+     * Get a cookie by name
+     *
+     * If the provided name matches a URL (matches `#^https?://#`) this method
+     * will assume you want a list of cookies that match that URL. This is
+     * backwards compatible behavior that will be removed in 4.0.0
+     *
+     * @param string $name The name of the cookie. If the name looks like a URL,
+     *  backwards compatible behavior will be used.
+     * @return \Cake\Http\Cookie\Cookie|null|array
+     */
+    public function get($name)
+    {
+        $key = mb_strtolower($name);
+        if (isset($this->cookies[$key])) {
+            return $this->cookies[$key];
+        }
+        return null;
+    }
+
+    /**
+     * Check if a cookie with the given name exists
+     *
+     * @param string $name The cookie name to check.
+     * @return bool True if the cookie exists, otherwise false.
+     */
+    public function has($name)
+    {
+        $key = mb_strtolower($name);
+        return isset($this->cookies[$key]);
+    }
+
+    /**
      * Checks if only valid cookie objects are in the array
      *
      * @param array $cookies Array of cookie objects

+ 58 - 0
tests/TestCase/Http/Cookie/CookieCollectionTest.php

@@ -50,6 +50,64 @@ class CookieCollectionTest extends TestCase
     }
 
     /**
+     * Test iteration
+     *
+     * @return void
+     */
+    public function testIteration()
+    {
+        $cookies = [
+            new Cookie('remember_me', 'a'),
+            new Cookie('gtm', 'b'),
+            new Cookie('three', 'tree')
+        ];
+
+        $collection = new CookieCollection($cookies);
+        $names = [];
+        foreach ($collection as $cookie) {
+            $names[] = $cookie->getName();
+        }
+        $this->assertSame(['remember_me', 'gtm', 'three'], $names);
+    }
+
+    /**
+     * Test has()
+     *
+     * @return void
+     */
+    public function testHas()
+    {
+        $cookies = [
+            new Cookie('remember_me', 'a'),
+            new Cookie('gtm', 'b')
+        ];
+
+        $collection = new CookieCollection($cookies);
+        $this->assertFalse($collection->has('nope'));
+        $this->assertTrue($collection->has('remember_me'));
+        $this->assertTrue($collection->has('REMEMBER_me'), 'case insensitive cookie names');
+    }
+
+    /**
+     * Test getting cookies by name
+     *
+     * @return void
+     */
+    public function testGetByName()
+    {
+        $cookies = [
+            new Cookie('remember_me', 'a'),
+            new Cookie('gtm', 'b')
+        ];
+
+        $collection = new CookieCollection($cookies);
+        $this->assertNull($collection->get('nope'));
+        $this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
+        $this->assertInstanceOf(Cookie::class, $collection->get('remember_me'));
+        $this->assertSame($cookies[0], $collection->get('remember_me'));
+    }
+
+    /**
      * Test that the constructor takes only an array of objects implementing
      * the CookieInterface
      *