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
20 * @dataProvider getTestsForToFromString
22 public function testToFromString($cookie, $url = null)
24 $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
27 public function getTestsForToFromString()
30 array('foo=bar; path=/'),
31 array('foo=bar; path=/foo'),
32 array('foo=bar; domain=google.com; path=/'),
33 array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
34 array('foo=bar; path=/; httponly'),
35 array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
36 array('foo=bar=baz; path=/'),
37 array('foo=bar%3Dbaz; path=/'),
41 public function testFromStringIgnoreSecureFlag()
43 $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
44 $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
48 * @dataProvider getExpireCookieStrings
50 public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
52 $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
55 public function getExpireCookieStrings()
58 array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
59 array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
60 array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
61 array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
62 array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
63 array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
64 array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
65 array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
69 public function testFromStringWithCapitalization()
71 $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
72 $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'));
73 $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
76 public function testFromStringWithUrl()
78 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com/'));
79 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com'));
80 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com?foo'));
81 $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::fromString('foo=bar', 'http://www.example.com/foo/bar'));
82 $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
83 $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::fromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
86 public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
88 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
89 Cookie::fromString('foo');
92 public function testFromStringIgnoresInvalidExpiresDate()
94 $cookie = Cookie::fromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
96 $this->assertFalse($cookie->isExpired());
99 public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
101 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
102 Cookie::fromString('foo=bar', 'foobar');
105 public function testGetName()
107 $cookie = new Cookie('foo', 'bar');
108 $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
111 public function testGetValue()
113 $cookie = new Cookie('foo', 'bar');
114 $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
116 $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
117 $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
120 public function testGetRawValue()
122 $cookie = new Cookie('foo', 'bar=baz'); // decoded value
123 $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
124 $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
125 $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
128 public function testGetPath()
130 $cookie = new Cookie('foo', 'bar', 0);
131 $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
133 $cookie = new Cookie('foo', 'bar', 0, '/foo');
134 $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
137 public function testGetDomain()
139 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
140 $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
143 public function testIsSecure()
145 $cookie = new Cookie('foo', 'bar');
146 $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
148 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
149 $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
152 public function testIsHttponly()
154 $cookie = new Cookie('foo', 'bar');
155 $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
157 $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
158 $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
161 public function testGetExpiresTime()
163 $cookie = new Cookie('foo', 'bar');
164 $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
166 $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
167 $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
170 public function testIsExpired()
172 $cookie = new Cookie('foo', 'bar');
173 $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
175 $cookie = new Cookie('foo', 'bar', time() - 86400);
176 $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
178 $cookie = new Cookie('foo', 'bar', 0);
179 $this->assertFalse($cookie->isExpired());
183 * @expectedException \UnexpectedValueException
184 * @expectedExceptionMessage The cookie expiration time "string" is not valid.
186 public function testConstructException()
188 $cookie = new Cookie('foo', 'bar', 'string');