暫無描述

CookieTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\HttpFoundation\Tests;
  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. * @group time-sensitive
  19. */
  20. class CookieTest extends \PHPUnit_Framework_TestCase
  21. {
  22. public function invalidNames()
  23. {
  24. return array(
  25. array(''),
  26. array(',MyName'),
  27. array(';MyName'),
  28. array(' MyName'),
  29. array("\tMyName"),
  30. array("\rMyName"),
  31. array("\nMyName"),
  32. array("\013MyName"),
  33. array("\014MyName"),
  34. );
  35. }
  36. /**
  37. * @dataProvider invalidNames
  38. * @expectedException \InvalidArgumentException
  39. */
  40. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  41. {
  42. new Cookie($name);
  43. }
  44. /**
  45. * @expectedException \InvalidArgumentException
  46. */
  47. public function testInvalidExpiration()
  48. {
  49. new Cookie('MyCookie', 'foo', 'bar');
  50. }
  51. public function testNegativeExpirationIsNotPossible()
  52. {
  53. $cookie = new Cookie('foo', 'bar', -100);
  54. $this->assertSame(0, $cookie->getExpiresTime());
  55. }
  56. public function testGetValue()
  57. {
  58. $value = 'MyValue';
  59. $cookie = new Cookie('MyCookie', $value);
  60. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  61. }
  62. public function testGetPath()
  63. {
  64. $cookie = new Cookie('foo', 'bar');
  65. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  66. }
  67. public function testGetExpiresTime()
  68. {
  69. $cookie = new Cookie('foo', 'bar', 3600);
  70. $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  71. }
  72. public function testGetExpiresTimeIsCastToInt()
  73. {
  74. $cookie = new Cookie('foo', 'bar', 3600.9);
  75. $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
  76. }
  77. public function testConstructorWithDateTime()
  78. {
  79. $expire = new \DateTime();
  80. $cookie = new Cookie('foo', 'bar', $expire);
  81. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  82. }
  83. /**
  84. * @requires PHP 5.5
  85. */
  86. public function testConstructorWithDateTimeImmutable()
  87. {
  88. $expire = new \DateTimeImmutable();
  89. $cookie = new Cookie('foo', 'bar', $expire);
  90. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  91. }
  92. public function testGetExpiresTimeWithStringValue()
  93. {
  94. $value = '+1 day';
  95. $cookie = new Cookie('foo', 'bar', $value);
  96. $expire = strtotime($value);
  97. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
  98. }
  99. public function testGetDomain()
  100. {
  101. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
  102. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  103. }
  104. public function testIsSecure()
  105. {
  106. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
  107. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  108. }
  109. public function testIsHttpOnly()
  110. {
  111. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
  112. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  113. }
  114. public function testCookieIsNotCleared()
  115. {
  116. $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
  117. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  118. }
  119. public function testCookieIsCleared()
  120. {
  121. $cookie = new Cookie('foo', 'bar', time() - 20);
  122. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  123. }
  124. public function testToString()
  125. {
  126. $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  127. $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
  128. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  129. $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
  130. $cookie = new Cookie('foo', 'bar', 0, '/', '');
  131. $this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
  132. }
  133. public function testRawCookie()
  134. {
  135. $cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
  136. $this->assertFalse($cookie->isRaw());
  137. $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
  138. $cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
  139. $this->assertTrue($cookie->isRaw());
  140. $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
  141. }
  142. }