From c3c8516ed9f96a42a661937f981e6103e7c420ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Fri, 21 Apr 2017 08:37:45 +0200 Subject: [PATCH] Move Cookie::toArrayResponse() to Response::convertCookie() --- src/Http/Cookie/Cookie.php | 21 ----------------- src/Http/Response.php | 20 ++++------------ tests/TestCase/Http/Cookie/CookieTest.php | 26 --------------------- tests/TestCase/Http/ResponseTest.php | 28 +++++++++++++++++++++++ 4 files changed, 33 insertions(+), 62 deletions(-) diff --git a/src/Http/Cookie/Cookie.php b/src/Http/Cookie/Cookie.php index 2c6fc39552e..95daeeed41a 100644 --- a/src/Http/Cookie/Cookie.php +++ b/src/Http/Cookie/Cookie.php @@ -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 * diff --git a/src/Http/Response.php b/src/Http/Response.php index cb33940aaad..0d78929f1cf 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -1941,7 +1941,7 @@ public function cookie($options = null) $cookie = $this->_cookies->get($options); - return $this->toArrayResponse($cookie); + return $this->convertCookie($cookie); } $options += [ @@ -2047,7 +2047,7 @@ public function getCookie($name) $cookie = $this->_cookies->get($name); - return $this->toArrayResponse($cookie); + return $this->convertCookie($cookie); } /** @@ -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; @@ -2076,18 +2076,8 @@ 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(), @@ -2095,7 +2085,7 @@ protected function toArrayResponse(CookieInterface $cookie) 'domain' => $cookie->getDomain(), 'secure' => $cookie->isSecure(), 'httpOnly' => $cookie->isHttpOnly(), - 'expire' => $expires + 'expire' => $cookie->getExpiresTimestamp() ]; } diff --git a/tests/TestCase/Http/Cookie/CookieTest.php b/tests/TestCase/Http/Cookie/CookieTest.php index 62198266585..4c3d297285f 100644 --- a/tests/TestCase/Http/Cookie/CookieTest.php +++ b/tests/TestCase/Http/Cookie/CookieTest.php @@ -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()); - } } diff --git a/tests/TestCase/Http/ResponseTest.php b/tests/TestCase/Http/ResponseTest.php index 412c9c5c587..be893f5cb80 100644 --- a/tests/TestCase/Http/ResponseTest.php +++ b/tests/TestCase/Http/ResponseTest.php @@ -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; @@ -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)); + } }