PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/CookieTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site
PHP | 144 lines | 89 code | 30 blank | 25 comment | 0 complexity | afbe4680ee2750444f72ccab3a3ca5ff 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. /**
  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. /**
  35. * @dataProvider invalidNames
  36. * @expectedException InvalidArgumentException
  37. * @covers Symfony\Component\HttpFoundation\Cookie::__construct
  38. */
  39. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  40. {
  41. new Cookie($name);
  42. }
  43. /**
  44. * @expectedException InvalidArgumentException
  45. */
  46. public function testInvalidExpiration()
  47. {
  48. $cookie = new Cookie('MyCookie', 'foo','bar');
  49. }
  50. /**
  51. * @covers Symfony\Component\HttpFoundation\Cookie::getValue
  52. */
  53. public function testGetValue()
  54. {
  55. $value = 'MyValue';
  56. $cookie = new Cookie('MyCookie', $value);
  57. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  58. }
  59. public function testGetPath()
  60. {
  61. $cookie = new Cookie('foo', 'bar');
  62. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  63. }
  64. public function testGetExpiresTime()
  65. {
  66. $cookie = new Cookie('foo', 'bar', 3600);
  67. $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  68. }
  69. public function testConstructorWithDateTime()
  70. {
  71. $expire = new \DateTime();
  72. $cookie = new Cookie('foo', 'bar', $expire);
  73. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  74. }
  75. public function testGetExpiresTimeWithStringValue()
  76. {
  77. $value = "+1 day";
  78. $cookie = new Cookie('foo', 'bar', $value);
  79. $expire = strtotime($value);
  80. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  81. }
  82. public function testGetDomain()
  83. {
  84. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
  85. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  86. }
  87. public function testIsSecure()
  88. {
  89. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
  90. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  91. }
  92. public function testIsHttpOnly()
  93. {
  94. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
  95. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  96. }
  97. public function testCookieIsNotCleared()
  98. {
  99. $cookie = new Cookie('foo', 'bar', time()+3600*24);
  100. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  101. }
  102. public function testCookieIsCleared()
  103. {
  104. $cookie = new Cookie('foo', 'bar', time()-20);
  105. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  106. }
  107. public function testToString()
  108. {
  109. $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  110. $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
  111. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  112. $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');
  113. }
  114. }