Skip to content

Commit

Permalink
Convert setPath() & httpOnly to use immutable patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 19, 2017
1 parent 9eecf10 commit 3682bdc
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 31 deletions.
69 changes: 39 additions & 30 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -125,10 +125,12 @@ public function __construct($name, $value = '', $expiresAt = null, $path = '', $
$this->validateName($name);
$this->name = $name;
$this->_setValue($value);
$this->validateDomain($domain);
$this->validateString($domain);
$this->domain = $domain;
$this->setHttpOnly($httpOnly);
$this->setPath($path);
$this->validateBool($httpOnly);
$this->httpOnly = $httpOnly;
$this->validateString($path);
$this->path = $path;

if ($expiresAt !== null) {
$this->expiresAt($expiresAt);
Expand Down Expand Up @@ -260,23 +262,18 @@ protected function _setValue($value)
}

/**
* Sets the path
* Create a new cookie with an updated path
*
* @param string $path Sets the path
* @return $this
* @return static
*/
public function setPath($path)
public function withPath($path)
{
if (!is_string($path)) {
throw new InvalidArgumentException(sprintf(
'The provided arg must be of type `string` but `%s` given',
gettype($path)
));
}

$this->path = $path;
$this->validateString($path);
$new = clone $this;
$new->path = $path;

return $this;
return $new;
}

/**
Expand All @@ -287,26 +284,26 @@ public function setPath($path)
*/
public function withDomain($domain)
{
$this->validateDomain($domain);
$this->validateString($domain);
$new = clone $this;
$new->domain = $domain;

return $new;
}

/**
* Validate that domains are strings.
* Validate that an argument is a string
*
* @param string $domain The domain to validate.
* @param string $value The value to validate.
* @return void
* @throws \InvalidArgumentException
*/
protected function validateDomain($domain)
protected function validateString($value)
{
if (!is_string($domain)) {
if (!is_string($value)) {
throw new InvalidArgumentException(sprintf(
'The provided arg must be of type `string` but `%s` given',
gettype($domain)
gettype($value)
));
}
}
Expand All @@ -322,23 +319,35 @@ public function isSecure()
}

/**
* Set HTTP Only
* Create a cookie with HTTP Only updated
*
* @param bool $httpOnly HTTP Only
* @return $this
* @return static
*/
public function setHttpOnly($httpOnly)
public function withHttpOnly($httpOnly)
{
if (!is_bool($httpOnly)) {
$this->validateBool($httpOnly);
$new = clone $this;
$new->httpOnly = $httpOnly;

return $new;
}

/**
* Validate that an argument is a boolean
*
* @param bool $value The value to validate.
* @return void
* @throws \InvalidArgumentException
*/
protected function validateBool($value)
{
if (!is_bool($value)) {
throw new InvalidArgumentException(sprintf(
'The provided arg must be of type `bool` but `%s` given',
gettype($httpOnly)
gettype($value)
));
}

$this->httpOnly = $httpOnly;

return $this;
}

/**
Expand Down
87 changes: 86 additions & 1 deletion tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -135,6 +135,17 @@ public function testWithValue()
$this->assertSame('new', $new->getValue());
}

/**
* Test setting domain in cookies
*
* @return void
* @expectedException \InvalidArgumentException
*/
public function testWithDomainInvalidConstructor()
{
new Cookie('cakephp', 'rocks', null, '', 1234);
}

/**
* Test setting domain in cookies
*
Expand All @@ -158,7 +169,81 @@ public function testWithDomain()
$new = $cookie->withDomain('example.com');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertNotContains('example.com', $cookie->toHeaderValue(), 'old instance not modified');
$this->assertContains('example.com', $new->toHeaderValue());
$this->assertContains('domain=example.com', $new->toHeaderValue());
}

/**
* Test setting path in cookies
*
* @return void
* @expectedException \InvalidArgumentException
*/
public function testWithPathInvalid()
{
$cookie = new Cookie('cakephp', 'rocks');
$cookie->withPath(['oops']);
}

/**
* Test setting path in cookies
*
* @return void
* @expectedException \InvalidArgumentException
*/
public function testWithPathInvalidConstructor()
{
new Cookie('cakephp', 'rocks', null, 123);
}

/**
* Test setting path in cookies
*
* @return void
*/
public function testWithPath()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withPath('/api');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertNotContains('path=/api', $cookie->toHeaderValue(), 'old instance not modified');
$this->assertContains('path=/api', $new->toHeaderValue());
}

/**
* Test setting httponly in cookies
*
* @return void
* @expectedException \InvalidArgumentException
*/
public function testWithHttpOnlyInvalidConstructor()
{
new Cookie('cakephp', 'cakephp-rocks', null, '', '', false, 'invalid');
}

/**
* Test setting httponly in cookies
*
* @return void
* @expectedException \InvalidArgumentException
*/
public function testWithHttpOnlyInvalid()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie->withHttpOnly('no');
}

/**
* Test setting httponly in cookies
*
* @return void
*/
public function testWithHttpOnly()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withHttpOnly(true);
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertFalse($cookie->isHttpOnly());
$this->assertTrue($new->isHttpOnly());
}

/**
Expand Down

0 comments on commit 3682bdc

Please sign in to comment.