PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/web/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Tests/CookieJarTest.php

https://bitbucket.org/hill2steve/mobileroom
PHP | 191 lines | 144 code | 36 blank | 11 comment | 0 complexity | 41e0ec04df64871c95983cc9b338333e MD5 | raw file
  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\Component\BrowserKit\Tests;
  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($cookie1, $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->updateFromResponse($response);
  52. $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
  53. }
  54. public function testUpdateFromSetCookie()
  55. {
  56. $setCookies = array('foo=foo');
  57. $cookieJar = new CookieJar();
  58. $cookieJar->set(new Cookie('bar', 'bar'));
  59. $cookieJar->updateFromSetCookie($setCookies);
  60. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
  61. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
  62. $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
  63. $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
  64. }
  65. public function testUpdateFromSetCookieWithMultipleCookies()
  66. {
  67. $timestamp = time() + 3600;
  68. $date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
  69. $setCookies = array(sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date));
  70. $cookieJar = new CookieJar();
  71. $cookieJar->updateFromSetCookie($setCookies);
  72. $fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
  73. $barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
  74. $phpCookie = $cookieJar->get('PHPSESSID');
  75. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $fooCookie);
  76. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $barCookie);
  77. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $phpCookie);
  78. $this->assertEquals('foo', $fooCookie->getValue());
  79. $this->assertEquals('bar', $barCookie->getValue());
  80. $this->assertEquals('id', $phpCookie->getValue());
  81. $this->assertEquals($timestamp, $fooCookie->getExpiresTime());
  82. $this->assertNull($barCookie->getExpiresTime());
  83. $this->assertEquals($timestamp, $phpCookie->getExpiresTime());
  84. }
  85. /**
  86. * @dataProvider provideAllValuesValues
  87. */
  88. public function testAllValues($uri, $values)
  89. {
  90. $cookieJar = new CookieJar();
  91. $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
  92. $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
  93. $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
  94. $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
  95. $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
  96. $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
  97. $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
  98. }
  99. public function provideAllValuesValues()
  100. {
  101. return array(
  102. array('http://www.example.com', array('foo_nothing', 'foo_domain')),
  103. array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
  104. array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
  105. array('http://foo.example1.com/', array('foo_nothing')),
  106. array('https://foo.example.com/', array('foo_nothing', 'foo_secure', 'foo_domain')),
  107. array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
  108. array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
  109. );
  110. }
  111. public function testEncodedValues()
  112. {
  113. $cookieJar = new CookieJar();
  114. $cookieJar->set($cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true));
  115. $this->assertEquals(array('foo' => 'bar=baz'), $cookieJar->allValues('/'));
  116. $this->assertEquals(array('foo' => 'bar%3Dbaz'), $cookieJar->allRawValues('/'));
  117. }
  118. public function testCookieExpireWithSameNameButDifferentPaths()
  119. {
  120. $cookieJar = new CookieJar();
  121. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
  122. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
  123. $cookieJar->expire('foo', '/foo');
  124. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  125. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  126. $this->assertEquals(array(), $cookieJar->allValues('http://example.com/foo'));
  127. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
  128. }
  129. public function testCookieExpireWithNullPaths()
  130. {
  131. $cookieJar = new CookieJar();
  132. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/'));
  133. $cookieJar->expire('foo', null);
  134. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  135. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  136. }
  137. public function testCookieWithSameNameButDifferentPaths()
  138. {
  139. $cookieJar = new CookieJar();
  140. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
  141. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
  142. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  143. $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://example.com/foo'));
  144. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
  145. }
  146. public function testCookieWithSameNameButDifferentDomains()
  147. {
  148. $cookieJar = new CookieJar();
  149. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/', 'foo.example.com'));
  150. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/', 'bar.example.com'));
  151. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  152. $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://foo.example.com/'));
  153. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://bar.example.com/'));
  154. }
  155. }