CookieJarTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\BrowserKit;
  11. use Symfony\Component\BrowserKit\CookieJar;
  12. use Symfony\Component\BrowserKit\Cookie;
  13. use Symfony\Component\BrowserKit\Response;
  14. class CookieJarTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSetGet()
  17. {
  18. $cookieJar = new CookieJar();
  19. $cookieJar->set($cookie = new Cookie('foo', 'bar'));
  20. $this->assertEquals($cookie, $cookieJar->get('foo'), '->set() sets a cookie');
  21. $this->assertNull($cookieJar->get('foobar'), '->get() returns null if the cookie does not exist');
  22. $cookieJar->set($cookie = new Cookie('foo', 'bar', time() - 86400));
  23. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  24. }
  25. public function testExpire()
  26. {
  27. $cookieJar = new CookieJar();
  28. $cookieJar->set($cookie = new Cookie('foo', 'bar'));
  29. $cookieJar->expire('foo');
  30. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  31. }
  32. public function testAll()
  33. {
  34. $cookieJar = new CookieJar();
  35. $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
  36. $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
  37. $this->assertEquals(array('foo' => $cookie1, 'bar' => $cookie2), $cookieJar->all(), '->all() returns all cookies in the jar');
  38. }
  39. public function testClear()
  40. {
  41. $cookieJar = new CookieJar();
  42. $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
  43. $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
  44. $cookieJar->clear();
  45. $this->assertEquals(array(), $cookieJar->all(), '->clear() expires all cookies');
  46. }
  47. public function testUpdateFromResponse()
  48. {
  49. $response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
  50. $cookieJar = new CookieJar();
  51. $cookieJar->set(new Cookie('bar', 'bar'));
  52. $cookieJar->updateFromResponse($response);
  53. $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
  54. $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromResponse() keeps existing cookies');
  55. }
  56. /**
  57. * @dataProvider provideAllValuesValues
  58. */
  59. public function testAllValues($uri, $values)
  60. {
  61. $cookieJar = new CookieJar();
  62. $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
  63. $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
  64. $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
  65. $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
  66. $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
  67. $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
  68. $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
  69. }
  70. public function provideAllValuesValues()
  71. {
  72. return array(
  73. array('http://www.example.com', array('foo_nothing', 'foo_domain')),
  74. array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
  75. array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
  76. array('http://foo.example1.com/', array('foo_nothing')),
  77. array('https://foo.example.com/', array('foo_nothing', 'foo_domain', 'foo_secure')),
  78. array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
  79. array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
  80. );
  81. }
  82. public function testEncodedValues()
  83. {
  84. $cookieJar = new CookieJar();
  85. $cookieJar->set($cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true));
  86. $this->assertEquals(array('foo' => 'bar=baz'), $cookieJar->allValues('/'));
  87. $this->assertEquals(array('foo' => 'bar%3Dbaz'), $cookieJar->allRawValues('/'));
  88. }
  89. }