PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Tests/CookieTest.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 179 lines | 136 code | 29 blank | 14 comment | 0 complexity | 2fa2f6bc2022ed9d412e10b19afff8b1 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\Cookie;
  12. class CookieTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getTestsForToFromString
  16. */
  17. public function testToFromString($cookie, $url = null)
  18. {
  19. $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
  20. }
  21. public function getTestsForToFromString()
  22. {
  23. return array(
  24. array('foo=bar; path=/'),
  25. array('foo=bar; path=/foo'),
  26. array('foo=bar; domain=google.com; path=/'),
  27. array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
  28. array('foo=bar; path=/; httponly'),
  29. array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
  30. array('foo=bar=baz; path=/'),
  31. array('foo=bar%3Dbaz; path=/'),
  32. );
  33. }
  34. public function testFromStringIgnoreSecureFlag()
  35. {
  36. $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
  37. $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
  38. }
  39. /**
  40. * @dataProvider getExpireCookieStrings
  41. */
  42. public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
  43. {
  44. $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
  45. }
  46. public function getExpireCookieStrings()
  47. {
  48. return array(
  49. array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
  50. array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
  51. array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
  52. array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
  53. array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
  54. array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
  55. array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
  56. array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
  57. );
  58. }
  59. public function testFromStringWithCapitalization()
  60. {
  61. $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
  62. $this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
  63. $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
  64. }
  65. public function testFromStringWithUrl()
  66. {
  67. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
  68. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com'));
  69. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com?foo'));
  70. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
  71. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  72. $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  73. }
  74. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  75. {
  76. $this->setExpectedException('InvalidArgumentException');
  77. Cookie::FromString('foo');
  78. }
  79. public function testFromStringThrowsAnExceptionIfCookieDateIsNotValid()
  80. {
  81. $this->setExpectedException('InvalidArgumentException');
  82. Cookie::FromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
  83. }
  84. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  85. {
  86. $this->setExpectedException('InvalidArgumentException');
  87. Cookie::FromString('foo=bar', 'foobar');
  88. }
  89. public function testGetName()
  90. {
  91. $cookie = new Cookie('foo', 'bar');
  92. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  93. }
  94. public function testGetValue()
  95. {
  96. $cookie = new Cookie('foo', 'bar');
  97. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  98. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  99. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  100. }
  101. public function testGetRawValue()
  102. {
  103. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  104. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  105. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  106. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  107. }
  108. public function testGetPath()
  109. {
  110. $cookie = new Cookie('foo', 'bar', 0);
  111. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  112. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  113. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  114. }
  115. public function testGetDomain()
  116. {
  117. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  118. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  119. }
  120. public function testIsSecure()
  121. {
  122. $cookie = new Cookie('foo', 'bar');
  123. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  124. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  125. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  126. }
  127. public function testIsHttponly()
  128. {
  129. $cookie = new Cookie('foo', 'bar');
  130. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  131. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  132. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  133. }
  134. public function testGetExpiresTime()
  135. {
  136. $cookie = new Cookie('foo', 'bar');
  137. $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  138. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  139. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  140. }
  141. public function testIsExpired()
  142. {
  143. $cookie = new Cookie('foo', 'bar');
  144. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  145. $cookie = new Cookie('foo', 'bar', time() - 86400);
  146. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  147. $cookie = new Cookie('foo', 'bar', 0);
  148. $this->assertFalse($cookie->isExpired());
  149. }
  150. }