Skip to content

Commit

Permalink
Implement add() and count() for CookieCollection.
Browse files Browse the repository at this point in the history
Make remove() chainable to be consistent with add(). Having chainable
methods here, makes it easier to use the collection as a 'builder'.
  • Loading branch information
markstory committed Mar 13, 2017
1 parent 1b61694 commit 5bfe86e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
33 changes: 30 additions & 3 deletions src/Http/Cookie/CookieCollection.php
Expand Up @@ -14,13 +14,14 @@
namespace Cake\Http\Cookie;

use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;

/**
* Cookie Collection
*/
class CookieCollection implements IteratorAggregate
class CookieCollection implements IteratorAggregate, Countable
{

/**
Expand All @@ -45,6 +46,30 @@ public function __construct(array $cookies = [])
}
}

/**
* Get the number of cookies in the collection.
*
* @return int
*/
public function count()
{
return count($this->cookies);
}

/**
* Add a cookie to the collection
*
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie instance to add.
* @return $this
*/
public function add(CookieInterface $cookie)
{
$key = mb_strtolower($cookie->getName());
$this->cookies[$key] = $cookie;

return $this;
}

/**
* Get a cookie by name
*
Expand All @@ -54,7 +79,7 @@ public function __construct(array $cookies = [])
*
* @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
* @return \Cake\Http\Cookie\CookieInterface|null|array
*/
public function get($name)
{
Expand Down Expand Up @@ -83,12 +108,14 @@ public function has($name)
* If the cookie is not in the collection, this method will do nothing.
*
* @param string $name The name of the cookie to remove.
* @return void
* @return $this
*/
public function remove($name)
{
$key = mb_strtolower($name);
unset($this->cookies[$key]);

return $this;
}

/**
Expand Down
27 changes: 25 additions & 2 deletions tests/TestCase/Http/Cookie/CookieCollectionTest.php
Expand Up @@ -70,6 +70,30 @@ public function testIteration()
$this->assertSame(['remember_me', 'gtm', 'three'], $names);
}

/**
* Test adding cookies
*
* @return void
*/
public function testAdd()
{
$cookies = [];

$collection = new CookieCollection($cookies);
$this->assertCount(0, $collection);

$remember = new Cookie('remember_me', 'a');
$this->assertSame($collection, $collection->add($remember));
$this->assertCount(1, $collection);
$this->assertTrue($collection->has('remember_me'));
$this->assertSame($remember, $collection->get('remember_me'));

$rememberNo = new Cookie('remember_me', 'no');
$this->assertSame($collection, $collection->add($remember)->add($rememberNo));
$this->assertSame($rememberNo, $collection->get('remember_me'));

}

/**
* Test has()
*
Expand All @@ -88,7 +112,6 @@ public function testHas()
$this->assertTrue($collection->has('REMEMBER_me'), 'case insensitive cookie names');
}


/**
* Test removing cookies
*
Expand All @@ -103,7 +126,7 @@ public function testRemove()

$collection = new CookieCollection($cookies);
$this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
$this->assertNull($collection->remove('remember_me'));
$this->assertSame($collection, $collection->remove('remember_me'));
$this->assertFalse($collection->has('remember_me'));
$this->assertNull($collection->get('remember_me'));
}
Expand Down

0 comments on commit 5bfe86e

Please sign in to comment.