PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

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