PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/UnionOfRAD/lithium
PHP | 276 lines | 204 code | 62 blank | 10 comment | 2 complexity | 02ec2f14ee5b70734f7791dbb013a021 MD5 | raw file
  1. <?php
  2. /**
  3. * li₃: the most RAD framework for PHP (http://li3.me)
  4. *
  5. * Copyright 2009, Union of RAD. All rights reserved. This source
  6. * code is distributed under the terms of the BSD 3-Clause License.
  7. * The full license text can be found in the LICENSE.txt file.
  8. */
  9. namespace lithium\tests\cases\storage\session\adapter;
  10. use lithium\util\Inflector;
  11. use lithium\storage\session\adapter\Cookie;
  12. use lithium\core\Libraries;
  13. class CookieTest extends \lithium\test\Unit {
  14. public $cookie;
  15. public $name = 'testcookie';
  16. /**
  17. * Skip the test if running under CLI.
  18. */
  19. public function skip() {
  20. $sapi = PHP_SAPI;
  21. $message = 'Cookie tests cannot be run via command-line interface.';
  22. $this->skipIf($sapi === 'cli', $message);
  23. }
  24. public function setUp() {
  25. $this->cookie = new Cookie(['name' => $this->name]);
  26. }
  27. public function tearDown() {
  28. $this->_destroyCookie($this->name);
  29. $cookies = array_keys($_COOKIE);
  30. foreach ($cookies as $cookie) {
  31. setcookie($cookie, "", time()-1);
  32. }
  33. }
  34. protected function _destroyCookie($name = null) {
  35. if (!$name) {
  36. $name = session_name();
  37. }
  38. $settings = session_get_cookie_params();
  39. setcookie(
  40. $name, '', time() - 1000, $settings['path'], $settings['domain'],
  41. $settings['secure'], $settings['httponly']
  42. );
  43. if (session_id()) {
  44. session_destroy();
  45. }
  46. $_COOKIE = [];
  47. }
  48. public function testEnabled() {
  49. $this->assertTrue($this->cookie->isEnabled());
  50. }
  51. public function testKey() {
  52. $this->assertEqual($this->name, $this->cookie->key());
  53. }
  54. public function testIsStarted() {
  55. $this->assertTrue($this->cookie->isStarted());
  56. }
  57. public function testWriteDefaultParameters() {
  58. $key = 'write';
  59. $value = 'value to be written';
  60. $expires = "+2 days";
  61. $path = '/';
  62. $closure = $this->cookie->write($key, $value);
  63. $this->assertInternalType('callable', $closure);
  64. $params = compact('key', 'value');
  65. $result = $closure($params);
  66. $this->assertCookie(compact('key', 'value', 'expires', 'path'));
  67. }
  68. public function testCustomCookieName() {
  69. $cookie = new Cookie(['name' => 'test']);
  70. $this->assertEqual('test', $cookie->key());
  71. }
  72. public function testWriteArrayData() {
  73. $key = 'user';
  74. $value = [
  75. 'email' => 'test@localhost',
  76. 'name' => 'Testy McTesterson',
  77. 'address' => ['country' => 'Iran', 'city' => 'Mashhad']
  78. ];
  79. $expires = "+2 days";
  80. $path = '/';
  81. $closure = $this->cookie->write($key, $value);
  82. $this->assertInternalType('callable', $closure);
  83. $params = compact('key', 'value');
  84. $result = $closure($params);
  85. $expected = compact('expires');
  86. $expected += ['key' => 'user.email', 'value' => 'test@localhost'];
  87. $this->assertCookie($expected);
  88. $expected = compact('expires');
  89. $expected += ['key' => 'user.address.country', 'value' => 'Iran'];
  90. $this->assertCookie($expected);
  91. }
  92. public function testReadDotSyntax() {
  93. $key = 'read.test';
  94. $value = 'value to be read';
  95. $_COOKIE[$this->name]['read']['test'] = $value;
  96. $closure = $this->cookie->read($key);
  97. $this->assertInternalType('callable', $closure);
  98. $params = compact('key');
  99. $result = $closure($params, null);
  100. $this->assertEqual($value, $result);
  101. $result = $closure(['key' => null], null);
  102. $this->assertEqual($_COOKIE[$this->name], $result);
  103. $key = 'does.not.exist';
  104. $closure = $this->cookie->read($key);
  105. $this->assertInternalType('callable', $closure);
  106. $params = compact('key');
  107. $result = $closure($params, null);
  108. $this->assertNull($result);
  109. }
  110. public function testWriteCustomParameters() {
  111. $key = 'write';
  112. $value = 'value to be written';
  113. $expires = "+1 day";
  114. $path = '/';
  115. $options = ['expire' => $expires];
  116. $closure = $this->cookie->write($key, $value, $options);
  117. $this->assertInternalType('callable', $closure);
  118. $params = compact('key', 'value', 'options');
  119. $result = $closure($params);
  120. $this->assertCookie(compact('key', 'value', 'expires', 'path'));
  121. }
  122. public function testRead() {
  123. $key = 'read';
  124. $value = 'value to be read';
  125. $_COOKIE[$this->name][$key] = $value;
  126. $closure = $this->cookie->read($key);
  127. $this->assertInternalType('callable', $closure);
  128. $params = compact('key');
  129. $result = $closure($params, null);
  130. $this->assertEqual($value, $result);
  131. $result = $closure(['key' => null], null);
  132. $this->assertEqual($_COOKIE[$this->name], $result);
  133. $key = 'does_not_exist';
  134. $closure = $this->cookie->read($key);
  135. $this->assertInternalType('callable', $closure);
  136. $params = compact('key');
  137. $result = $closure($params, null);
  138. $this->assertNull($result);
  139. }
  140. public function testCheck() {
  141. $key = 'read';
  142. $value = 'value to be read';
  143. $_COOKIE[$this->name][$key] = $value;
  144. $closure = $this->cookie->check($key);
  145. $this->assertInternalType('callable', $closure);
  146. $params = compact('key');
  147. $result = $closure($params, null);
  148. $this->assertTrue($result);
  149. $key = 'does_not_exist';
  150. $closure = $this->cookie->check($key);
  151. $this->assertInternalType('callable', $closure);
  152. $params = compact('key');
  153. $result = $closure($params, null);
  154. $this->assertFalse($result);
  155. }
  156. public function testClearCookie() {
  157. $key = 'clear_key';
  158. $value = 'clear_value';
  159. $_COOKIE[$this->name][$key] = $value;
  160. $closure = $this->cookie->check($key);
  161. $this->assertInternalType('callable', $closure);
  162. $params = compact('key');
  163. $result = $closure($params, null);
  164. $this->assertTrue($result);
  165. $closure = $this->cookie->clear();
  166. $this->assertInternalType('callable', $closure);
  167. $params = [];
  168. $result = $closure($params, null);
  169. $this->assertTrue($result);
  170. $this->assertNoCookie(compact('key', 'value'));
  171. }
  172. public function testDeleteArrayData() {
  173. $key = 'user';
  174. $value = ['email' => 'user@localhost', 'name' => 'Ali'];
  175. $_COOKIE[$this->name][$key] = $value;
  176. $closure = $this->cookie->delete($key);
  177. $this->assertInternalType('callable', $closure);
  178. $params = compact('key');
  179. $result = $closure($params, null);
  180. $this->assertTrue($result);
  181. $expected = ['key' => 'user.name', 'value' => 'deleted'];
  182. $this->assertCookie($expected);
  183. $expected = ['key' => 'user.email', 'value' => 'deleted'];
  184. $this->assertCookie($expected);
  185. }
  186. public function testDeleteNonExistentValue() {
  187. $key = 'delete';
  188. $value = 'deleted';
  189. $path = '/';
  190. $closure = $this->cookie->delete($key);
  191. $this->assertInternalType('callable', $closure);
  192. $params = compact('key');
  193. $result = $closure($params, null);
  194. $this->assertTrue($result);
  195. $this->assertCookie(compact('key', 'value', 'path'));
  196. }
  197. public function testDefaultCookieName() {
  198. $cookie = new Cookie();
  199. $expected = Inflector::slug(basename(Libraries::get(true, 'path'))) . 'cookie';
  200. $this->assertEqual($expected, $cookie->key());
  201. }
  202. public function testBadWrite() {
  203. $cookie = new Cookie(['expire' => null]);
  204. $this->assertNull($cookie->write('bad', 'val'));
  205. }
  206. public function testNameWithDotCookie() {
  207. $cookie = new Cookie(['name' => 'my.name']);
  208. $key = 'key';
  209. $value = 'value';
  210. $result = $cookie->write($key, $value)->__invoke(compact('key', 'value'));
  211. $this->assertCookie(compact('key', 'value'));
  212. }
  213. }
  214. ?>