3 class CookieTest
extends PHPUnit_Framework_TestCase
{
16 $this->assertEquals($a, $c->toArray());
17 $this->assertEquals($a, $o->toArray());
20 function testExpiresAsDate() {
22 $c = new http\
Cookie(array("expires" => $d->format(DateTime
::RFC1123
)));
23 $this->assertEquals($d->format("U"), $c->getExpires());
26 function testNumeric() {
27 $c = new http\
Cookie("1=%20; 2=%22; 3=%5D", 0, array(2));
28 $this->assertEquals("1=%20; 3=%5D; 2=%22; ", (string) $c);
32 $c = new http\
Cookie("1=%20; 2=%22; e3=%5D", http\Cookie
::PARSE_RAW
, array(2));
33 $this->assertEquals("1=%2520; e3=%255D; 2=%2522; ", (string) $c);
36 function testSimple() {
37 $orig = new http\
Cookie("key=value");
39 $same = new http\
Cookie($copy);
40 $even = new http\
Cookie($same->toArray());
41 foreach (array($orig, $copy) as $c) {
42 $this->assertEquals("value", $c->getCookie("key"));
43 $this->assertEquals(-1, $c->getExpires());
44 $this->assertEquals(-1, $c->getMaxAge());
45 $this->assertEquals(0, $c->getFlags());
46 $this->assertEquals(null, $c->getPath());
47 $this->assertEquals(null, $c->getDomain());
48 $this->assertEquals(array(), $c->getExtras());
49 $this->assertEquals(array("key" => "value"), $c->getCookies());
50 $this->assertEquals("key=value; ", $c->toString());
71 function testExpires() {
72 $c = new http\
Cookie("this=expires; expires=Tue, 24 Jan 2012 10:35:32 +0100");
73 $this->assertEquals("expires", $c->getCookie("this"));
74 $this->assertEquals(1327397732, $c->getExpires());
78 $this->assertEquals(-1, $o->getExpires());
79 $this->assertNotEquals(-1, $c->getExpires());
81 $this->assertEquals($t, $o->getExpires());
82 $this->assertNotEquals($t, $c->getExpires());
85 "this=expires; expires=%s; ",
87 ->setTimezone(new DateTimezone("UTC"))
88 ->format("D, d M Y H:i:s \\G\\M\\T")
94 function testMaxAge() {
95 $c = new http\
Cookie("this=max-age; max-age=12345");
96 $this->assertEquals("max-age", $c->getCookie("this"));
97 $this->assertEquals(12345, $c->getMaxAge());
101 $this->assertEquals(-1, $o->getMaxAge());
102 $this->assertNotEquals(-1, $c->getMaxAge());
104 $this->assertEquals($t, $o->getMaxAge());
105 $this->assertNotEquals($t, $c->getMaxAge());
107 "this=max-age; max-age=$t; ",
112 function testPath() {
113 $c = new http\
Cookie("this=has a path; path=/down; ");
114 $this->assertEquals("has a path", $c->getCookie("this"));
115 $this->assertEquals("this=has%20a%20path; path=/down; ", (string)$c);
116 $this->assertEquals("/down", $c->getPath());
120 $this->assertEquals(null, $o->getPath());
121 $this->assertNotEquals(null, $c->getPath());
123 $this->assertEquals($p, $o->getPath());
124 $this->assertNotEquals($p, $c->getPath());
125 $this->assertEquals("this=has%20a%20path; path=$p; ", $o->toString());
128 function testDomain() {
129 $c = new http\
Cookie("this=has a domain; domain=.example.com; ");
130 $this->assertEquals("has a domain", $c->getCookie("this"));
131 $this->assertEquals("this=has%20a%20domain; domain=.example.com; ", (string)$c);
132 $this->assertEquals(".example.com", $c->getDomain());
134 $d = "sub.example.com";
136 $this->assertEquals(null, $o->getDomain());
137 $this->assertNotEquals(null, $c->getDomain());
139 $this->assertEquals($d, $o->getDomain());
140 $this->assertNotEquals($d, $c->getDomain());
141 $this->assertEquals("this=has%20a%20domain; domain=$d; ", $o->toString());
144 function testFlags() {
145 $c = new http\
Cookie("icanhas=flags; secure; httpOnly");
146 $this->assertEquals(http\Cookie
::SECURE
, $c->getFlags() & http\Cookie
::SECURE
, "secure");
147 $this->assertEquals(http\Cookie
::HTTPONLY
, $c->getFlags() & http\Cookie
::HTTPONLY
, "httpOnly");
148 $c->setFlags($c->getFlags() ^ http\Cookie
::SECURE
);
149 $this->assertEquals(0, $c->getFlags() & http\Cookie
::SECURE
, "secure");
150 $this->assertEquals(http\Cookie
::HTTPONLY
, $c->getFlags() & http\Cookie
::HTTPONLY
, "httpOnly");
151 $c->setFlags($c->getFlags() ^ http\Cookie
::HTTPONLY
);
152 $this->assertEquals(0, $c->getFlags() & http\Cookie
::SECURE
, "secure");
153 $this->assertEquals(0, $c->getFlags() & http\Cookie
::HTTPONLY
, "httpOnly");
154 $this->assertEquals("icanhas=flags; ", $c->toString());
155 $c->setFlags(http\Cookie
::SECURE|http\Cookie
::HTTPONLY
);
156 $this->assertEquals("icanhas=flags; secure; httpOnly; ", $c->toString());
159 function testExtras() {
160 $c = new http\
Cookie("c1=v1; e0=1; e2=2; c2=v2", 0, array("e0", "e1", "e2"));
161 $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getCookies());
162 $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getExtras());
163 $c->addExtra("e1", 1);
165 $c->setExtra("e3", 123);
166 $this->assertEquals(123, $c->getExtra("e3"));
168 $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getExtras());
169 $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; ", $c->toString());
170 $c->addExtras(array("e3"=>3, "e4"=>4));
171 $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getExtras());
172 $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; e3=3; e4=4; ", $c->toString());
173 $c->setExtras(array("e"=>"x"));
174 $this->assertEquals(array("e"=>"x"), $c->getExtras());
175 $this->assertEquals("c1=v1; c2=v2; e=x; ", $c->toString());
177 $this->assertEquals(array(), $c->getExtras());
178 $this->assertEquals("c1=v1; c2=v2; ", $c->toString());
181 function testCookies() {
182 $c = new http\
Cookie("e0=1; c1=v1; e2=2; c2=v2", 0, array("c0", "c1", "c2"));
183 $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getExtras());
184 $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getCookies());
185 $c->addCookie("e1", 1);
187 $c->setCookie("e3", 123);
188 $this->assertEquals(123, $c->getCookie("e3"));
190 $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getCookies());
191 $this->assertEquals("e2=2; e1=1; c1=v1; c2=v2; ", $c->toString());
192 $c->addCookies(array("e3"=>3, "e4"=>4));
193 $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getCookies());
194 $this->assertEquals("e2=2; e1=1; e3=3; e4=4; c1=v1; c2=v2; ", $c->toString());
195 $c->setCookies(array("e"=>"x"));
196 $this->assertEquals(array("e"=>"x"), $c->getCookies());
197 $this->assertEquals("e=x; c1=v1; c2=v2; ", $c->toString());
199 $this->assertEquals(array(), $c->getCookies());
200 $this->assertEquals("c1=v1; c2=v2; ", $c->toString());