Skip to content

Commit

Permalink
Add another compatibility shim for cookies on the response.
Browse files Browse the repository at this point in the history
Rename the existing compat method as there are two now.
  • Loading branch information
markstory committed Apr 7, 2017
1 parent 59c2996 commit 5547d37
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Http/Client/Response.php
Expand Up @@ -442,7 +442,7 @@ public function getCookieData($name)
return null;
}

return $this->cookies->get($name)->toArrayCompat();
return $this->cookies->get($name)->toArrayClient();
}

/**
Expand All @@ -469,7 +469,7 @@ protected function _getCookies()

$cookies = [];
foreach ($this->cookies as $cookie) {
$cookies[$cookie->getName()] = $cookie->toArrayCompat();
$cookies[$cookie->getName()] = $cookie->toArrayClient();
}

return $cookies;
Expand Down
32 changes: 28 additions & 4 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -121,7 +121,7 @@ class Cookie implements CookieInterface
* @link http://php.net/manual/en/function.setcookie.php
* @param string $name Cookie name
* @param string|array $value Value of the cookie
* @param \DateTimeInterface|null $expiresAt Expiration time and date
* @param \DateTimeInterface|int|null $expiresAt Expiration time and date
* @param string $path Path
* @param string $domain Domain
* @param bool $secure Is secure
Expand All @@ -130,7 +130,7 @@ class Cookie implements CookieInterface
public function __construct(
$name,
$value = '',
DateTimeInterface $expiresAt = null,
$expiresAt = null,
$path = '',
$domain = '',
$secure = false,
Expand All @@ -153,7 +153,10 @@ public function __construct(
$this->validateBool($secure);
$this->secure = $secure;

if ($expiresAt !== null) {
if (is_int($expiresAt)) {
$this->expiresAt = $expiresAt;
}
if ($expiresAt instanceof DateTimeInterface) {
$this->expiresAt = (int)$expiresAt->format('U');
}
}
Expand Down Expand Up @@ -631,7 +634,7 @@ public function toArray()
*
* @return array
*/
public function toArrayCompat()
public function toArrayClient()
{
return [
'name' => $this->getName(),
Expand All @@ -644,6 +647,27 @@ public function toArrayCompat()
];
}

/**
* Convert the cookie into an array of its properties.
*
* This method is compatible with the historical behavior of Cake\Http\Response,
* where `httponly` is `httpOnly` and `expires` is `expire`
*
* @return array
*/
public function toArrayResponse()
{
return [
'name' => $this->getName(),
'value' => $this->getValue(),
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httpOnly' => $this->isHttpOnly(),
'expire' => $this->expiresAt
];
}

/**
* Implode method to keep keys are multidimensional arrays
*
Expand Down
32 changes: 29 additions & 3 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -549,11 +549,11 @@ public function testToArray()
}

/**
* Test toArrayCompat
* Test toArrayClient
*
* @return void
*/
public function testToArrayCompat()
public function testToArrayClient()
{
$date = Chronos::parse('2017-03-31 12:34:56');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
Expand All @@ -571,6 +571,32 @@ public function testToArrayCompat()
'secure' => true,
'httponly' => true
];
$this->assertEquals($expected, $cookie->toArrayCompat());
$this->assertEquals($expected, $cookie->toArrayClient());
}

/**
* Test toArrayResponse
*
* @return void
*/
public function testToArrayResponse()
{
$date = Chronos::parse('2017-03-31 12:34:56');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie = $cookie->withDomain('cakephp.org')
->withPath('/api')
->withExpiry($date)
->withHttpOnly(true)
->withSecure(true);
$expected = [
'name' => 'cakephp',
'value' => 'cakephp-rocks',
'path' => '/api',
'domain' => 'cakephp.org',
'expire' => $date->format('U'),
'secure' => true,
'httpOnly' => true
];
$this->assertEquals($expected, $cookie->toArrayResponse());
}
}

0 comments on commit 5547d37

Please sign in to comment.