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

https://github.com/greut/framework · PHP · 290 lines · 207 code · 75 blank · 8 comment · 2 complexity · ae02e02dc419f009b4ef02eee1bab773 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2011, 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\core\Libraries;
  10. use lithium\storage\session\adapter\Php;
  11. use lithium\tests\mocks\storage\session\adapter\MockPhp;
  12. class PhpTest extends \lithium\test\Unit {
  13. public function setUp() {
  14. $this->_session = isset($_SESSION) ? $_SESSION : array();
  15. $this->_destroySession();
  16. $this->Php = new Php();
  17. $this->_destroySession();
  18. /* Garbage collection */
  19. $this->_gc_divisor = ini_get('session.gc_divisor');
  20. ini_set('session.gc_divisor', '1');
  21. }
  22. public function tearDown() {
  23. $this->_destroySession();
  24. /* Revert to original garbage collection probability */
  25. ini_set('session.gc_divisor', $this->_gc_divisor);
  26. $_SESSION = $this->_session;
  27. }
  28. protected function _destroySession($name = null) {
  29. if (!$name) {
  30. $name = session_name();
  31. }
  32. $settings = session_get_cookie_params();
  33. setcookie(
  34. $name, '', time() - 1000, $settings['path'], $settings['domain'],
  35. $settings['secure'], $settings['httponly']
  36. );
  37. if (session_id()) {
  38. session_destroy();
  39. }
  40. $_SESSION = array();
  41. }
  42. public function testEnabled() {
  43. $php = $this->Php;
  44. $this->_destroySession(session_name());
  45. $this->assertFalse($php::enabled());
  46. }
  47. public function testInit() {
  48. $id = session_id();
  49. $this->assertTrue(empty($id));
  50. $result = ini_get('session.name');
  51. $path = explode('/', LITHIUM_APP_PATH);
  52. $this->assertEqual(end($path), $result);
  53. $result = ini_get('session.cookie_lifetime');
  54. $this->assertEqual(0, (integer) $result);
  55. }
  56. public function testCustomConfiguration() {
  57. $config = array(
  58. 'session.name' => 'awesome_name', 'session.cookie_lifetime' => 1200,
  59. 'session.cookie_domain' => 'awesome.domain',
  60. 'session.save_path' => Libraries::get(true, 'resources') . '/tmp/',
  61. 'somebad.configuration' => 'whoops'
  62. );
  63. $adapter = new Php($config);
  64. $result = ini_get('session.name');
  65. $this->assertEqual($config['session.name'], $result);
  66. $result = ini_get('session.cookie_lifetime');
  67. $this->assertEqual($config['session.cookie_lifetime'], (integer) $result);
  68. $result = ini_get('session.cookie_domain');
  69. $this->assertEqual($config['session.cookie_domain'], $result);
  70. $result = ini_get('session.cookie_secure');
  71. $this->assertFalse($result);
  72. $result = ini_get('session.cookie_httponly');
  73. $this->assertFalse($result);
  74. $result = ini_get('session.save_path');
  75. $this->assertEqual($config['session.save_path'], $result);
  76. $result = ini_get('somebad.configuration');
  77. $this->assertFalse($result);
  78. }
  79. public function testIsStarted() {
  80. $result = $this->Php->isStarted();
  81. $this->assertFalse($result);
  82. $this->Php->read();
  83. $result = $this->Php->isStarted();
  84. $this->assertTrue($result);
  85. $this->_destroySession(session_name());
  86. $result = $this->Php->isStarted();
  87. $this->assertFalse($result);
  88. }
  89. public function testIsStartedNoInit() {
  90. $this->_destroySession(session_name());
  91. $Php = new Php(array('init' => false));
  92. $result = $Php->isStarted();
  93. $this->assertFalse($result);
  94. $Php = new Php();
  95. $Php->read();
  96. $result = $Php->isStarted();
  97. $this->assertTrue($result);
  98. }
  99. public function testKey() {
  100. $result = $this->Php->key();
  101. $this->assertEqual(session_id(), $result);
  102. $this->_destroySession(session_name());
  103. $result = $this->Php->key();
  104. $this->assertNull($result);
  105. }
  106. public function testWrite() {
  107. $key = 'write-test';
  108. $value = 'value to be written';
  109. $closure = $this->Php->write($key, $value);
  110. $this->assertTrue(is_callable($closure));
  111. $params = compact('key', 'value');
  112. $result = $closure($this->Php, $params, null);
  113. $this->assertEqual($_SESSION[$key], $value);
  114. }
  115. public function testRead() {
  116. $this->Php->read();
  117. $key = 'read_test';
  118. $value = 'value to be read';
  119. $_SESSION[$key] = $value;
  120. $closure = $this->Php->read($key);
  121. $this->assertTrue(is_callable($closure));
  122. $params = compact('key');
  123. $result = $closure($this->Php, $params, null);
  124. $this->assertIdentical($value, $result);
  125. $key = 'non-existent';
  126. $closure = $this->Php->read($key);
  127. $this->assertTrue(is_callable($closure));
  128. $params = compact('key');
  129. $result = $closure($this->Php, $params, null);
  130. $this->assertNull($result);
  131. $closure = $this->Php->read();
  132. $this->assertTrue(is_callable($closure));
  133. $result = $closure($this->Php, array('key' => null), null);
  134. $expected = array('read_test' => 'value to be read');
  135. $this->assertEqual($expected, $result);
  136. }
  137. public function testCheck() {
  138. $this->Php->read();
  139. $key = 'read';
  140. $value = 'value to be read';
  141. $_SESSION[$key] = $value;
  142. $closure = $this->Php->check($key);
  143. $this->assertTrue(is_callable($closure));
  144. $params = compact('key');
  145. $result = $closure($this->Php, $params, null);
  146. $this->assertTrue($result);
  147. $key = 'does_not_exist';
  148. $closure = $this->Php->check($key);
  149. $this->assertTrue(is_callable($closure));
  150. $params = compact('key');
  151. $result = $closure($this->Php, $params, null);
  152. $this->assertFalse($result);
  153. }
  154. public function testDelete() {
  155. $this->Php->read();
  156. $key = 'delete_test';
  157. $value = 'value to be deleted';
  158. $_SESSION[$key] = $value;
  159. $closure = $this->Php->delete($key);
  160. $this->assertTrue(is_callable($closure));
  161. $params = compact('key');
  162. $result = $closure($this->Php, $params, null);
  163. $this->assertTrue($result);
  164. $key = 'non-existent';
  165. $closure = $this->Php->delete($key);
  166. $this->assertTrue(is_callable($closure));
  167. $params = compact('key');
  168. $result = $closure($this->Php, $params, null);
  169. $this->assertTrue($result);
  170. }
  171. public function testCheckThrowException() {
  172. $Php = new MockPhp(array('init' => false));
  173. $this->expectException('/Could not start session./');
  174. $Php->check('whatever');
  175. }
  176. public function testReadThrowException() {
  177. $Php = new MockPhp(array('init' => false));
  178. $this->expectException('/Could not start session./');
  179. $Php->read('whatever');
  180. }
  181. public function testWriteThrowException() {
  182. $Php = new MockPhp(array('init' => false));
  183. $this->expectException('/Could not start session./');
  184. $Php->write('whatever', 'value');
  185. }
  186. public function testDeleteThrowException() {
  187. $Php = new MockPhp(array('init' => false));
  188. $this->expectException('/Could not start session./');
  189. $Php->delete('whatever');
  190. }
  191. public function testReadDotSyntax() {
  192. $this->Php->read();
  193. $key = 'dot';
  194. $value = array('syntax' => array('key' => 'value'));
  195. $_SESSION[$key] = $value;
  196. $closure = $this->Php->read($key);
  197. $this->assertTrue(is_callable($closure));
  198. $params = compact('key');
  199. $result = $closure($this->Php, $params, null);
  200. $this->assertIdentical($value, $result);
  201. $params = array('key' => 'dot.syntax');
  202. $result = $closure($this->Php, $params, null);
  203. $this->assertIdentical($value['syntax'], $result);
  204. }
  205. public function testWriteDotSyntax() {
  206. $key = 'dot.syntax';
  207. $value = 'value to be written';
  208. $closure = $this->Php->write($key, $value);
  209. $this->assertTrue(is_callable($closure));
  210. $params = compact('key', 'value');
  211. $result = $closure($this->Php, $params, null);
  212. $this->assertEqual($_SESSION['dot']['syntax'], $value);
  213. }
  214. }
  215. ?>