PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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