14 use PHPUnit\Framework\TestCase;
56 new Cookie(
'MyCookie',
'foo',
'bar');
61 $cookie =
new Cookie(
'foo',
'bar', -100);
63 $this->assertSame(0, $cookie->getExpiresTime());
69 $cookie =
new Cookie(
'MyCookie', $value);
71 $this->assertSame($value, $cookie->getValue(),
'->getValue() returns the proper value');
76 $cookie =
new Cookie(
'foo',
'bar');
78 $this->assertSame(
'/', $cookie->getPath(),
'->getPath() returns / as the default path');
83 $cookie =
new Cookie(
'foo',
'bar');
85 $this->assertEquals(0, $cookie->getExpiresTime(),
'->getExpiresTime() returns the default expire date');
87 $cookie =
new Cookie(
'foo',
'bar', $expire = time() + 3600);
89 $this->assertEquals($expire, $cookie->getExpiresTime(),
'->getExpiresTime() returns the expire date');
94 $cookie =
new Cookie(
'foo',
'bar', 3600.9);
96 $this->assertSame(3600, $cookie->getExpiresTime(),
'->getExpiresTime() returns the expire date as an integer');
101 $expire = new \DateTime();
102 $cookie =
new Cookie(
'foo',
'bar', $expire);
104 $this->assertEquals($expire->format(
'U'), $cookie->getExpiresTime(),
'->getExpiresTime() returns the expire date');
112 $expire = new \DateTimeImmutable();
113 $cookie =
new Cookie(
'foo',
'bar', $expire);
115 $this->assertEquals($expire->format(
'U'), $cookie->getExpiresTime(),
'->getExpiresTime() returns the expire date');
121 $cookie =
new Cookie(
'foo',
'bar', $value);
122 $expire = strtotime($value);
124 $this->assertEquals($expire, $cookie->getExpiresTime(),
'->getExpiresTime() returns the expire date', 1);
129 $cookie =
new Cookie(
'foo',
'bar', 0,
'/',
'.myfoodomain.com');
131 $this->assertEquals(
'.myfoodomain.com', $cookie->getDomain(),
'->getDomain() returns the domain name on which the cookie is valid');
136 $cookie =
new Cookie(
'foo',
'bar', 0,
'/',
'.myfoodomain.com',
true);
138 $this->assertTrue($cookie->isSecure(),
'->isSecure() returns whether the cookie is transmitted over HTTPS');
143 $cookie =
new Cookie(
'foo',
'bar', 0,
'/',
'.myfoodomain.com',
false,
true);
145 $this->assertTrue($cookie->isHttpOnly(),
'->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
150 $cookie =
new Cookie(
'foo',
'bar', time() + 3600 * 24);
152 $this->assertFalse($cookie->isCleared(),
'->isCleared() returns false if the cookie did not expire yet');
157 $cookie =
new Cookie(
'foo',
'bar', time() - 20);
159 $this->assertTrue($cookie->isCleared(),
'->isCleared() returns true if the cookie has expired');
164 $cookie =
new Cookie(
'foo',
'bar', $expire = strtotime(
'Fri, 20-May-2011 15:25:52 GMT'),
'/',
'.myfoodomain.com',
true);
165 $this->assertEquals(
'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).
'; path=/; domain=.myfoodomain.com; secure; httponly', (
string) $cookie,
'->__toString() returns string representation of the cookie');
167 $cookie =
new Cookie(
'foo',
'bar with white spaces', strtotime(
'Fri, 20-May-2011 15:25:52 GMT'),
'/',
'.myfoodomain.com',
true);
168 $this->assertEquals(
'foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).
'; path=/; domain=.myfoodomain.com; secure; httponly', (
string) $cookie,
'->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
170 $cookie =
new Cookie(
'foo',
null, 1,
'/admin/',
'.myfoodomain.com');
171 $this->assertEquals(
'foo=deleted; expires='.gmdate(
'D, d-M-Y H:i:s T', $expire = time() - 31536001).
'; max-age='.($expire - time()).
'; path=/admin/; domain=.myfoodomain.com; httponly', (
string) $cookie,
'->__toString() returns string representation of a cleared cookie if value is NULL');
173 $cookie =
new Cookie(
'foo',
'bar', 0,
'/',
'');
174 $this->assertEquals(
'foo=bar; path=/; httponly', (
string) $cookie);
179 $cookie =
new Cookie(
'foo',
'b a r', 0,
'/',
null,
false,
false);
180 $this->assertFalse($cookie->isRaw());
181 $this->assertEquals(
'foo=b%20a%20r; path=/', (
string) $cookie);
183 $cookie =
new Cookie(
'foo',
'b+a+r', 0,
'/',
null,
false,
false,
true);
184 $this->assertTrue($cookie->isRaw());
185 $this->assertEquals(
'foo=b+a+r; path=/', (
string) $cookie);
190 $cookie =
new Cookie(
'foo',
'bar');
191 $this->assertEquals(0, $cookie->getMaxAge());
193 $cookie =
new Cookie(
'foo',
'bar', $expire = time() + 100);
194 $this->assertEquals($expire - time(), $cookie->getMaxAge());
196 $cookie =
new Cookie(
'foo',
'bar', $expire = time() - 100);
197 $this->assertEquals($expire - time(), $cookie->getMaxAge());
202 $cookie =
Cookie::fromString(
'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
203 $this->assertEquals(
new Cookie(
'foo',
'bar', strtotime(
'Fri, 20-May-2011 15:25:52 GMT'),
'/',
'.myfoodomain.com',
true,
true,
true), $cookie);
206 $this->assertEquals(
new Cookie(
'foo',
'bar', 0,
'/',
null,
false,
false), $cookie);
211 $cookie =
Cookie::fromString(
'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
212 $this->assertTrue($cookie->isHttpOnly());
214 $cookie =
Cookie::fromString(
'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
215 $this->assertFalse($cookie->isHttpOnly());
220 $cookie =
new Cookie(
'foo',
'bar', 0,
'/',
null,
false,
true,
false,
'Lax');
221 $this->assertEquals(
'lax', $cookie->getSameSite());