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

https://github.com/brtriver/sukonv · PHP · 378 lines · 273 code · 94 blank · 11 comment · 0 complexity · d08e3a4f3df027990aeacbccb93da155 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\Redis;
  10. class RedisTest extends \lithium\test\Unit {
  11. /**
  12. * Skip the test if the Redis extension is unavailable.
  13. *
  14. * @return void
  15. */
  16. public function skip() {
  17. $extensionExists = extension_loaded('redis');
  18. $message = 'The redis extension is not installed.';
  19. $this->skipIf(!$extensionExists, $message);
  20. $R = new \Redis();
  21. $result = null;
  22. try {
  23. $R->connect('127.0.0.1', 6379);
  24. } catch (\Exception $e) {
  25. $message = 'redis-server does not appear to be running on 127.0.0.1:6379';
  26. $result = $R->info();
  27. $this->skipIf(empty($result), $message);
  28. }
  29. unset($R);
  30. }
  31. public function setUp() {
  32. $this->server = array('host' => '127.0.0.1', 'port' => 6379);
  33. $this->_Redis = new \Redis();
  34. $this->_Redis->connect($this->server['host'], $this->server['port']);
  35. $this->Redis = new Redis();
  36. }
  37. public function tearDown() {
  38. $this->_Redis->flushdb();
  39. }
  40. public function testEnabled() {
  41. $redis = $this->Redis;
  42. $this->assertTrue($redis::enabled());
  43. }
  44. public function testInit() {
  45. $Redis = new Redis();
  46. $this->assertTrue($Redis::$connection instanceof \Redis);
  47. }
  48. public function testSimpleWrite() {
  49. $key = 'key';
  50. $data = 'value';
  51. $expiry = '+5 seconds';
  52. $time = strtotime($expiry);
  53. $closure = $this->Redis->write($key, $data, $expiry);
  54. $this->assertTrue(is_callable($closure));
  55. $params = compact('key', 'data', 'expiry');
  56. $result = $closure($this->Redis, $params, null);
  57. $expected = $data;
  58. $this->assertEqual($expected, $result);
  59. $result = $this->_Redis->get($key);
  60. $this->assertEqual($expected, $result);
  61. $result = $this->_Redis->ttl($key);
  62. $this->assertEqual($time - time(), $result);
  63. $result = $this->_Redis->delete($key);
  64. $this->assertTrue($result);
  65. $key = 'another_key';
  66. $data = 'more_data';
  67. $expiry = '+1 minute';
  68. $time = strtotime($expiry);
  69. $closure = $this->Redis->write($key, $data, $expiry);
  70. $this->assertTrue(is_callable($closure));
  71. $params = compact('key', 'data', 'expiry');
  72. $result = $closure($this->Redis, $params, null);
  73. $expected = $data;
  74. $this->assertEqual($expected, $result);
  75. $result = $this->_Redis->get($key);
  76. $this->assertEqual($expected, $result);
  77. $result = $this->_Redis->ttl($key);
  78. $this->assertEqual($time - time(), $result);
  79. $result = $this->_Redis->delete($key);
  80. $this->assertTrue($result);
  81. }
  82. public function testWriteDefaultCacheExpiry() {
  83. $Redis = new Redis(array('expiry' => '+5 seconds'));
  84. $key = 'default_key';
  85. $data = 'value';
  86. $time = strtotime('+5 seconds');
  87. $closure = $Redis->write($key, $data);
  88. $this->assertTrue(is_callable($closure));
  89. $params = compact('key', 'data');
  90. $result = $closure($Redis, $params, null);
  91. $expected = $data;
  92. $this->assertEqual($expected, $result);
  93. $result = $this->_Redis->get($key);
  94. $this->assertEqual($expected, $result);
  95. $result = $this->_Redis->ttl($key);
  96. $this->assertEqual($time - time(), $result);
  97. $result = $this->_Redis->delete($key);
  98. $this->assertTrue($result);
  99. }
  100. public function testSimpleRead() {
  101. $key = 'read_key';
  102. $data = 'read data';
  103. $result = $this->_Redis->set($key, $data);
  104. $this->assertTrue($result);
  105. $closure = $this->Redis->read($key);
  106. $this->assertTrue(is_callable($closure));
  107. $params = compact('key');
  108. $result = $closure($this->Redis, $params, null);
  109. $expected = $data;
  110. $this->assertEqual($expected, $result);
  111. $result = $this->_Redis->delete($key);
  112. $this->assertTrue($result);
  113. $key = 'another_read_key';
  114. $data = 'read data';
  115. $time = strtotime('+1 minute');
  116. $result = $this->_Redis->set($key, $data);
  117. $this->assertTrue($result);
  118. $result = $this->_Redis->ttl($key);
  119. $this->assertTrue($result);
  120. $closure = $this->Redis->read($key);
  121. $this->assertTrue(is_callable($closure));
  122. $params = compact('key');
  123. $result = $closure($this->Redis, $params, null);
  124. $expected = $data;
  125. $this->assertEqual($expected, $result);
  126. $result = $this->_Redis->delete($key);
  127. $this->assertTrue($result);
  128. }
  129. public function testMultiRead() {
  130. $data = array('key1' => 'value1', 'key2' => 'value2');
  131. $result = $this->_Redis->mset($data);
  132. $this->assertTrue($result);
  133. $closure = $this->Redis->read(array_keys($data));
  134. $this->assertTrue(is_callable($closure));
  135. $params = array('key' => array_keys($data));
  136. $result = $closure($this->Redis, $params, null);
  137. $expected = array_values($data);
  138. $this->assertEqual($expected, $result);
  139. foreach ($data as $k => $v) {
  140. $result = $this->_Redis->delete($k);
  141. $this->assertTrue($result);
  142. }
  143. }
  144. public function testMultiWrite() {
  145. $key = array('key1' => 'value1', 'key2' => 'value2');
  146. $expiry = '+5 seconds';
  147. $time = strtotime($expiry);
  148. $closure = $this->Redis->write($key, $expiry);
  149. $this->assertTrue(is_callable($closure));
  150. $params = array('key' => $key, 'data' => $expiry, 'expiry' => null);
  151. $result = $closure($this->Redis, $params, null);
  152. $expected = array('key1' => true, 'key2' => true);
  153. $this->assertEqual($expected, $result);
  154. $result = $this->_Redis->getMultiple(array_keys($key));
  155. $expected = array_values($key);
  156. $this->assertEqual($expected, $result);
  157. }
  158. public function testReadKeyThatDoesNotExist() {
  159. $key = 'does_not_exist';
  160. $closure = $this->Redis->read($key);
  161. $this->assertTrue(is_callable($closure));
  162. $params = compact('key');
  163. $result = $closure($this->Redis, $params, null);
  164. $this->assertFalse($result);
  165. }
  166. public function testDelete() {
  167. $key = 'delete_key';
  168. $data = 'data to delete';
  169. $time = strtotime('+1 minute');
  170. $result = $this->_Redis->set($key, $data);
  171. $this->assertTrue($result);
  172. $closure = $this->Redis->delete($key);
  173. $this->assertTrue(is_callable($closure));
  174. $params = compact('key');
  175. $result = $closure($this->Redis, $params, null);
  176. $this->assertTrue($result);
  177. $this->assertFalse($this->_Redis->delete($key));
  178. }
  179. public function testDeleteNonExistentKey() {
  180. $key = 'delete_key';
  181. $closure = $this->Redis->delete($key);
  182. $this->assertTrue(is_callable($closure));
  183. $params = compact('key');
  184. $result = $closure($this->Redis, $params, null);
  185. $this->assertFalse($result);
  186. }
  187. public function testWriteReadAndDeleteRoundtrip() {
  188. $key = 'write_read_key';
  189. $data = 'write/read value';
  190. $expiry = '+5 seconds';
  191. $time = strtotime($expiry);
  192. $closure = $this->Redis->write($key, $data, $expiry);
  193. $this->assertTrue(is_callable($closure));
  194. $params = compact('key', 'data', 'expiry');
  195. $result = $closure($this->Redis, $params, null);
  196. $expected = $data;
  197. $this->assertEqual($expected, $result);
  198. $result = $this->_Redis->get($key);
  199. $this->assertEqual($expected, $result);
  200. $closure = $this->Redis->read($key);
  201. $this->assertTrue(is_callable($closure));
  202. $params = compact('key');
  203. $result = $closure($this->Redis, $params, null);
  204. $expected = $data;
  205. $this->assertEqual($expected, $result);
  206. $closure = $this->Redis->delete($key);
  207. $this->assertTrue(is_callable($closure));
  208. $params = compact('key');
  209. $result = $closure($this->Redis, $params, null);
  210. $this->assertTrue($result);
  211. $this->assertFalse($this->_Redis->get($key));
  212. }
  213. public function testClear() {
  214. $result = $this->_Redis->set('key', 'value');
  215. $this->assertTrue($result);
  216. $result = $this->_Redis->set('another_key', 'value');
  217. $this->assertTrue($result);
  218. $result = $this->Redis->clear();
  219. $this->assertTrue($result);
  220. $this->assertFalse($this->_Redis->get('key'));
  221. $this->assertFalse($this->_Redis->get('another_key'));
  222. }
  223. public function testDecrement() {
  224. $key = 'decrement';
  225. $value = 10;
  226. $result = $this->_Redis->set($key, $value);
  227. $this->assertTrue($result);
  228. $closure = $this->Redis->decrement($key);
  229. $this->assertTrue(is_callable($closure));
  230. $params = compact('key');
  231. $result = $closure($this->Redis, $params, null);
  232. $this->assertEqual($value - 1, $result);
  233. $result = $this->_Redis->get($key);
  234. $this->assertEqual($value - 1, $result);
  235. $result = $this->_Redis->delete($key);
  236. $this->assertTrue($result);
  237. }
  238. public function testDecrementNonIntegerValue() {
  239. $key = 'non_integer';
  240. $value = 'no';
  241. $result = $this->_Redis->set($key, $value);
  242. $this->assertTrue($result);
  243. $closure = $this->Redis->decrement($key);
  244. $this->assertTrue(is_callable($closure));
  245. $params = compact('key');
  246. $result = $closure($this->Redis, $params, null);
  247. $this->assertFalse($result);
  248. $result = $this->_Redis->get($key);
  249. $this->assertEqual($value, $result);
  250. $result = $this->_Redis->delete($key);
  251. $this->assertTrue($result);
  252. }
  253. public function testIncrement() {
  254. $key = 'increment';
  255. $value = 10;
  256. $result = $this->_Redis->set($key, $value);
  257. $this->assertTrue($result);
  258. $closure = $this->Redis->increment($key);
  259. $this->assertTrue(is_callable($closure));
  260. $params = compact('key');
  261. $result = $closure($this->Redis, $params, null);
  262. $this->assertEqual($value + 1, $result);
  263. $result = $this->_Redis->get($key);
  264. $this->assertEqual($value + 1, $result);
  265. $result = $this->_Redis->delete($key);
  266. $this->assertTrue($result);
  267. }
  268. public function testIncrementNonIntegerValue() {
  269. $key = 'non_integer_increment';
  270. $value = 'yes';
  271. $result = $this->_Redis->set($key, $value);
  272. $this->assertTrue($result);
  273. $closure = $this->Redis->increment($key);
  274. $this->assertTrue(is_callable($closure));
  275. $params = compact('key');
  276. $result = $closure($this->Redis, $params, null);
  277. $this->assertFalse($result);
  278. $result = $this->_Redis->get($key);
  279. $this->assertEqual($value, $result);
  280. $result = $this->_Redis->delete($key);
  281. $this->assertTrue($result);
  282. }
  283. }
  284. ?>