4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\BrowserKit\CookieJar;
16 use Symfony\Component\BrowserKit\Cookie;
17 use Symfony\Component\BrowserKit\Response;
19 class CookieJarTest extends TestCase
21 public function testSetGet()
23 $cookieJar = new CookieJar();
24 $cookieJar->set($cookie = new Cookie('foo', 'bar'));
26 $this->assertEquals($cookie, $cookieJar->get('foo'), '->set() sets a cookie');
28 $this->assertNull($cookieJar->get('foobar'), '->get() returns null if the cookie does not exist');
30 $cookieJar->set($cookie = new Cookie('foo', 'bar', time() - 86400));
31 $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
34 public function testExpire()
36 $cookieJar = new CookieJar();
37 $cookieJar->set($cookie = new Cookie('foo', 'bar'));
38 $cookieJar->expire('foo');
39 $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
42 public function testAll()
44 $cookieJar = new CookieJar();
45 $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
46 $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
48 $this->assertEquals(array($cookie1, $cookie2), $cookieJar->all(), '->all() returns all cookies in the jar');
51 public function testClear()
53 $cookieJar = new CookieJar();
54 $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
55 $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
59 $this->assertEquals(array(), $cookieJar->all(), '->clear() expires all cookies');
62 public function testUpdateFromResponse()
64 $response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
66 $cookieJar = new CookieJar();
67 $cookieJar->updateFromResponse($response);
69 $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
72 public function testUpdateFromSetCookie()
74 $setCookies = array('foo=foo');
76 $cookieJar = new CookieJar();
77 $cookieJar->set(new Cookie('bar', 'bar'));
78 $cookieJar->updateFromSetCookie($setCookies);
80 $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
81 $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
82 $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
83 $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
86 public function testUpdateFromEmptySetCookie()
88 $cookieJar = new CookieJar();
89 $cookieJar->updateFromSetCookie(array(''));
90 $this->assertEquals(array(), $cookieJar->all());
93 public function testUpdateFromSetCookieWithMultipleCookies()
95 $timestamp = time() + 3600;
96 $date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
97 $setCookies = array(sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date));
99 $cookieJar = new CookieJar();
100 $cookieJar->updateFromSetCookie($setCookies);
102 $fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
103 $barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
104 $phpCookie = $cookieJar->get('PHPSESSID');
106 $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $fooCookie);
107 $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $barCookie);
108 $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $phpCookie);
109 $this->assertEquals('foo', $fooCookie->getValue());
110 $this->assertEquals('bar', $barCookie->getValue());
111 $this->assertEquals('id', $phpCookie->getValue());
112 $this->assertEquals($timestamp, $fooCookie->getExpiresTime());
113 $this->assertNull($barCookie->getExpiresTime());
114 $this->assertEquals($timestamp, $phpCookie->getExpiresTime());
118 * @dataProvider provideAllValuesValues
120 public function testAllValues($uri, $values)
122 $cookieJar = new CookieJar();
123 $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
124 $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
125 $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
126 $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
127 $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
128 $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
130 $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
133 public function provideAllValuesValues()
136 array('http://www.example.com', array('foo_nothing', 'foo_domain')),
137 array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
138 array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
139 array('http://foo.example1.com/', array('foo_nothing')),
140 array('https://foo.example.com/', array('foo_nothing', 'foo_secure', 'foo_domain')),
141 array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
142 array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
146 public function testEncodedValues()
148 $cookieJar = new CookieJar();
149 $cookieJar->set($cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true));
151 $this->assertEquals(array('foo' => 'bar=baz'), $cookieJar->allValues('/'));
152 $this->assertEquals(array('foo' => 'bar%3Dbaz'), $cookieJar->allRawValues('/'));
155 public function testCookieExpireWithSameNameButDifferentPaths()
157 $cookieJar = new CookieJar();
158 $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
159 $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
160 $cookieJar->expire('foo', '/foo');
162 $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
163 $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
164 $this->assertEquals(array(), $cookieJar->allValues('http://example.com/foo'));
165 $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
168 public function testCookieExpireWithNullPaths()
170 $cookieJar = new CookieJar();
171 $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/'));
172 $cookieJar->expire('foo', null);
174 $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
175 $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
178 public function testCookieExpireWithDomain()
180 $cookieJar = new CookieJar();
181 $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo', 'http://example2.com/'));
182 $cookieJar->expire('foo', '/foo', 'http://example2.com/');
184 $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
185 $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example2.com/')));
188 public function testCookieWithSameNameButDifferentPaths()
190 $cookieJar = new CookieJar();
191 $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
192 $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
194 $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
195 $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://example.com/foo'));
196 $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
199 public function testCookieWithSameNameButDifferentDomains()
201 $cookieJar = new CookieJar();
202 $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/', 'foo.example.com'));
203 $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/', 'bar.example.com'));
205 $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
206 $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://foo.example.com/'));
207 $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://bar.example.com/'));
210 public function testCookieGetWithSubdomain()
212 $cookieJar = new CookieJar();
213 $cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/', '.example.com'));
214 $cookieJar->set($cookie2 = new Cookie('foo1', 'bar', null, '/', 'test.example.com'));
216 $this->assertEquals($cookie1, $cookieJar->get('foo', '/', 'foo.example.com'));
217 $this->assertEquals($cookie1, $cookieJar->get('foo', '/', 'example.com'));
218 $this->assertEquals($cookie2, $cookieJar->get('foo1', '/', 'test.example.com'));
221 public function testCookieGetWithWrongSubdomain()
223 $cookieJar = new CookieJar();
224 $cookieJar->set($cookie1 = new Cookie('foo1', 'bar', null, '/', 'test.example.com'));
226 $this->assertNull($cookieJar->get('foo1', '/', 'foo.example.com'));
229 public function testCookieGetWithSubdirectory()
231 $cookieJar = new CookieJar();
232 $cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/test', '.example.com'));
233 $cookieJar->set($cookie2 = new Cookie('foo1', 'bar1', null, '/', '.example.com'));
235 $this->assertNull($cookieJar->get('foo', '/', '.example.com'));
236 $this->assertNull($cookieJar->get('foo', '/bar', '.example.com'));
237 $this->assertEquals($cookie1, $cookieJar->get('foo', '/test', 'example.com'));
238 $this->assertEquals($cookie2, $cookieJar->get('foo1', '/', 'example.com'));
239 $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar', 'example.com'));
242 public function testCookieWithWildcardDomain()
244 $cookieJar = new CookieJar();
245 $cookieJar->set(new Cookie('foo', 'bar', null, '/', '.example.com'));
247 $this->assertEquals(array('foo' => 'bar'), $cookieJar->allValues('http://www.example.com'));
248 $this->assertEmpty($cookieJar->allValues('http://wwwexample.com'));