From 74e1c4f7c71f29f530ba05715dd55835a1197e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Fri, 21 Apr 2017 08:43:03 +0200 Subject: [PATCH] Moves Cookie::toArray() to Client\CookieCollection::convertCookie() as it's only a backcompat method for http client. --- src/Http/Client/CookieCollection.php | 26 +++++++++++++++-- src/Http/Cookie/Cookie.php | 20 ------------- src/Http/Cookie/CookieInterface.php | 9 ------ .../Http/Client/CookieCollectionTest.php | 28 +++++++++++++++++++ tests/TestCase/Http/Cookie/CookieTest.php | 26 ----------------- 5 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/Http/Client/CookieCollection.php b/src/Http/Client/CookieCollection.php index 871886651ca..00e228f2032 100644 --- a/src/Http/Client/CookieCollection.php +++ b/src/Http/Client/CookieCollection.php @@ -14,6 +14,7 @@ namespace Cake\Http\Client; use Cake\Http\Cookie\CookieCollection as BaseCollection; +use Cake\Http\Cookie\CookieInterface; /** * Container class for cookies used in Http\Client. @@ -32,7 +33,7 @@ class CookieCollection extends BaseCollection * Store the cookies that haven't expired. If a cookie has been expired * and is currently stored, it will be removed. * - * @param \Cake\Http\Client\Response $response The response to read cookies from + * @param Response $response The response to read cookies from * @param string $url The request URL used for default host/path values. * @return void */ @@ -78,11 +79,32 @@ public function getAll() { $out = []; foreach ($this->cookies as $cookie) { - $out[] = $cookie->toArray(); + $out[] = $this->convertCookie($cookie); } return $out; } + + /** + * Convert the cookie into an array of its properties. + * + * Primarily useful where backwards compatibility is needed. + * + * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object. + * @return array + */ + public function convertCookie(CookieInterface $cookie) + { + return [ + 'name' => $cookie->getName(), + 'value' => $cookie->getValue(), + 'path' => $cookie->getPath(), + 'domain' => $cookie->getDomain(), + 'secure' => $cookie->isSecure(), + 'httponly' => $cookie->isHttpOnly(), + 'expires' => $cookie->getExpiresTimestamp() + ]; + } } // @deprecated Add backwards compat alias. diff --git a/src/Http/Cookie/Cookie.php b/src/Http/Cookie/Cookie.php index 95daeeed41a..d47f2c7ecd0 100644 --- a/src/Http/Cookie/Cookie.php +++ b/src/Http/Cookie/Cookie.php @@ -599,26 +599,6 @@ public function isExpanded() return $this->isExpanded; } - /** - * Convert the cookie into an array of its properties. - * - * Primarily useful where backwards compatibility is needed. - * - * @return array - */ - public function toArray() - { - return [ - 'name' => $this->getName(), - 'value' => $this->getValue(), - 'path' => $this->getPath(), - 'domain' => $this->getDomain(), - 'secure' => $this->isSecure(), - 'httponly' => $this->isHttpOnly(), - 'expires' => $this->getExpiresTimestamp() - ]; - } - /** * Implode method to keep keys are multidimensional arrays * diff --git a/src/Http/Cookie/CookieInterface.php b/src/Http/Cookie/CookieInterface.php index 282637a25b7..a0ef7541179 100644 --- a/src/Http/Cookie/CookieInterface.php +++ b/src/Http/Cookie/CookieInterface.php @@ -191,13 +191,4 @@ public function withSecure($secure); * @return string */ public function toHeaderValue(); - - /** - * Convert the cookie into an array of its properties. - * - * Primarily useful where backwards compatibility is needed. - * - * @return array - */ - public function toArray(); } diff --git a/tests/TestCase/Http/Client/CookieCollectionTest.php b/tests/TestCase/Http/Client/CookieCollectionTest.php index 8397f086da3..63d4429f83c 100644 --- a/tests/TestCase/Http/Client/CookieCollectionTest.php +++ b/tests/TestCase/Http/Client/CookieCollectionTest.php @@ -13,8 +13,10 @@ */ namespace Cake\Test\TestCase\Http\Client; +use Cake\Chronos\Chronos; use Cake\Http\Client\CookieCollection; use Cake\Http\Client\Response; +use Cake\Http\Cookie\Cookie; use Cake\TestSuite\TestCase; /** @@ -301,4 +303,30 @@ public function testGetMatchingDomainWithDot() $expected = []; $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', + 'expires' => $date->format('U'), + 'secure' => true, + 'httponly' => true + ]; + $this->assertEquals($expected, $this->cookies->convertCookie($cookie)); + } } diff --git a/tests/TestCase/Http/Cookie/CookieTest.php b/tests/TestCase/Http/Cookie/CookieTest.php index 4c3d297285f..ea1c9491700 100644 --- a/tests/TestCase/Http/Cookie/CookieTest.php +++ b/tests/TestCase/Http/Cookie/CookieTest.php @@ -542,30 +542,4 @@ public function testGetId() $cookie = new Cookie('test', 'val', null, '/path', 'example.com'); $this->assertEquals('test;example.com;/path', $cookie->getId()); } - - /** - * Test toArray - * - * @return void - */ - public function testToArray() - { - $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', - 'expires' => $date->format('U'), - 'secure' => true, - 'httponly' => true - ]; - $this->assertEquals($expected, $cookie->toArray()); - } }