Skip to content

Commit

Permalink
Add get() and has() to CookieCollection.
Browse files Browse the repository at this point in the history
Also add missing tests for iterating the collection.
  • Loading branch information
markstory committed Mar 13, 2017
1 parent 030f3e0 commit 8890344
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Http/Cookie/CookieCollection.php
Expand Up @@ -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
*
Expand Down
58 changes: 58 additions & 0 deletions tests/TestCase/Http/Cookie/CookieCollectionTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 8890344

Please sign in to comment.