Skip to content

Commit

Permalink
Moves Cookie::toArray() to Client\CookieCollection::convertCookie()
Browse files Browse the repository at this point in the history
as it's only a backcompat method for http client.
  • Loading branch information
Robert Pustułka committed Apr 21, 2017
1 parent c3c8516 commit 74e1c4f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 57 deletions.
26 changes: 24 additions & 2 deletions src/Http/Client/CookieCollection.php
Expand Up @@ -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.
Expand All @@ -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
*/
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 0 additions & 20 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -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
*
Expand Down
9 changes: 0 additions & 9 deletions src/Http/Cookie/CookieInterface.php
Expand Up @@ -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();
}
28 changes: 28 additions & 0 deletions tests/TestCase/Http/Client/CookieCollectionTest.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
}
}
26 changes: 0 additions & 26 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -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());
}
}

0 comments on commit 74e1c4f

Please sign in to comment.