PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/lithium/tests/cases/storage/session/adapter/CookieTest.php

https://github.com/prestes/chegamos
PHP | 202 lines | 145 code | 46 blank | 11 comment | 2 complexity | d38715b171dd962d0f39b89b817e3949 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of Rad, Inc. (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\storage\session\adapter;
  9. use \lithium\storage\session\adapter\Cookie;
  10. class CookieTest extends \lithium\test\Unit {
  11. /**
  12. * Skip the test if running under CLI.
  13. *
  14. * @return void
  15. */
  16. public function skip() {
  17. $sapi = PHP_SAPI;
  18. $message = 'Cookie tests cannot be run via command-line interface.';
  19. $this->skipIf($sapi === 'cli', $message);
  20. }
  21. public function setUp() {
  22. $this->Cookie = new Cookie();
  23. $path = explode('/', LITHIUM_APP_PATH);
  24. $this->name = end($path) . 'cookie';
  25. }
  26. public function tearDown() {
  27. $this->_destroyCookie();
  28. }
  29. protected function _destroyCookie($name = null) {
  30. if (!$name) {
  31. $name = session_name();
  32. }
  33. $settings = session_get_cookie_params();
  34. setcookie(
  35. $name, '', time() - 1000, $settings['path'], $settings['domain'],
  36. $settings['secure'], $settings['httponly']
  37. );
  38. if (session_id()) {
  39. session_destroy();
  40. }
  41. $_COOKIE = array();
  42. }
  43. public function testEnabled() {
  44. $this->assertTrue($this->Cookie->isEnabled());
  45. }
  46. public function testKey() {
  47. $this->assertEqual($this->name, $this->Cookie->key());
  48. }
  49. public function testIsStarted() {
  50. $this->assertTrue($this->Cookie->isStarted());
  51. }
  52. public function testWriteDefaultParameters() {
  53. $key = 'write';
  54. $value = 'value to be written';
  55. $expires = "+2 days";
  56. $path = '/';
  57. $closure = $this->Cookie->write($key, $value);
  58. $this->assertTrue(is_callable($closure));
  59. $params = compact('key', 'value');
  60. $result = $closure($this->Cookie, $params, null);
  61. $this->assertCookie(compact('key', 'value', 'expires', 'path'));
  62. }
  63. public function testCustomCookieName() {
  64. $Cookie = new Cookie(array('name' => 'test'));
  65. $this->assertEqual('test', $Cookie->key());
  66. }
  67. public function testWriteArrayData() {
  68. $key = 'user';
  69. $value = array('email' => 'test@localhost', 'name' => 'Testy McTesterson');
  70. $expires = "+2 days";
  71. $path = '/';
  72. $closure = $this->Cookie->write($key, $value);
  73. $this->assertTrue(is_callable($closure));
  74. $params = compact('key', 'value');
  75. $result = $closure($this->Cookie, $params, null);
  76. $expected = compact('expires');
  77. $expected += array('key' => 'user.email', 'value' => 'test@localhost');
  78. $this->assertCookie($expected, headers_list());
  79. }
  80. public function testReadDotSyntax() {
  81. $key = 'read.test';
  82. $value = 'value to be read';
  83. $_COOKIE[$this->name]['read']['test'] = $value;
  84. $closure = $this->Cookie->read($key);
  85. $this->assertTrue(is_callable($closure));
  86. $params = compact('key');
  87. $result = $closure($this->Cookie, $params, null);
  88. $this->assertEqual($value, $result);
  89. $result = $closure($this->Cookie, array('key' => null), null);
  90. $this->assertEqual($_COOKIE, $result);
  91. $key = 'does_not_exist';
  92. $closure = $this->Cookie->read($key);
  93. $this->assertTrue(is_callable($closure));
  94. $params = compact('key');
  95. $result = $closure($this->Cookie, $params, null);
  96. $this->assertNull($result);
  97. }
  98. public function testWriteCustomParameters() {
  99. $key = 'write';
  100. $value = 'value to be written';
  101. $expires = "+1 day";
  102. $path = '/';
  103. $options = array('expire' => $expires);
  104. $closure = $this->Cookie->write($key, $value, $options);
  105. $this->assertTrue(is_callable($closure));
  106. $params = compact('key', 'value', 'options');
  107. $result = $closure($this->Cookie, $params, null);
  108. $this->assertCookie(compact('key', 'value', 'expires', 'path'));
  109. }
  110. public function testRead() {
  111. $key = 'read';
  112. $value = 'value to be read';
  113. $_COOKIE[$this->name][$key] = $value;
  114. $closure = $this->Cookie->read($key);
  115. $this->assertTrue(is_callable($closure));
  116. $params = compact('key');
  117. $result = $closure($this->Cookie, $params, null);
  118. $this->assertEqual($value, $result);
  119. $result = $closure($this->Cookie, array('key' => null), null);
  120. $this->assertEqual($_COOKIE, $result);
  121. $key = 'does_not_exist';
  122. $closure = $this->Cookie->read($key);
  123. $this->assertTrue(is_callable($closure));
  124. $params = compact('key');
  125. $result = $closure($this->Cookie, $params, null);
  126. $this->assertNull($result);
  127. }
  128. public function testCheck() {
  129. $key = 'read';
  130. $value = 'value to be read';
  131. $_COOKIE[$this->name][$key] = $value;
  132. $closure = $this->Cookie->check($key);
  133. $this->assertTrue(is_callable($closure));
  134. $params = compact('key');
  135. $result = $closure($this->Cookie, $params, null);
  136. $this->assertTrue($result);
  137. $key = 'does_not_exist';
  138. $closure = $this->Cookie->check($key);
  139. $this->assertTrue(is_callable($closure));
  140. $params = compact('key');
  141. $result = $closure($this->Cookie, $params, null);
  142. $this->assertFalse($result);
  143. }
  144. public function testDeleteNonExistentValue() {
  145. $key = 'delete';
  146. $value = 'deleted';
  147. $path = '/';
  148. $closure = $this->Cookie->delete($key);
  149. $this->assertTrue(is_callable($closure));
  150. $params = compact('key');
  151. $result = $closure($this->Cookie, $params, null);
  152. $this->assertNull($result);
  153. $this->assertCookie(compact('key', 'value', 'path'));
  154. }
  155. }
  156. ?>