CookieTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\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'),
  25. array('foo=bar; expires=Fri, 31-Dec-2010 23:59:59 GMT'),
  26. array('foo=bar; path=/foo'),
  27. array('foo=bar; domain=google.com'),
  28. array('foo=bar; domain=example.com; secure', 'https://example.com/'),
  29. array('foo=bar; httponly'),
  30. array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
  31. array('foo=bar=baz'),
  32. array('foo=bar%3Dbaz'),
  33. );
  34. }
  35. public function testFromStringIgnoreSecureFlag()
  36. {
  37. $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
  38. $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
  39. }
  40. public function testFromStringWithUrl()
  41. {
  42. $this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
  43. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
  44. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  45. $this->assertEquals('foo=bar; domain=www.myotherexample.com', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  46. }
  47. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  48. {
  49. $this->setExpectedException('InvalidArgumentException');
  50. Cookie::FromString('foo');
  51. }
  52. public function testFromStringThrowsAnExceptionIfCookieDateIsNotValid()
  53. {
  54. $this->setExpectedException('InvalidArgumentException');
  55. Cookie::FromString('foo=bar; expires=foo');
  56. }
  57. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  58. {
  59. $this->setExpectedException('InvalidArgumentException');
  60. Cookie::FromString('foo=bar', 'foobar');
  61. }
  62. public function testGetName()
  63. {
  64. $cookie = new Cookie('foo', 'bar');
  65. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  66. }
  67. public function testGetValue()
  68. {
  69. $cookie = new Cookie('foo', 'bar');
  70. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  71. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  72. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  73. }
  74. public function testGetRawValue()
  75. {
  76. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  77. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  78. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  79. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  80. }
  81. public function testGetPath()
  82. {
  83. $cookie = new Cookie('foo', 'bar', 0);
  84. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  85. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  86. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  87. }
  88. public function testGetDomain()
  89. {
  90. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  91. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  92. }
  93. public function testIsSecure()
  94. {
  95. $cookie = new Cookie('foo', 'bar');
  96. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  97. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  98. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  99. }
  100. public function testIsHttponly()
  101. {
  102. $cookie = new Cookie('foo', 'bar');
  103. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  104. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  105. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  106. }
  107. public function testGetExpiresTime()
  108. {
  109. $cookie = new Cookie('foo', 'bar');
  110. $this->assertEquals(null, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  111. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  112. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  113. }
  114. public function testIsExpired()
  115. {
  116. $cookie = new Cookie('foo', 'bar');
  117. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  118. $cookie = new Cookie('foo', 'bar', time() - 86400);
  119. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  120. $cookie = new Cookie('foo', 'bar', 0);
  121. $this->assertFalse($cookie->isExpired());
  122. }
  123. }