CookieTest.php 5.4 KB

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