Open Monograph Press  3.3.0
ArrayCookieJarTest.php
1 <?php
2 
4 
9 
14 {
18  private $jar;
19 
20  public function setUp()
21  {
22  $this->jar = new ArrayCookieJar();
23  }
24 
25  protected function getTestCookies()
26  {
27  return array(
28  new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'foo.com', 'path' => '/', 'discard' => true)),
29  new Cookie(array('name' => 'test', 'value' => '123', 'domain' => 'baz.com', 'path' => '/foo', 'expires' => 2)),
30  new Cookie(array('name' => 'you', 'value' => '123', 'domain' => 'bar.com', 'path' => '/boo', 'expires' => time() + 1000))
31  );
32  }
33 
37  public function getCookiesDataProvider()
38  {
39  return array(
40  array(array('foo', 'baz', 'test', 'muppet', 'googoo'), '', '', '', false),
41  array(array('foo', 'baz', 'muppet', 'googoo'), '', '', '', true),
42  array(array('googoo'), 'www.example.com', '', '', false),
43  array(array('muppet', 'googoo'), 'test.y.example.com', '', '', false),
44  array(array('foo', 'baz'), 'example.com', '', '', false),
45  array(array('muppet'), 'x.y.example.com', '/acme/', '', false),
46  array(array('muppet'), 'x.y.example.com', '/acme/test/', '', false),
47  array(array('googoo'), 'x.y.example.com', '/test/acme/test/', '', false),
48  array(array('foo', 'baz'), 'example.com', '', '', false),
49  array(array('baz'), 'example.com', '', 'baz', false),
50  );
51  }
52 
53  public function testStoresAndRetrievesCookies()
54  {
55  $cookies = $this->getTestCookies();
56  foreach ($cookies as $cookie) {
57  $this->assertTrue($this->jar->add($cookie));
58  }
59 
60  $this->assertEquals(3, count($this->jar));
61  $this->assertEquals(3, count($this->jar->getIterator()));
62  $this->assertEquals($cookies, $this->jar->all(null, null, null, false, false));
63  }
64 
65  public function testRemovesExpiredCookies()
66  {
67  $cookies = $this->getTestCookies();
68  foreach ($this->getTestCookies() as $cookie) {
69  $this->jar->add($cookie);
70  }
71  $this->jar->removeExpired();
72  $this->assertEquals(array($cookies[0], $cookies[2]), $this->jar->all());
73  }
74 
75  public function testRemovesTemporaryCookies()
76  {
77  $cookies = $this->getTestCookies();
78  foreach ($this->getTestCookies() as $cookie) {
79  $this->jar->add($cookie);
80  }
81  $this->jar->removeTemporary();
82  $this->assertEquals(array($cookies[2]), $this->jar->all());
83  }
84 
85  public function testIsSerializable()
86  {
87  $this->assertEquals('[]', $this->jar->serialize());
88  $this->jar->unserialize('[]');
89  $this->assertEquals(array(), $this->jar->all());
90 
91  $cookies = $this->getTestCookies();
92  foreach ($this->getTestCookies() as $cookie) {
93  $this->jar->add($cookie);
94  }
95 
96  // Remove discard and expired cookies
97  $serialized = $this->jar->serialize();
98  $data = json_decode($serialized, true);
99  $this->assertEquals(1, count($data));
100 
101  $a = new ArrayCookieJar();
102  $a->unserialize($serialized);
103  $this->assertEquals(1, count($a));
104  }
105 
106  public function testRemovesSelectively()
107  {
108  $cookies = $this->getTestCookies();
109  foreach ($this->getTestCookies() as $cookie) {
110  $this->jar->add($cookie);
111  }
112 
113  // Remove foo.com cookies
114  $this->jar->remove('foo.com');
115  $this->assertEquals(2, count($this->jar));
116  // Try again, removing no further cookies
117  $this->jar->remove('foo.com');
118  $this->assertEquals(2, count($this->jar));
119 
120  // Remove bar.com cookies with path of /boo
121  $this->jar->remove('bar.com', '/boo');
122  $this->assertEquals(1, count($this->jar));
123 
124  // Remove cookie by name
125  $this->jar->remove(null, null, 'test');
126  $this->assertEquals(0, count($this->jar));
127  }
128 
129  public function testDoesNotAddIncompleteCookies()
130  {
131  $this->assertEquals(false, $this->jar->add(new Cookie()));
132  $this->assertFalse($this->jar->add(new Cookie(array(
133  'name' => 'foo'
134  ))));
135  $this->assertFalse($this->jar->add(new Cookie(array(
136  'name' => false
137  ))));
138  $this->assertFalse($this->jar->add(new Cookie(array(
139  'name' => true
140  ))));
141  $this->assertFalse($this->jar->add(new Cookie(array(
142  'name' => 'foo',
143  'domain' => 'foo.com'
144  ))));
145  }
146 
147  public function testDoesAddValidCookies()
148  {
149  $this->assertTrue($this->jar->add(new Cookie(array(
150  'name' => 'foo',
151  'domain' => 'foo.com',
152  'value' => 0
153  ))));
154  $this->assertTrue($this->jar->add(new Cookie(array(
155  'name' => 'foo',
156  'domain' => 'foo.com',
157  'value' => 0.0
158  ))));
159  $this->assertTrue($this->jar->add(new Cookie(array(
160  'name' => 'foo',
161  'domain' => 'foo.com',
162  'value' => '0'
163  ))));
164  }
165 
167  {
168  $t = time() + 1000;
169  $data = array(
170  'name' => 'foo',
171  'value' => 'bar',
172  'domain' => '.example.com',
173  'path' => '/',
174  'max_age' => '86400',
175  'port' => array(80, 8080),
176  'version' => '1',
177  'secure' => true,
178  'discard' => true,
179  'expires' => $t
180  );
181 
182  // Make sure that the discard cookie is overridden with the non-discard
183  $this->assertTrue($this->jar->add(new Cookie($data)));
184 
185  unset($data['discard']);
186  $this->assertTrue($this->jar->add(new Cookie($data)));
187  $this->assertEquals(1, count($this->jar));
188 
189  $c = $this->jar->all();
190  $this->assertEquals(false, $c[0]->getDiscard());
191 
192  // Make sure it doesn't duplicate the cookie
193  $this->jar->add(new Cookie($data));
194  $this->assertEquals(1, count($this->jar));
195 
196  // Make sure the more future-ful expiration date supersede the other
197  $data['expires'] = time() + 2000;
198  $this->assertTrue($this->jar->add(new Cookie($data)));
199  $this->assertEquals(1, count($this->jar));
200  $c = $this->jar->all();
201  $this->assertNotEquals($t, $c[0]->getExpires());
202  }
203 
204  public function testOverwritesCookiesThatHaveChanged()
205  {
206  $t = time() + 1000;
207  $data = array(
208  'name' => 'foo',
209  'value' => 'bar',
210  'domain' => '.example.com',
211  'path' => '/',
212  'max_age' => '86400',
213  'port' => array(80, 8080),
214  'version' => '1',
215  'secure' => true,
216  'discard' => true,
217  'expires' => $t
218  );
219 
220  // Make sure that the discard cookie is overridden with the non-discard
221  $this->assertTrue($this->jar->add(new Cookie($data)));
222 
223  $data['value'] = 'boo';
224  $this->assertTrue($this->jar->add(new Cookie($data)));
225  $this->assertEquals(1, count($this->jar));
226 
227  // Changing the value plus a parameter also must overwrite the existing one
228  $data['value'] = 'zoo';
229  $data['secure'] = false;
230  $this->assertTrue($this->jar->add(new Cookie($data)));
231  $this->assertEquals(1, count($this->jar));
232 
233  $c = $this->jar->all();
234  $this->assertEquals('zoo', $c[0]->getValue());
235  }
236 
238  {
239  $response = new Response(200, array(
240  'Set-Cookie' => array(
241  "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
242  "FPCK3=AgBNbvoQAGpGEABZLRAAbFsQAF1tEABkDhAAeO0=; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
243  "CH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1",
244  "CH=AgBNbvoQAAEcEAApuhAAMJcQADQvEAAvGxAALe0QAD6uEAATwhAAC1AQAC8t; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1"
245  )
246  ));
247 
248  $this->jar->addCookiesFromResponse($response);
249  $this->assertEquals(3, count($this->jar));
250  $this->assertEquals(1, count($this->jar->all(null, null, 'fpc')));
251  $this->assertEquals(1, count($this->jar->all(null, null, 'FPCK3')));
252  $this->assertEquals(1, count($this->jar->all(null, null, 'CH')));
253  }
254 
256  {
257  $response = new Response(200, array(
258  'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
259  ));
260  $request = new Request('GET', 'http://www.example.com');
261  $this->jar->addCookiesFromResponse($response, $request);
262  $this->assertEquals(1, count($this->jar));
263  }
264 
265  public function getMatchingCookiesDataProvider()
266  {
267  return array(
268  array('https://example.com', array(0)),
269  array('http://example.com', array()),
270  array('https://example.com:8912', array()),
271  array('https://foo.example.com', array(0)),
272  array('http://foo.example.com/test/acme/', array(4))
273  );
274  }
275 
279  public function testReturnsCookiesMatchingRequests($url, $cookies)
280  {
281  $bag = array(
282  new Cookie(array(
283  'name' => 'foo',
284  'value' => 'bar',
285  'domain' => 'example.com',
286  'path' => '/',
287  'max_age' => '86400',
288  'port' => array(443, 8080),
289  'version' => '1',
290  'secure' => true
291  )),
292  new Cookie(array(
293  'name' => 'baz',
294  'value' => 'foobar',
295  'domain' => 'example.com',
296  'path' => '/',
297  'max_age' => '86400',
298  'port' => array(80, 8080),
299  'version' => '1',
300  'secure' => true
301  )),
302  new Cookie(array(
303  'name' => 'test',
304  'value' => '123',
305  'domain' => 'www.foobar.com',
306  'path' => '/path/',
307  'discard' => true
308  )),
309  new Cookie(array(
310  'name' => 'muppet',
311  'value' => 'cookie_monster',
312  'domain' => '.y.example.com',
313  'path' => '/acme/',
314  'comment' => 'Comment goes here...',
315  'expires' => time() + 86400
316  )),
317  new Cookie(array(
318  'name' => 'googoo',
319  'value' => 'gaga',
320  'domain' => '.example.com',
321  'path' => '/test/acme/',
322  'max_age' => 1500,
323  'version' => 2
324  ))
325  );
326 
327  foreach ($bag as $cookie) {
328  $this->jar->add($cookie);
329  }
330 
331  $request = new Request('GET', $url);
332  $results = $this->jar->getMatchingCookies($request);
333  $this->assertEquals(count($cookies), count($results));
334  foreach ($cookies as $i) {
335  $this->assertContains($bag[$i], $results);
336  }
337  }
338 
343  public function testThrowsExceptionWithStrictMode()
344  {
345  $a = new ArrayCookieJar();
346  $a->setStrictMode(true);
347  $a->add(new Cookie(array(
348  'name' => 'abc:@123',
349  'value' => 'foo',
350  'domain' => 'bar'
351  )));
352  }
353 
354  public function testRemoveExistingCookieIfEmpty()
355  {
356  // Add a cookie that should not be affected
357  $a = new Cookie(array(
358  'name' => 'foo',
359  'value' => 'nope',
360  'domain' => 'foo.com',
361  'path' => '/abc'
362  ));
363  $this->jar->add($a);
364 
365  $data = array(
366  'name' => 'foo',
367  'value' => 'bar',
368  'domain' => 'foo.com',
369  'path' => '/'
370  );
371 
372  $b = new Cookie($data);
373  $this->assertTrue($this->jar->add($b));
374  $this->assertEquals(2, count($this->jar));
375 
376  // Try to re-set the same cookie with no value: assert that cookie is not added
377  $data['value'] = null;
378  $this->assertFalse($this->jar->add(new Cookie($data)));
379  // assert that original cookie has been deleted
380  $cookies = $this->jar->all('foo.com');
381  $this->assertTrue(in_array($a, $cookies, true));
382  $this->assertFalse(in_array($b, $cookies, true));
383  $this->assertEquals(1, count($this->jar));
384  }
385 }
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testOverwritesCookiesThatHaveChanged
testOverwritesCookiesThatHaveChanged()
Definition: ArrayCookieJarTest.php:207
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testDoesAddValidCookies
testDoesAddValidCookies()
Definition: ArrayCookieJarTest.php:150
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testRemovesSelectively
testRemovesSelectively()
Definition: ArrayCookieJarTest.php:109
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testStoresAndRetrievesCookies
testStoresAndRetrievesCookies()
Definition: ArrayCookieJarTest.php:56
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testAddsCookiesFromResponseWithRequest
testAddsCookiesFromResponseWithRequest()
Definition: ArrayCookieJarTest.php:258
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testDoesNotAddIncompleteCookies
testDoesNotAddIncompleteCookies()
Definition: ArrayCookieJarTest.php:132
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\getCookiesDataProvider
getCookiesDataProvider()
Definition: ArrayCookieJarTest.php:40
Guzzle\Http\Message\Response
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\setUp
setUp()
Definition: ArrayCookieJarTest.php:23
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testReturnsCookiesMatchingRequests
testReturnsCookiesMatchingRequests($url, $cookies)
Definition: ArrayCookieJarTest.php:282
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\getTestCookies
getTestCookies()
Definition: ArrayCookieJarTest.php:28
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testRemovesExpiredCookies
testRemovesExpiredCookies()
Definition: ArrayCookieJarTest.php:68
Guzzle\Plugin\Cookie\Cookie
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Plugin/Cookie/Cookie.php:10
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\getMatchingCookiesDataProvider
getMatchingCookiesDataProvider()
Definition: ArrayCookieJarTest.php:268
Guzzle\Http\Message\Request
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php:25
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testRemoveExistingCookieIfEmpty
testRemoveExistingCookieIfEmpty()
Definition: ArrayCookieJarTest.php:357
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testRemovesTemporaryCookies
testRemovesTemporaryCookies()
Definition: ArrayCookieJarTest.php:78
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testThrowsExceptionWithStrictMode
testThrowsExceptionWithStrictMode()
Definition: ArrayCookieJarTest.php:346
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testOverwritesCookiesThatAreOlderOrDiscardable
testOverwritesCookiesThatAreOlderOrDiscardable()
Definition: ArrayCookieJarTest.php:169
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest
Definition: ArrayCookieJarTest.php:13
Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar
Definition: ArrayCookieJar.php:14
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testAddsCookiesFromResponseWithNoRequest
testAddsCookiesFromResponseWithNoRequest()
Definition: ArrayCookieJarTest.php:240
Guzzle\Tests\Plugin\Cookie\CookieJar\ArrayCookieJarTest\testIsSerializable
testIsSerializable()
Definition: ArrayCookieJarTest.php:88
Guzzle\Tests\Plugin\Cookie\CookieJar
Definition: ArrayCookieJarTest.php:3