PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Session/StorageTest.php

https://bitbucket.org/saifshuvo/zf2
PHP | 256 lines | 206 code | 33 blank | 17 comment | 1 complexity | 89a7e0e04784b4621ba2f9381e7c32b7 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Session
  9. */
  10. namespace ZendTest\Session;
  11. use Zend\Session\Storage\ArrayStorage;
  12. /**
  13. * @category Zend
  14. * @package Zend_Session
  15. * @subpackage UnitTests
  16. * @group Zend_Session
  17. */
  18. class StorageTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var ArrayStorage
  22. */
  23. protected $storage;
  24. public function setUp()
  25. {
  26. $this->storage = new ArrayStorage;
  27. }
  28. public function testStorageAllowsArrayAccess()
  29. {
  30. $this->storage['foo'] = 'bar';
  31. $this->assertTrue(isset($this->storage['foo']));
  32. $this->assertEquals('bar', $this->storage['foo']);
  33. unset($this->storage['foo']);
  34. $this->assertFalse(isset($this->storage['foo']));
  35. }
  36. public function testStorageAllowsPropertyAccess()
  37. {
  38. $this->storage->foo = 'bar';
  39. $this->assertTrue(isset($this->storage->foo));
  40. $this->assertEquals('bar', $this->storage->foo);
  41. unset($this->storage->foo);
  42. $this->assertFalse(isset($this->storage->foo));
  43. }
  44. public function testStorageAllowsSettingMetadata()
  45. {
  46. $this->storage->setMetadata('TEST', 'foo');
  47. $this->assertEquals('foo', $this->storage->getMetadata('TEST'));
  48. }
  49. public function testSettingArrayMetadataMergesOnSubsequentRequests()
  50. {
  51. $this->storage->setMetadata('TEST', array('foo' => 'bar', 'bar' => 'baz'));
  52. $this->storage->setMetadata('TEST', array('foo' => 'baz', 'baz' => 'bat', 'lonesome'));
  53. $expected = array('foo' => 'baz', 'bar' => 'baz', 'baz' => 'bat', 'lonesome');
  54. $this->assertEquals($expected, $this->storage->getMetadata('TEST'));
  55. }
  56. public function testSettingArrayMetadataWithOverwriteFlagOverwritesExistingData()
  57. {
  58. $this->storage->setMetadata('TEST', array('foo' => 'bar', 'bar' => 'baz'));
  59. $this->storage->setMetadata('TEST', array('foo' => 'baz', 'baz' => 'bat', 'lonesome'), true);
  60. $expected = array('foo' => 'baz', 'baz' => 'bat', 'lonesome');
  61. $this->assertEquals($expected, $this->storage->getMetadata('TEST'));
  62. }
  63. public function testLockWithNoKeyMakesStorageReadOnly()
  64. {
  65. $this->storage->foo = 'bar';
  66. $this->storage->lock();
  67. $this->setExpectedException('Zend\Session\Exception\RuntimeException', 'Cannot set key "foo" due to locking');
  68. $this->storage->foo = 'baz';
  69. }
  70. public function testLockWithNoKeyMarksEntireStorageLocked()
  71. {
  72. $this->storage->foo = 'bar';
  73. $this->storage->bar = 'baz';
  74. $this->storage->lock();
  75. $this->assertTrue($this->storage->isLocked());
  76. $this->assertTrue($this->storage->isLocked('foo'));
  77. $this->assertTrue($this->storage->isLocked('bar'));
  78. }
  79. public function testLockWithKeyMakesOnlyThatKeyReadOnly()
  80. {
  81. $this->storage->foo = 'bar';
  82. $this->storage->lock('foo');
  83. $this->storage->bar = 'baz';
  84. $this->assertEquals('baz', $this->storage->bar);
  85. $this->setExpectedException('Zend\Session\Exception\RuntimeException', 'Cannot set key "foo" due to locking');
  86. $this->storage->foo = 'baz';
  87. }
  88. public function testLockWithKeyMarksOnlyThatKeyLocked()
  89. {
  90. $this->storage->foo = 'bar';
  91. $this->storage->bar = 'baz';
  92. $this->storage->lock('foo');
  93. $this->assertTrue($this->storage->isLocked('foo'));
  94. $this->assertFalse($this->storage->isLocked('bar'));
  95. }
  96. public function testLockWithNoKeyShouldWriteToMetadata()
  97. {
  98. $this->storage->foo = 'bar';
  99. $this->storage->lock();
  100. $locked = $this->storage->getMetadata('_READONLY');
  101. $this->assertTrue($locked);
  102. }
  103. public function testLockWithKeyShouldWriteToMetadata()
  104. {
  105. $this->storage->foo = 'bar';
  106. $this->storage->lock('foo');
  107. $locks = $this->storage->getMetadata('_LOCKS');
  108. $this->assertTrue(is_array($locks));
  109. $this->assertTrue(array_key_exists('foo', $locks));
  110. }
  111. public function testUnlockShouldUnlockEntireObject()
  112. {
  113. $this->storage->foo = 'bar';
  114. $this->storage->bar = 'baz';
  115. $this->storage->lock();
  116. $this->storage->unlock();
  117. $this->assertFalse($this->storage->isLocked('foo'));
  118. $this->assertFalse($this->storage->isLocked('bar'));
  119. }
  120. public function testUnlockShouldUnlockSelectivelyLockedKeys()
  121. {
  122. $this->storage->foo = 'bar';
  123. $this->storage->bar = 'baz';
  124. $this->storage->lock('foo');
  125. $this->storage->unlock();
  126. $this->assertFalse($this->storage->isLocked('foo'));
  127. $this->assertFalse($this->storage->isLocked('bar'));
  128. }
  129. public function testUnlockWithKeyShouldUnlockOnlyThatKey()
  130. {
  131. $this->storage->foo = 'bar';
  132. $this->storage->bar = 'baz';
  133. $this->storage->lock();
  134. $this->storage->unlock('foo');
  135. $this->assertFalse($this->storage->isLocked('foo'));
  136. $this->assertTrue($this->storage->isLocked('bar'));
  137. }
  138. public function testUnlockWithKeyOfSelectiveLockShouldUnlockThatKey()
  139. {
  140. $this->storage->foo = 'bar';
  141. $this->storage->lock('foo');
  142. $this->storage->unlock('foo');
  143. $this->assertFalse($this->storage->isLocked('foo'));
  144. }
  145. public function testClearWithNoArgumentsRemovesExistingData()
  146. {
  147. $this->storage->foo = 'bar';
  148. $this->storage->bar = 'baz';
  149. $this->storage->clear();
  150. $data = $this->storage->toArray();
  151. $this->assertSame(array(), $data);
  152. }
  153. public function testClearWithNoArgumentsRemovesExistingMetadata()
  154. {
  155. $this->storage->foo = 'bar';
  156. $this->storage->lock('foo');
  157. $this->storage->setMetadata('FOO', 'bar');
  158. $this->storage->clear();
  159. $this->assertFalse($this->storage->isLocked('foo'));
  160. $this->assertFalse($this->storage->getMetadata('FOO'));
  161. }
  162. public function testClearWithArgumentRemovesExistingDataForThatKeyOnly()
  163. {
  164. $this->storage->foo = 'bar';
  165. $this->storage->bar = 'baz';
  166. $this->storage->clear('foo');
  167. $data = $this->storage->toArray();
  168. $this->assertSame(array('bar' => 'baz'), $data);
  169. }
  170. public function testClearWithArgumentRemovesExistingMetadataForThatKeyOnly()
  171. {
  172. $this->storage->foo = 'bar';
  173. $this->storage->bar = 'baz';
  174. $this->storage->lock('foo');
  175. $this->storage->lock('bar');
  176. $this->storage->setMetadata('foo', 'bar');
  177. $this->storage->setMetadata('bar', 'baz');
  178. $this->storage->clear('foo');
  179. $this->assertFalse($this->storage->isLocked('foo'));
  180. $this->assertTrue($this->storage->isLocked('bar'));
  181. $this->assertFalse($this->storage->getMetadata('foo'));
  182. $this->assertEquals('baz', $this->storage->getMetadata('bar'));
  183. }
  184. public function testClearWhenStorageMarkedImmutableRaisesException()
  185. {
  186. $this->storage->foo = 'bar';
  187. $this->storage->bar = 'baz';
  188. $this->storage->markImmutable();
  189. $this->setExpectedException('Zend\Session\Exception\RuntimeException',
  190. 'Cannot clear storage as it is marked immutable');
  191. $this->storage->clear();
  192. }
  193. public function testRequestAccessTimeIsPreservedEvenInFactoryMethod()
  194. {
  195. $this->assertNotEmpty($this->storage->getRequestAccessTime());
  196. $this->storage->fromArray(array());
  197. $this->assertNotEmpty($this->storage->getRequestAccessTime());
  198. }
  199. public function testToArrayWithMetaData()
  200. {
  201. $this->storage->foo = 'bar';
  202. $this->storage->bar = 'baz';
  203. $this->storage->setMetadata('foo', 'bar');
  204. $expected = array(
  205. '__ZF' => array(
  206. '_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
  207. 'foo' => 'bar',
  208. ),
  209. 'foo' => 'bar',
  210. 'bar' => 'baz',
  211. );
  212. $this->assertSame($expected, $this->storage->toArray(true));
  213. }
  214. public function testUnsetMultidimensional()
  215. {
  216. if (version_compare(PHP_VERSION, '5.3.4') < 0) {
  217. $this->markTestSkipped('Known issue on versions of PHP less than 5.3.4');
  218. }
  219. $this->storage['foo'] = array('bar' => array('baz' => 'boo'));
  220. unset($this->storage['foo']['bar']['baz']);
  221. unset($this->storage['foo']['bar']);
  222. }
  223. }