From 8890344f27f54bb3b225f073bbb22625d2f652ad Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 12 Mar 2017 20:29:15 -0400 Subject: [PATCH] Add get() and has() to CookieCollection. Also add missing tests for iterating the collection. --- src/Http/Cookie/CookieCollection.php | 32 ++++++++++ .../Http/Cookie/CookieCollectionTest.php | 58 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/Http/Cookie/CookieCollection.php b/src/Http/Cookie/CookieCollection.php index 7d9aba6f966..c1c970c5457 100644 --- a/src/Http/Cookie/CookieCollection.php +++ b/src/Http/Cookie/CookieCollection.php @@ -45,6 +45,38 @@ public function __construct(array $cookies = []) } } + /** + * 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 * diff --git a/tests/TestCase/Http/Cookie/CookieCollectionTest.php b/tests/TestCase/Http/Cookie/CookieCollectionTest.php index e0cd97fbf30..d47c61e2031 100644 --- a/tests/TestCase/Http/Cookie/CookieCollectionTest.php +++ b/tests/TestCase/Http/Cookie/CookieCollectionTest.php @@ -49,6 +49,64 @@ public function testConstructorWithCookieArray() $this->assertCount(2, $collection); } + /** + * 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