CookieTest.php 4.3 KB

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