PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/http-foundation/Tests/CookieTest.php

https://gitlab.com/judielsm/Handora
PHP | 147 lines | 92 code | 30 blank | 25 comment | 0 complexity | 51826aa37c262646624d2c51998609a5 MD5 | raw file
  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. require_once __DIR__.'/ClockMock.php';
  13. /**
  14. * CookieTest.
  15. *
  16. * @author John Kary <john@johnkary.net>
  17. * @author Hugo Hamon <hugo.hamon@sensio.com>
  18. */
  19. class CookieTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function invalidNames()
  22. {
  23. return array(
  24. array(''),
  25. array(',MyName'),
  26. array(';MyName'),
  27. array(' MyName'),
  28. array("\tMyName"),
  29. array("\rMyName"),
  30. array("\nMyName"),
  31. array("\013MyName"),
  32. array("\014MyName"),
  33. );
  34. }
  35. /**
  36. * @dataProvider invalidNames
  37. * @expectedException \InvalidArgumentException
  38. * @covers Symfony\Component\HttpFoundation\Cookie::__construct
  39. */
  40. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  41. {
  42. new Cookie($name);
  43. }
  44. /**
  45. * @expectedException \InvalidArgumentException
  46. */
  47. public function testInvalidExpiration()
  48. {
  49. $cookie = new Cookie('MyCookie', 'foo', 'bar');
  50. }
  51. /**
  52. * @covers Symfony\Component\HttpFoundation\Cookie::getValue
  53. */
  54. public function testGetValue()
  55. {
  56. $value = 'MyValue';
  57. $cookie = new Cookie('MyCookie', $value);
  58. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  59. }
  60. public function testGetPath()
  61. {
  62. $cookie = new Cookie('foo', 'bar');
  63. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  64. }
  65. public function testGetExpiresTime()
  66. {
  67. $cookie = new Cookie('foo', 'bar', 3600);
  68. $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  69. }
  70. public function testConstructorWithDateTime()
  71. {
  72. $expire = new \DateTime();
  73. $cookie = new Cookie('foo', 'bar', $expire);
  74. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  75. }
  76. public function testGetExpiresTimeWithStringValue()
  77. {
  78. $value = '+1 day';
  79. $cookie = new Cookie('foo', 'bar', $value);
  80. $expire = strtotime($value);
  81. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  82. }
  83. public function testGetDomain()
  84. {
  85. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
  86. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  87. }
  88. public function testIsSecure()
  89. {
  90. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
  91. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  92. }
  93. public function testIsHttpOnly()
  94. {
  95. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
  96. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  97. }
  98. public function testCookieIsNotCleared()
  99. {
  100. $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
  101. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  102. }
  103. public function testCookieIsCleared()
  104. {
  105. $cookie = new Cookie('foo', 'bar', time() - 20);
  106. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  107. }
  108. public function testToString()
  109. {
  110. $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  111. $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
  112. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  113. $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
  114. $cookie = new Cookie('foo', 'bar', 0, '/', '');
  115. $this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
  116. }
  117. }