Skip to content

Commit

Permalink
Move Cookie::toArrayResponse() to Response::convertCookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka committed Apr 21, 2017
1 parent 3db767b commit c3c8516
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 62 deletions.
21 changes: 0 additions & 21 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -619,27 +619,6 @@ public function toArray()
];
}

/**
* 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->getExpiresTimestamp()
];
}

/**
* Implode method to keep keys are multidimensional arrays
*
Expand Down
20 changes: 5 additions & 15 deletions src/Http/Response.php
Expand Up @@ -1941,7 +1941,7 @@ public function cookie($options = null)

$cookie = $this->_cookies->get($options);

return $this->toArrayResponse($cookie);
return $this->convertCookie($cookie);
}

$options += [
Expand Down Expand Up @@ -2047,7 +2047,7 @@ public function getCookie($name)

$cookie = $this->_cookies->get($name);

return $this->toArrayResponse($cookie);
return $this->convertCookie($cookie);
}

/**
Expand All @@ -2061,7 +2061,7 @@ public function getCookies()
{
$out = [];
foreach ($this->_cookies as $cookie) {
$out[$cookie->getName()] = $this->toArrayResponse($cookie);
$out[$cookie->getName()] = $this->convertCookie($cookie);
}

return $out;
Expand All @@ -2076,26 +2076,16 @@ public function getCookies()
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
* @return array
*/
protected function toArrayResponse(CookieInterface $cookie)
public function convertCookie(CookieInterface $cookie)
{
if ($cookie instanceof Cookie) {
return $cookie->toArrayResponse();
}

if ($cookie->getExpiry()) {
$expires = $cookie->getExpiry()->format('U');
} else {
$expires = '';
}

return [
'name' => $cookie->getName(),
'value' => $cookie->getValue(),
'path' => $cookie->getPath(),
'domain' => $cookie->getDomain(),
'secure' => $cookie->isSecure(),
'httpOnly' => $cookie->isHttpOnly(),
'expire' => $expires
'expire' => $cookie->getExpiresTimestamp()
];
}

Expand Down
26 changes: 0 additions & 26 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -568,30 +568,4 @@ public function testToArray()
];
$this->assertEquals($expected, $cookie->toArray());
}

/**
* 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());
}
}
28 changes: 28 additions & 0 deletions tests/TestCase/Http/ResponseTest.php
Expand Up @@ -16,6 +16,7 @@

include_once CORE_TEST_CASES . DS . 'Http' . DS . 'server_mocks.php';

use Cake\Chronos\Chronos;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Http\Response;
Expand Down Expand Up @@ -3050,4 +3051,31 @@ public function testDebugInfo()
];
$this->assertEquals($expected, $result);
}

/**
* Test convertCookie
*
* @return void
*/
public function testConvertCookie()
{
$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
];
$response = new Response();
$this->assertEquals($expected, $response->convertCookie($cookie));
}
}

0 comments on commit c3c8516

Please sign in to comment.