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\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Cookie;
20 * @author John Kary <john@johnkary.net>
21 * @author Hugo Hamon <hugo.hamon@sensio.com>
23 * @group time-sensitive
25 class CookieTest extends TestCase
27 public function invalidNames()
43 * @dataProvider invalidNames
44 * @expectedException \InvalidArgumentException
46 public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
52 * @expectedException \InvalidArgumentException
54 public function testInvalidExpiration()
56 new Cookie('MyCookie', 'foo', 'bar');
59 public function testNegativeExpirationIsNotPossible()
61 $cookie = new Cookie('foo', 'bar', -100);
63 $this->assertSame(0, $cookie->getExpiresTime());
66 public function testGetValue()
69 $cookie = new Cookie('MyCookie', $value);
71 $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
74 public function testGetPath()
76 $cookie = new Cookie('foo', 'bar');
78 $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
81 public function testGetExpiresTime()
83 $cookie = new Cookie('foo', 'bar', 3600);
85 $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
88 public function testGetExpiresTimeIsCastToInt()
90 $cookie = new Cookie('foo', 'bar', 3600.9);
92 $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
95 public function testConstructorWithDateTime()
97 $expire = new \DateTime();
98 $cookie = new Cookie('foo', 'bar', $expire);
100 $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
106 public function testConstructorWithDateTimeImmutable()
108 $expire = new \DateTimeImmutable();
109 $cookie = new Cookie('foo', 'bar', $expire);
111 $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
114 public function testGetExpiresTimeWithStringValue()
117 $cookie = new Cookie('foo', 'bar', $value);
118 $expire = strtotime($value);
120 $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
123 public function testGetDomain()
125 $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
127 $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
130 public function testIsSecure()
132 $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
134 $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
137 public function testIsHttpOnly()
139 $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
141 $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
144 public function testCookieIsNotCleared()
146 $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
148 $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
151 public function testCookieIsCleared()
153 $cookie = new Cookie('foo', 'bar', time() - 20);
155 $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
158 public function testToString()
160 $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
161 $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
163 $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
164 $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
166 $cookie = new Cookie('foo', 'bar', 0, '/', '');
167 $this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);