1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests;
13:
14: use Symfony\Component\HttpFoundation\Cookie;
15:
16: 17: 18: 19: 20: 21:
22: class CookieTest extends \PHPUnit_Framework_TestCase
23: {
24: public function invalidNames()
25: {
26: return array(
27: array(''),
28: array(",MyName"),
29: array(";MyName"),
30: array(" MyName"),
31: array("\tMyName"),
32: array("\rMyName"),
33: array("\nMyName"),
34: array("\013MyName"),
35: array("\014MyName"),
36: );
37: }
38:
39: 40: 41: 42: 43:
44: public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
45: {
46: new Cookie($name);
47: }
48:
49: 50: 51:
52: public function testInvalidExpiration()
53: {
54: $cookie = new Cookie('MyCookie', 'foo', 'bar');
55: }
56:
57: 58: 59:
60: public function testGetValue()
61: {
62: $value = 'MyValue';
63: $cookie = new Cookie('MyCookie', $value);
64:
65: $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
66: }
67:
68: public function testGetPath()
69: {
70: $cookie = new Cookie('foo', 'bar');
71:
72: $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
73: }
74:
75: public function testGetExpiresTime()
76: {
77: $cookie = new Cookie('foo', 'bar', 3600);
78:
79: $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
80: }
81:
82: public function testConstructorWithDateTime()
83: {
84: $expire = new \DateTime();
85: $cookie = new Cookie('foo', 'bar', $expire);
86:
87: $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
88: }
89:
90: public function testGetExpiresTimeWithStringValue()
91: {
92: $value = "+1 day";
93: $cookie = new Cookie('foo', 'bar', $value);
94: $expire = strtotime($value);
95:
96: $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
97: }
98:
99: public function testGetDomain()
100: {
101: $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
102:
103: $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
104: }
105:
106: public function testIsSecure()
107: {
108: $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
109:
110: $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
111: }
112:
113: public function testIsHttpOnly()
114: {
115: $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
116:
117: $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
118: }
119:
120: public function testCookieIsNotCleared()
121: {
122: $cookie = new Cookie('foo', 'bar', time()+3600*24);
123:
124: $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
125: }
126:
127: public function testCookieIsCleared()
128: {
129: $cookie = new Cookie('foo', 'bar', time()-20);
130:
131: $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
132: }
133:
134: public function testToString()
135: {
136: $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
137: $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
138:
139: $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
140: $this->assertEquals('foo=deleted; expires='.gmdate("D, d-M-Y H:i:s T", time()-31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
141:
142: $cookie = new Cookie('foo', 'bar', 0, '/', '');
143: $this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
144: }
145: }
146: