CookieTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. /**
  13. * CookieTest
  14. *
  15. * @author John Kary <john@johnkary.net>
  16. * @author Hugo Hamon <hugo.hamon@sensio.com>
  17. */
  18. class CookieTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function invalidNames()
  21. {
  22. return array(
  23. array(''),
  24. array(",MyName"),
  25. array(";MyName"),
  26. array(" MyName"),
  27. array("\tMyName"),
  28. array("\rMyName"),
  29. array("\nMyName"),
  30. array("\013MyName"),
  31. array("\014MyName"),
  32. );
  33. }
  34. public function invalidValues()
  35. {
  36. return array(
  37. array(",MyValue"),
  38. array(";MyValue"),
  39. array(" MyValue"),
  40. array("\tMyValue"),
  41. array("\rMyValue"),
  42. array("\nMyValue"),
  43. array("\013MyValue"),
  44. array("\014MyValue"),
  45. );
  46. }
  47. /**
  48. * @dataProvider invalidNames
  49. * @expectedException InvalidArgumentException
  50. * @covers Symfony\Component\HttpFoundation\Cookie::__construct
  51. */
  52. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  53. {
  54. new Cookie($name);
  55. }
  56. /**
  57. * @dataProvider invalidValues
  58. * @expectedException InvalidArgumentException
  59. * @covers Symfony\Component\HttpFoundation\Cookie::__construct
  60. */
  61. public function testInstantiationThrowsExceptionIfCookieValueContainsInvalidCharacters($value)
  62. {
  63. new Cookie('MyCookie', $value);
  64. }
  65. /**
  66. * @covers Symfony\Component\HttpFoundation\Cookie::getValue
  67. */
  68. public function testGetValue()
  69. {
  70. $value = 'MyValue';
  71. $cookie = new Cookie('MyCookie', $value);
  72. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  73. }
  74. public function testGetPath()
  75. {
  76. $cookie = new Cookie('foo', 'bar');
  77. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  78. }
  79. public function testGetExpires()
  80. {
  81. $cookie = new Cookie('foo', 'bar', 3600);
  82. $this->assertEquals(3600, $cookie->getExpire(), '->getExpire() returns the expire date');
  83. }
  84. public function testGetDomain()
  85. {
  86. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
  87. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  88. }
  89. public function testIsSecure()
  90. {
  91. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
  92. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  93. }
  94. public function testIsHttpOnly()
  95. {
  96. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
  97. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  98. }
  99. public function testCookieIsNotCleared()
  100. {
  101. $cookie = new Cookie('foo', 'bar', time()+3600*24);
  102. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  103. }
  104. public function testCookieIsCleared()
  105. {
  106. $cookie = new Cookie('foo', 'bar', time()-20);
  107. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  108. }
  109. }