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\Cookie;
17 class CookieTest extends TestCase
19 public function testToString()
21 $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
22 $this->assertEquals('foo=bar; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
24 $cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
25 $this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
27 $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
28 $this->assertEquals('foo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=.myfoodomain.com; path=/admin/; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
30 $cookie = new Cookie('foo', 'bar', 0, '/', '');
31 $this->assertEquals('foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; httponly', (string) $cookie);
35 * @dataProvider getTestsForToFromString
37 public function testToFromString($cookie, $url = null)
39 $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
42 public function getTestsForToFromString()
45 array('foo=bar; path=/'),
46 array('foo=bar; path=/foo'),
47 array('foo=bar; domain=google.com; path=/'),
48 array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
49 array('foo=bar; path=/; httponly'),
50 array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
51 array('foo=bar=baz; path=/'),
52 array('foo=bar%3Dbaz; path=/'),
56 public function testFromStringIgnoreSecureFlag()
58 $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
59 $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
63 * @dataProvider getExpireCookieStrings
65 public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
67 $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
70 public function getExpireCookieStrings()
73 array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
74 array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
75 array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
76 array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
77 array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
78 array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
79 array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
80 array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
84 public function testFromStringWithCapitalization()
86 $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
87 $this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
88 $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
91 public function testFromStringWithUrl()
93 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com/'));
94 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com'));
95 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com?foo'));
96 $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::fromString('foo=bar', 'http://www.example.com/foo/bar'));
97 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
98 $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::fromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
101 public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
103 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
104 Cookie::fromString('foo');
107 public function testFromStringIgnoresInvalidExpiresDate()
109 $cookie = Cookie::fromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
111 $this->assertFalse($cookie->isExpired());
114 public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
116 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
117 Cookie::fromString('foo=bar', 'foobar');
120 public function testGetName()
122 $cookie = new Cookie('foo', 'bar');
123 $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
126 public function testGetValue()
128 $cookie = new Cookie('foo', 'bar');
129 $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
131 $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
132 $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
135 public function testGetRawValue()
137 $cookie = new Cookie('foo', 'bar=baz'); // decoded value
138 $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
139 $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
140 $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
143 public function testGetPath()
145 $cookie = new Cookie('foo', 'bar', 0);
146 $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
148 $cookie = new Cookie('foo', 'bar', 0, '/foo');
149 $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
152 public function testGetDomain()
154 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
155 $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
158 public function testIsSecure()
160 $cookie = new Cookie('foo', 'bar');
161 $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
163 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
164 $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
167 public function testIsHttponly()
169 $cookie = new Cookie('foo', 'bar');
170 $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
172 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
173 $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
176 public function testGetExpiresTime()
178 $cookie = new Cookie('foo', 'bar');
179 $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
181 $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
182 $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
185 public function testIsExpired()
187 $cookie = new Cookie('foo', 'bar');
188 $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
190 $cookie = new Cookie('foo', 'bar', time() - 86400);
191 $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
193 $cookie = new Cookie('foo', 'bar', 0);
194 $this->assertFalse($cookie->isExpired());
198 * @expectedException \UnexpectedValueException
199 * @expectedExceptionMessage The cookie expiration time "string" is not valid.
201 public function testConstructException()
203 $cookie = new Cookie('foo', 'bar', 'string');