merged branch GromNaN/browser-kit-cookie-jar-dependency (PR #4178)

Commits
-------

95b8e29 [BrowserKit] Remove dependency of CookieJar to Response

Discussion
----------

[BrowserKit] Remove dependency of CookieJar to Response

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

The CookieJar has currently a hard dependency to `BrowserKit\Response`, but this dependency could be avoided.

---------------------------------------------------------------------------

by stof at 2012-05-01T21:52:34Z

Renaming a method *is* a BC break.

You should add a new method and keep the old one accepting the Response (and make it calling the new method internally). This way, it would add the new feature without breaking the BC.

---------------------------------------------------------------------------

by stof at 2012-05-01T21:53:31Z

And btw, I don't see the issue with BrowerKit depending on BrowserKit. If you have a class, you also have the other one.

---------------------------------------------------------------------------

by GromNaN at 2012-05-02T05:57:51Z

The issue is that I want to use the CookieJar without the Request/Response of BrowserKit.

---------------------------------------------------------------------------

by fabpot at 2012-05-03T06:37:02Z

You should also keep some unit tests for the old method.

---------------------------------------------------------------------------

by GromNaN at 2012-05-03T08:22:39Z

@fabpot I've made the changes.

---------------------------------------------------------------------------

by fabpot at 2012-05-03T10:53:47Z

Can you squash your commits before I merge? Thanks.

---------------------------------------------------------------------------

by GromNaN at 2012-05-03T10:57:30Z

@fabpot Squashed
This commit is contained in:
Fabien Potencier 2012-05-03 13:11:02 +02:00
commit b49d611eca
2 changed files with 32 additions and 13 deletions

View File

@ -89,16 +89,16 @@ class CookieJar
}
/**
* Updates the cookie jar from a Response object.
* Updates the cookie jar from a response Set-Cookie headers.
*
* @param Response $response A Response object
* @param string $uri The base URL
* @param array $setCookies Set-Cookie headers from an HTTP response
* @param string $uri The base URL
*/
public function updateFromResponse(Response $response, $uri = null)
public function updateFromSetCookie(array $setCookies, $uri = null)
{
$cookies = array();
foreach ($response->getHeader('Set-Cookie', false) as $cookie) {
foreach ($setCookies as $cookie) {
foreach (explode(',', $cookie) as $i => $part) {
if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
$cookies[] = ltrim($part);
@ -113,6 +113,17 @@ class CookieJar
}
}
/**
* Updates the cookie jar from a Response object.
*
* @param Response $response A Response object
* @param string $uri The base URL
*/
public function updateFromResponse(Response $response, $uri = null)
{
$this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
}
/**
* Returns not yet expired cookies.
*

View File

@ -63,25 +63,33 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
$response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
$cookieJar = new CookieJar();
$cookieJar->set(new Cookie('bar', 'bar'));
$cookieJar->updateFromResponse($response);
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
}
public function testUpdateFromSetCookie()
{
$setCookies = array('foo=foo');
$cookieJar = new CookieJar();
$cookieJar->set(new Cookie('bar', 'bar'));
$cookieJar->updateFromSetCookie($setCookies);
$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
$this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromResponse() keeps existing cookies');
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
$this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
}
public function testUpdateFromResponseWithMultipleCookies()
public function testUpdateFromSetCookieWithMultipleCookies()
{
$timestamp = time() + 3600;
$date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
$response = new Response('', 200, array(
'Set-Cookie' => sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date)
));
$setCookies = array(sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date));
$cookieJar = new CookieJar();
$cookieJar->updateFromResponse($response);
$cookieJar->updateFromSetCookie($setCookies);
$fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
$barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');