CookieJarTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Components\BrowserKit;
  11. use Symfony\Components\BrowserKit\CookieJar;
  12. use Symfony\Components\BrowserKit\Cookie;
  13. use Symfony\Components\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(), array('foo' => array('value' => '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 provideGetValuesValues
  58. */
  59. public function testGetValues($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', 0, '/foo'));
  65. $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', 0, '/', 'example.com'));
  66. $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', 0, '/', '', true));
  67. $this->assertEquals($values, array_keys($cookieJar->getValues($uri)), '->getValues() returns the cookie for a given URI');
  68. }
  69. public function provideGetValuesValues()
  70. {
  71. return array(
  72. array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
  73. array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
  74. array('http://foo.example1.com/', array('foo_nothing')),
  75. array('https://foo.example.com/', array('foo_nothing', 'foo_domain', 'foo_secure')),
  76. array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
  77. );
  78. }
  79. }