PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php

https://github.com/comfortablynumb/symfony
PHP | 151 lines | 93 code | 28 blank | 30 comment | 0 complexity | 2db8adcfe790c5284ea010cb6214499b 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\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. * @expectedException InvalidArgumentException
  67. */
  68. public function testInvalidExpiration()
  69. {
  70. $cookie = new Cookie('MyCookie', 'foo','bar');
  71. }
  72. /**
  73. * @covers Symfony\Component\HttpFoundation\Cookie::getValue
  74. */
  75. public function testGetValue()
  76. {
  77. $value = 'MyValue';
  78. $cookie = new Cookie('MyCookie', $value);
  79. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  80. }
  81. public function testGetPath()
  82. {
  83. $cookie = new Cookie('foo', 'bar');
  84. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  85. }
  86. public function testGetExpiresTime()
  87. {
  88. $cookie = new Cookie('foo', 'bar', 3600);
  89. $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  90. }
  91. public function testGetDomain()
  92. {
  93. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
  94. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  95. }
  96. public function testIsSecure()
  97. {
  98. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
  99. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  100. }
  101. public function testIsHttpOnly()
  102. {
  103. $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
  104. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  105. }
  106. public function testCookieIsNotCleared()
  107. {
  108. $cookie = new Cookie('foo', 'bar', time()+3600*24);
  109. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  110. }
  111. public function testCookieIsCleared()
  112. {
  113. $cookie = new Cookie('foo', 'bar', time()-20);
  114. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  115. }
  116. public function testToString()
  117. {
  118. $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  119. $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');
  120. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  121. $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');
  122. }
  123. }