cookie.test.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php namespace Laravel;
  2. use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
  3. /**
  4. * Stub the global setcookie method into the Laravel namespace.
  5. */
  6. function setcookie($name, $value, $time, $path, $domain, $secure)
  7. {
  8. $_SERVER['cookie.stub'][$name] = compact('name', 'value', 'time', 'path', 'domain', 'secure');
  9. }
  10. function headers_sent()
  11. {
  12. return $_SERVER['function.headers_sent'];
  13. }
  14. class CookieTest extends \PHPUnit_Framework_TestCase {
  15. /**
  16. * Setup the test environment.
  17. */
  18. public function setUp()
  19. {
  20. Cookie::$jar = array();
  21. }
  22. /**
  23. * Tear down the test environment.
  24. */
  25. public function tearDown()
  26. {
  27. Cookie::$jar = array();
  28. }
  29. /**
  30. * Set one of the $_SERVER variables.
  31. *
  32. * @param string $key
  33. * @param string $value
  34. */
  35. protected function setServerVar($key, $value)
  36. {
  37. $_SERVER[$key] = $value;
  38. $this->restartRequest();
  39. }
  40. /**
  41. * Reinitialize the global request.
  42. *
  43. * @return void
  44. */
  45. protected function restartRequest()
  46. {
  47. // FIXME: Ugly hack, but old contents from previous requests seem to
  48. // trip up the Foundation class.
  49. $_FILES = array();
  50. Request::$foundation = RequestFoundation::createFromGlobals();
  51. }
  52. /**
  53. * Test Cookie::has method.
  54. *
  55. * @group laravel
  56. */
  57. public function testHasMethodIndicatesIfCookieInSet()
  58. {
  59. Cookie::$jar['foo'] = array('value' => Cookie::hash('bar').'+bar');
  60. $this->assertTrue(Cookie::has('foo'));
  61. $this->assertFalse(Cookie::has('bar'));
  62. Cookie::put('baz', 'foo');
  63. $this->assertTrue(Cookie::has('baz'));
  64. }
  65. /**
  66. * Test the Cookie::get method.
  67. *
  68. * @group laravel
  69. */
  70. public function testGetMethodCanReturnValueOfCookies()
  71. {
  72. Cookie::$jar['foo'] = array('value' => Cookie::hash('bar').'+bar');
  73. $this->assertEquals('bar', Cookie::get('foo'));
  74. Cookie::put('bar', 'baz');
  75. $this->assertEquals('baz', Cookie::get('bar'));
  76. }
  77. /**
  78. * Test Cookie::forever method.
  79. *
  80. * @group laravel
  81. */
  82. public function testForeverShouldUseATonOfMinutes()
  83. {
  84. Cookie::forever('foo', 'bar');
  85. $this->assertEquals(Cookie::hash('bar').'+bar', Cookie::$jar['foo']['value']);
  86. // Shouldn't be able to test this cause while we indicate -2000 seconds
  87. // cookie expiration store timestamp.
  88. // $this->assertEquals(525600, Cookie::$jar['foo']['expiration']);
  89. $this->setServerVar('HTTPS', 'on');
  90. Cookie::forever('bar', 'baz', 'path', 'domain', true);
  91. $this->assertEquals('path', Cookie::$jar['bar']['path']);
  92. $this->assertEquals('domain', Cookie::$jar['bar']['domain']);
  93. $this->assertTrue(Cookie::$jar['bar']['secure']);
  94. $this->setServerVar('HTTPS', 'off');
  95. }
  96. /**
  97. * Test the Cookie::forget method.
  98. *
  99. * @group laravel
  100. */
  101. public function testForgetSetsCookieWithExpiration()
  102. {
  103. Cookie::forget('bar', 'path', 'domain');
  104. // Shouldn't be able to test this cause while we indicate -2000 seconds
  105. // cookie expiration store timestamp.
  106. //$this->assertEquals(-2000, Cookie::$jar['bar']['expiration']);
  107. $this->assertEquals('path', Cookie::$jar['bar']['path']);
  108. $this->assertEquals('domain', Cookie::$jar['bar']['domain']);
  109. $this->assertFalse(Cookie::$jar['bar']['secure']);
  110. }
  111. }