/lithium/libraries/lithium/tests/cases/storage/cache/adapter/XCacheTest.php

https://github.com/brtriver/sukonv · PHP · 348 lines · 244 code · 88 blank · 16 comment · 3 complexity · 35f96c8f5b51a2a35d11d592d6cabd7b MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\storage\cache\adapter;
  9. use lithium\storage\cache\adapter\XCache;
  10. class XCacheTest extends \lithium\test\Unit {
  11. /**
  12. * Skip the test if XCache extension is unavailable.
  13. *
  14. * @return void
  15. */
  16. public function skip() {
  17. $extensionExists = (extension_loaded('xcache') && (ini_get('xcache.var_size') !== 0));
  18. $message = 'The XCache extension is not installed or not configured for userspace caching.';
  19. $this->skipIf(!$extensionExists, $message);
  20. }
  21. /**
  22. * Clear the userspace cache
  23. *
  24. * @return void
  25. */
  26. public function setUp() {
  27. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
  28. if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
  29. return false;
  30. }
  31. }
  32. $this->XCache = new XCache();
  33. }
  34. public function tearDown() {
  35. unset($this->XCache);
  36. }
  37. public function testEnabled() {
  38. $xcache = $this->XCache;
  39. $this->assertTrue($xcache::enabled());
  40. }
  41. public function testSimpleWrite() {
  42. $key = 'key';
  43. $data = 'value';
  44. $expiry = '+5 seconds';
  45. $time = strtotime($expiry);
  46. $closure = $this->XCache->write($key, $data, $expiry);
  47. $this->assertTrue(is_callable($closure));
  48. $params = compact('key', 'data', 'expiry');
  49. $result = $closure($this->XCache, $params, null);
  50. $expected = $data;
  51. $this->assertEqual($expected, $result);
  52. $result = xcache_get($key);
  53. $this->assertEqual($expected, $result);
  54. $result = xcache_unset($key);
  55. $this->assertTrue($result);
  56. $key = 'another_key';
  57. $data = 'more_data';
  58. $expiry = '+1 minute';
  59. $time = strtotime($expiry);
  60. $closure = $this->XCache->write($key, $data, $expiry);
  61. $this->assertTrue(is_callable($closure));
  62. $params = compact('key', 'data', 'expiry');
  63. $result = $closure($this->XCache, $params, null);
  64. $expected = $data;
  65. $this->assertEqual($expected, $result);
  66. $result = xcache_get($key);
  67. $this->assertEqual($expected, $result);
  68. $result = xcache_unset($key);
  69. $this->assertTrue($result);
  70. }
  71. public function testWriteDefaultCacheExpiry() {
  72. $XCache = new XCache(array('expiry' => '+5 seconds'));
  73. $key = 'default_key';
  74. $data = 'value';
  75. $time = strtotime('+5 seconds');
  76. $closure = $XCache->write($key, $data);
  77. $this->assertTrue(is_callable($closure));
  78. $params = compact('key', 'data');
  79. $result = $closure($XCache, $params, null);
  80. $expected = $data;
  81. $this->assertEqual($expected, $result);
  82. $result = xcache_get($key);
  83. $this->assertEqual($expected, $result);
  84. $result = xcache_unset($key);
  85. $this->assertTrue($result);
  86. }
  87. public function testSimpleRead() {
  88. $key = 'read_key';
  89. $data = 'read data';
  90. $time = strtotime('+1 minute');
  91. $result = xcache_set($key, $data, 60);
  92. $this->assertTrue($result);
  93. $closure = $this->XCache->read($key);
  94. $this->assertTrue(is_callable($closure));
  95. $params = compact('key');
  96. $result = $closure($this->XCache, $params, null);
  97. $expected = $data;
  98. $this->assertEqual($expected, $result);
  99. $result = xcache_unset($key);
  100. $this->assertTrue($result);
  101. $key = 'another_read_key';
  102. $data = 'read data';
  103. $time = strtotime('+1 minute');
  104. $result = xcache_set($key, $data, 60);
  105. $this->assertTrue($result);
  106. $closure = $this->XCache->read($key);
  107. $this->assertTrue(is_callable($closure));
  108. $params = compact('key');
  109. $result = $closure($this->XCache, $params, null);
  110. $expected = $data;
  111. $this->assertEqual($expected, $result);
  112. $result = xcache_unset($key);
  113. $this->assertTrue($result);
  114. }
  115. public function testReadKeyThatDoesNotExist() {
  116. $key = 'does_not_exist';
  117. $closure = $this->XCache->read($key);
  118. $this->assertTrue(is_callable($closure));
  119. $params = compact('key');
  120. $result = $closure($this->XCache, $params, null);
  121. $this->assertFalse($result);
  122. }
  123. public function testDelete() {
  124. $key = 'delete_key';
  125. $data = 'data to delete';
  126. $time = strtotime('+1 minute');
  127. $result = xcache_set($key, $data, 60);
  128. $this->assertTrue($result);
  129. $closure = $this->XCache->delete($key);
  130. $this->assertTrue(is_callable($closure));
  131. $params = compact('key');
  132. $result = $closure($this->XCache, $params, null);
  133. $this->assertTrue($result);
  134. }
  135. public function testDeleteNonExistentKey() {
  136. $key = 'delete_key';
  137. $data = 'data to delete';
  138. $time = strtotime('+1 minute');
  139. $closure = $this->XCache->delete($key);
  140. $this->assertTrue(is_callable($closure));
  141. $params = compact('key');
  142. $result = $closure($this->XCache, $params, null);
  143. $this->assertFalse($result);
  144. }
  145. public function testWriteReadAndDeleteRoundtrip() {
  146. $key = 'write_read_key';
  147. $data = 'write/read value';
  148. $expiry = '+5 seconds';
  149. $time = strtotime($expiry);
  150. $closure = $this->XCache->write($key, $data, $expiry);
  151. $this->assertTrue(is_callable($closure));
  152. $params = compact('key', 'data', 'expiry');
  153. $result = $closure($this->XCache, $params, null);
  154. $expected = $data;
  155. $this->assertEqual($expected, $result);
  156. $result = xcache_get($key);
  157. $this->assertEqual($expected, $result);
  158. $closure = $this->XCache->read($key);
  159. $this->assertTrue(is_callable($closure));
  160. $params = compact('key');
  161. $result = $closure($this->XCache, $params, null);
  162. $expected = $data;
  163. $this->assertEqual($expected, $result);
  164. $closure = $this->XCache->delete($key);
  165. $this->assertTrue(is_callable($closure));
  166. $params = compact('key');
  167. $result = $closure($this->XCache, $params, null);
  168. $this->assertTrue($result);
  169. }
  170. public function testClear() {
  171. $admin = (ini_get('xcache.admin.enable_auth') === "On");
  172. $this->skipIf($admin, "XCache::clear() test skipped due to authentication.");
  173. $key1 = 'key_clear_1';
  174. $key2 = 'key_clear_2';
  175. $time = strtotime('+1 minute');
  176. $result = xcache_set($key1, 'data that will no longer exist', $time);
  177. $this->assertTrue($result);
  178. $result = xcache_set($key2, 'more dead data', $time);
  179. $this->assertTrue($result);
  180. $result = $this->XCache->clear();
  181. $this->assertTrue($result);
  182. $this->assertFalse(xcache_get($key1));
  183. $this->assertFalse(xcache_get($key2));
  184. }
  185. public function testDecrement() {
  186. $time = strtotime('+1 minute');
  187. $key = 'decrement';
  188. $value = 10;
  189. $result = xcache_set($key, $value, $time);
  190. $this->assertTrue($result);
  191. $closure = $this->XCache->decrement($key);
  192. $this->assertTrue(is_callable($closure));
  193. $params = compact('key');
  194. $result = $closure($this->XCache, $params, null);
  195. $this->assertEqual($value - 1, $result);
  196. $result = xcache_get($key);
  197. $this->assertEqual($value - 1, $result);
  198. $result = xcache_unset($key);
  199. $this->assertTrue($result);
  200. }
  201. public function testDecrementNonIntegerValue() {
  202. $time = strtotime('+1 minute');
  203. $key = 'non_integer';
  204. $value = 'no';
  205. $result = xcache_set($key, $value, $time);
  206. $this->assertTrue($result);
  207. $closure = $this->XCache->decrement($key);
  208. $this->assertTrue(is_callable($closure));
  209. $params = compact('key');
  210. $result = $closure($this->XCache, $params, null);
  211. $result = xcache_get($key);
  212. $this->assertEqual(-1, $result);
  213. $closure = $this->XCache->decrement($key);
  214. $this->assertTrue(is_callable($closure));
  215. $params = compact('key');
  216. $result = $closure($this->XCache, $params, null);
  217. $result = xcache_get($key);
  218. $this->assertEqual(-2, $result);
  219. $result = xcache_unset($key);
  220. $this->assertTrue($result);
  221. }
  222. public function testIncrement() {
  223. $time = strtotime('+1 minute');
  224. $key = 'increment';
  225. $value = 10;
  226. $result = xcache_set($key, $value, $time);
  227. $this->assertTrue($result);
  228. $closure = $this->XCache->increment($key);
  229. $this->assertTrue(is_callable($closure));
  230. $params = compact('key');
  231. $result = $closure($this->XCache, $params, null);
  232. $this->assertEqual($value + 1, $result);
  233. $result = xcache_get($key);
  234. $this->assertEqual($value + 1, $result);
  235. $result = xcache_unset($key);
  236. $this->assertTrue($result);
  237. }
  238. public function testIncrementNonIntegerValue() {
  239. $time = strtotime('+1 minute');
  240. $key = 'non_integer_increment';
  241. $value = 'yes';
  242. $result = xcache_set($key, $value, $time);
  243. $this->assertTrue($result);
  244. $closure = $this->XCache->increment($key);
  245. $this->assertTrue(is_callable($closure));
  246. $params = compact('key');
  247. $result = $closure($this->XCache, $params, null);
  248. $result = xcache_get($key);
  249. $this->assertEqual(1, $result);
  250. $closure = $this->XCache->increment($key);
  251. $this->assertTrue(is_callable($closure));
  252. $params = compact('key');
  253. $result = $closure($this->XCache, $params, null);
  254. $result = xcache_get($key);
  255. $this->assertEqual(2, $result);
  256. $result = xcache_unset($key);
  257. $this->assertTrue($result);
  258. }
  259. }
  260. ?>