PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/UnionOfRAD/lithium
PHP | 392 lines | 290 code | 91 blank | 11 comment | 0 complexity | 55e4634cf5f37564fbcff0e392b40e79 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 Memcached;
  10. use lithium\storage\cache\adapter\Memcache;
  11. class MemcacheTest extends \lithium\test\Unit {
  12. protected $_conn = null;
  13. /**
  14. * Skip the test if the adapter is enabled. If it is not it means the
  15. * libmemcached extension is unavailable. Also checks for a running
  16. * Memcached server.
  17. */
  18. public function skip() {
  19. $this->skipIf(!Memcache::enabled(), 'The `Memcache` adapter is not enabled.');
  20. $conn = new Memcached();
  21. $conn->addServer('127.0.0.1', 11211);
  22. $message = 'The memcached daemon does not appear to be running on 127.0.0.1:11211';
  23. $result = $conn->getVersion();
  24. $this->skipIf(!$result, $message);
  25. unset($conn);
  26. }
  27. public function setUp() {
  28. $this->server = array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100);
  29. $this->_conn = new Memcached();
  30. $this->_conn->addServer(
  31. $this->server['host'], $this->server['port'], $this->server['weight']
  32. );
  33. $this->memcache = new Memcache();
  34. }
  35. public function tearDown() {
  36. $this->_conn->flush();
  37. }
  38. public function testEnabled() {
  39. $this->assertTrue(Memcache::enabled());
  40. }
  41. public function testSimpleWrite() {
  42. $key = 'key';
  43. $data = 'value';
  44. $expiry = '+5 seconds';
  45. $time = strtotime($expiry);
  46. $closure = $this->memcache->write($key, $data, $expiry);
  47. $this->assertEqual($data, $closure($this->memcache, compact('key', 'data', 'expiry')));
  48. $this->assertEqual($data, $this->_conn->get($key));
  49. $result = $this->_conn->delete($key);
  50. $this->assertTrue($result);
  51. $key = 'another_key';
  52. $data = 'more_data';
  53. $expiry = '+1 minute';
  54. $time = strtotime($expiry);
  55. $closure = $this->memcache->write($key, $data, $expiry);
  56. $this->assertTrue(is_callable($closure));
  57. $params = compact('key', 'data', 'expiry');
  58. $result = $closure($this->memcache, $params);
  59. $expected = $data;
  60. $this->assertEqual($expected, $result);
  61. $result = $this->_conn->get($key);
  62. $this->assertEqual($expected, $result);
  63. $result = $this->_conn->delete($key);
  64. $this->assertTrue($result);
  65. }
  66. public function testWriteDefaultCacheExpiry() {
  67. $Memcache = new Memcache(array('expiry' => '+5 seconds'));
  68. $key = 'default_key';
  69. $data = 'value';
  70. $closure = $Memcache->write($key, $data);
  71. $this->assertTrue(is_callable($closure));
  72. $params = compact('key', 'data');
  73. $result = $closure($Memcache, $params);
  74. $expected = $data;
  75. $this->assertEqual($expected, $result);
  76. $result = $this->_conn->get($key);
  77. $this->assertEqual($expected, $result);
  78. $result = $this->_conn->delete($key);
  79. $this->assertTrue($result);
  80. }
  81. public function testWriteMulti() {
  82. $expiry = '+1 minute';
  83. $time = strtotime($expiry);
  84. $key = array(
  85. 'key1' => 'data1',
  86. 'key2' => 'data2',
  87. 'key3' => 'data3'
  88. );
  89. $data = null;
  90. $closure = $this->memcache->write($key, $data, $expiry);
  91. $this->assertTrue(is_callable($closure));
  92. $params = compact('key', 'data', 'expiry');
  93. $result = $closure($this->memcache, $params);
  94. $this->assertTrue($result);
  95. $result = $this->_conn->getMulti(array_keys($key));
  96. $expected = $key;
  97. $this->assertEqual($expected, $result);
  98. foreach ($key as $name => &$value) {
  99. $result = $this->_conn->delete($name);
  100. $this->assertTrue($result);
  101. }
  102. }
  103. public function testSimpleRead() {
  104. $key = 'read_key';
  105. $data = 'read data';
  106. $time = strtotime('+1 minute');
  107. $result = $this->_conn->set($key, $data, $time);
  108. $this->assertTrue($result);
  109. $closure = $this->memcache->read($key);
  110. $this->assertTrue(is_callable($closure));
  111. $params = compact('key');
  112. $result = $closure($this->memcache, $params);
  113. $expected = $data;
  114. $this->assertEqual($expected, $result);
  115. $result = $this->_conn->delete($key);
  116. $this->assertTrue($result);
  117. $key = 'another_read_key';
  118. $data = 'read data';
  119. $time = strtotime('+1 minute');
  120. $result = $this->_conn->set($key, $data, $time);
  121. $this->assertTrue($result);
  122. $closure = $this->memcache->read($key);
  123. $this->assertTrue(is_callable($closure));
  124. $params = compact('key');
  125. $result = $closure($this->memcache, $params);
  126. $expected = $data;
  127. $this->assertEqual($expected, $result);
  128. $result = $this->_conn->delete($key);
  129. $this->assertTrue($result);
  130. }
  131. public function testReadMulti() {
  132. $expiry = '+1 minute';
  133. $time = strtotime($expiry);
  134. $key = array(
  135. 'key1' => 'data1',
  136. 'key2' => 'data2',
  137. 'key3' => 'data3'
  138. );
  139. $result = $this->_conn->setMulti($key, $time);
  140. $this->assertTrue($result);
  141. $closure = $this->memcache->read(array_keys($key));
  142. $this->assertTrue(is_callable($closure));
  143. $params = array('key' => array_keys($key));
  144. $result = $closure($this->memcache, $params);
  145. $expected = array(
  146. 'key1' => 'data1',
  147. 'key2' => 'data2',
  148. 'key3' => 'data3'
  149. );
  150. $this->assertEqual($expected, $result);
  151. foreach ($key as $name => &$value) {
  152. $result = $this->_conn->delete($name);
  153. $this->assertTrue($result);
  154. }
  155. }
  156. public function testReadKeyThatDoesNotExist() {
  157. $key = 'does_not_exist';
  158. $closure = $this->memcache->read($key);
  159. $this->assertTrue(is_callable($closure));
  160. $params = compact('key');
  161. $result = $closure($this->memcache, $params);
  162. $this->assertFalse($result);
  163. }
  164. public function testDelete() {
  165. $key = 'delete_key';
  166. $data = 'data to delete';
  167. $time = strtotime('+1 minute');
  168. $this->_conn->set($key, $data, $time);
  169. $reader = $this->memcache->read($key);
  170. $this->assertEqual($data, $reader($this->memcache, compact('key')));
  171. $delete = $this->memcache->delete($key);
  172. $this->assertTrue(is_callable($delete));
  173. $this->assertTrue($delete($this->memcache, compact('key')));
  174. $this->assertNull($reader($this->memcache, compact('key')));
  175. }
  176. public function testDeleteNonExistentKey() {
  177. $key = 'delete_key';
  178. $data = 'data to delete';
  179. $time = strtotime('+1 minute');
  180. $closure = $this->memcache->delete($key);
  181. $this->assertTrue(is_callable($closure));
  182. $params = compact('key');
  183. $result = $closure($this->memcache, $params);
  184. $this->assertFalse($result);
  185. }
  186. public function testDeprecatedConnectionSettings() {
  187. $servers = array(array('127.0.0.1', 11211, 1));
  188. $test = new Memcache(compact('servers'));
  189. $servers[0] = array_combine(array('host', 'port', 'weight'), $servers[0]);
  190. $this->assertEqual($servers, $test->connection->getServerList());
  191. }
  192. public function testSimpleConnectionSettings() {
  193. $test = new Memcache(array('host' => '127.0.0.1'));
  194. $hosts = array(array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0));
  195. $this->assertEqual($hosts, $test->connection->getServerList());
  196. $test = new Memcache(array('host' => '127.0.0.1:11222'));
  197. $hosts = array(array('host' => '127.0.0.1', 'port' => 11222, 'weight' => 0));
  198. $this->assertEqual($hosts, $test->connection->getServerList());
  199. }
  200. public function testMultiServerConnectionSettings() {
  201. $test = new Memcache(array('host' => array(
  202. '127.0.0.1:11222' => 1,
  203. '127.0.0.2:11223' => 2,
  204. '127.0.0.3:11224'
  205. )));
  206. $hosts = array(
  207. array('host' => '127.0.0.1', 'port' => 11222, 'weight' => 1),
  208. array('host' => '127.0.0.2', 'port' => 11223, 'weight' => 2),
  209. array('host' => '127.0.0.3', 'port' => 11224, 'weight' => 0)
  210. );
  211. $this->assertEqual($hosts, $test->connection->getServerList());
  212. }
  213. public function testWriteReadAndDeleteRoundtrip() {
  214. $key = 'write_read_key';
  215. $data = 'write/read value';
  216. $expiry = '+5 seconds';
  217. $time = strtotime($expiry);
  218. $writer = $this->memcache->write($key, $data, $expiry);
  219. $this->assertEqual($data, $writer($this->memcache, compact('key', 'data', 'expiry')));
  220. $this->assertEqual($data, $this->_conn->get($key));
  221. $closure = $this->memcache->read($key);
  222. $this->assertTrue(is_callable($closure));
  223. $this->assertEqual($data, $closure($this->memcache, compact('key')));
  224. $closure = $this->memcache->delete($key);
  225. $this->assertTrue(is_callable($closure));
  226. $params = compact('key');
  227. $result = $closure($this->memcache, $params);
  228. $this->assertTrue($result);
  229. $this->assertFalse($this->_conn->get($key));
  230. }
  231. public function testClear() {
  232. $time = strtotime('+1 minute');
  233. $result = $this->_conn->set('key', 'value', $time);
  234. $this->assertTrue($result);
  235. $result = $this->_conn->set('another_key', 'value', $time);
  236. $this->assertTrue($result);
  237. $result = $this->memcache->clear();
  238. $this->assertTrue($result);
  239. $this->assertFalse($this->_conn->get('key'));
  240. $this->assertFalse($this->_conn->get('another_key'));
  241. }
  242. public function testDecrement() {
  243. $time = strtotime('+1 minute');
  244. $key = 'decrement';
  245. $value = 10;
  246. $result = $this->_conn->set($key, $value, $time);
  247. $this->assertTrue($result);
  248. $closure = $this->memcache->decrement($key);
  249. $this->assertTrue(is_callable($closure));
  250. $params = compact('key');
  251. $result = $closure($this->memcache, $params);
  252. $this->assertEqual($value - 1, $result);
  253. $result = $this->_conn->get($key);
  254. $this->assertEqual($value - 1, $result);
  255. $result = $this->_conn->delete($key);
  256. $this->assertTrue($result);
  257. }
  258. public function testDecrementNonIntegerValue() {
  259. $time = strtotime('+1 minute');
  260. $key = 'non_integer';
  261. $value = 'no';
  262. $result = $this->_conn->set($key, $value, $time);
  263. $this->assertTrue($result);
  264. $closure = $this->memcache->decrement($key);
  265. $this->assertTrue(is_callable($closure));
  266. $params = compact('key');
  267. $result = $closure($this->memcache, $params);
  268. $result = $this->_conn->get($key);
  269. $this->assertEqual(0, $result);
  270. $result = $this->_conn->delete($key);
  271. $this->assertTrue($result);
  272. }
  273. public function testIncrement() {
  274. $time = strtotime('+1 minute');
  275. $key = 'increment';
  276. $value = 10;
  277. $this->assertTrue($this->_conn->set($key, $value, $time));
  278. $closure = $this->memcache->increment($key);
  279. $this->assertTrue(is_callable($closure));
  280. $result = $closure($this->memcache, compact('key'));
  281. $this->assertEqual($value + 1, $result);
  282. $this->assertEqual($value + 1, $this->_conn->get($key));
  283. $result = $this->_conn->delete($key);
  284. $this->assertTrue($result);
  285. }
  286. public function testIncrementNonIntegerValue() {
  287. $time = strtotime('+1 minute');
  288. $key = 'non_integer_increment';
  289. $value = 'yes';
  290. $result = $this->_conn->set($key, $value, $time);
  291. $this->assertTrue($result);
  292. $closure = $this->memcache->increment($key);
  293. $this->assertTrue(is_callable($closure));
  294. $result = $closure($this->memcache, compact('key'));
  295. $result = $this->_conn->get($key);
  296. $this->assertEqual(0, $result);
  297. $result = $this->_conn->delete($key);
  298. $this->assertTrue($result);
  299. }
  300. }
  301. ?>