PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php

https://gitlab.com/manuperazafa/elsartenbackend
PHP | 482 lines | 274 code | 72 blank | 136 comment | 1 complexity | a2ca33d0f35eb86cac5b792574cd5074 MD5 | raw file
  1. <?php
  2. /**
  3. * MemcacheEngineTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Cache.Engine
  15. * @since CakePHP(tm) v 1.2.0.5434
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Cache', 'Cache');
  19. App::uses('MemcacheEngine', 'Cache/Engine');
  20. /**
  21. * Class TestMemcacheEngine
  22. *
  23. * @package Cake.Test.Case.Cache.Engine
  24. */
  25. class TestMemcacheEngine extends MemcacheEngine {
  26. /**
  27. * public accessor to _parseServerString
  28. *
  29. * @param string $server
  30. * @return array
  31. */
  32. public function parseServerString($server) {
  33. return $this->_parseServerString($server);
  34. }
  35. public function setMemcache($memcache) {
  36. $this->_Memcache = $memcache;
  37. }
  38. }
  39. /**
  40. * MemcacheEngineTest class
  41. *
  42. * @package Cake.Test.Case.Cache.Engine
  43. */
  44. class MemcacheEngineTest extends CakeTestCase {
  45. /**
  46. * setUp method
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
  53. $this->_cacheDisable = Configure::read('Cache.disable');
  54. Configure::write('Cache.disable', false);
  55. Cache::config('memcache', array(
  56. 'engine' => 'Memcache',
  57. 'prefix' => 'cake_',
  58. 'duration' => 3600
  59. ));
  60. }
  61. /**
  62. * tearDown method
  63. *
  64. * @return void
  65. */
  66. public function tearDown() {
  67. parent::tearDown();
  68. Configure::write('Cache.disable', $this->_cacheDisable);
  69. Cache::drop('memcache');
  70. Cache::drop('memcache_groups');
  71. Cache::drop('memcache_helper');
  72. Cache::config('default');
  73. }
  74. /**
  75. * testSettings method
  76. *
  77. * @return void
  78. */
  79. public function testSettings() {
  80. $settings = Cache::settings('memcache');
  81. unset($settings['serialize'], $settings['path']);
  82. $expecting = array(
  83. 'prefix' => 'cake_',
  84. 'duration' => 3600,
  85. 'probability' => 100,
  86. 'servers' => array('127.0.0.1'),
  87. 'persistent' => true,
  88. 'compress' => false,
  89. 'engine' => 'Memcache',
  90. 'groups' => array()
  91. );
  92. $this->assertEquals($expecting, $settings);
  93. }
  94. /**
  95. * testSettings method
  96. *
  97. * @return void
  98. */
  99. public function testMultipleServers() {
  100. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  101. $available = true;
  102. $Memcache = new Memcache();
  103. foreach ($servers as $server) {
  104. list($host, $port) = explode(':', $server);
  105. //@codingStandardsIgnoreStart
  106. if (!@$Memcache->connect($host, $port)) {
  107. $available = false;
  108. }
  109. //@codingStandardsIgnoreEnd
  110. }
  111. $this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
  112. $Memcache = new MemcacheEngine();
  113. $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
  114. $settings = $Memcache->settings();
  115. $this->assertEquals($settings['servers'], $servers);
  116. Cache::drop('dual_server');
  117. }
  118. /**
  119. * testConnect method
  120. *
  121. * @return void
  122. */
  123. public function testConnect() {
  124. $Memcache = new MemcacheEngine();
  125. $Memcache->init(Cache::settings('memcache'));
  126. $result = $Memcache->connect('127.0.0.1');
  127. $this->assertTrue($result);
  128. }
  129. /**
  130. * test connecting to an ipv6 server.
  131. *
  132. * @return void
  133. */
  134. public function testConnectIpv6() {
  135. $Memcache = new MemcacheEngine();
  136. $result = $Memcache->init(array(
  137. 'prefix' => 'cake_',
  138. 'duration' => 200,
  139. 'engine' => 'Memcache',
  140. 'servers' => array(
  141. '[::1]:11211'
  142. )
  143. ));
  144. $this->assertTrue($result);
  145. }
  146. /**
  147. * test non latin domains.
  148. *
  149. * @return void
  150. */
  151. public function testParseServerStringNonLatin() {
  152. $Memcache = new TestMemcacheEngine();
  153. $result = $Memcache->parseServerString('schülervz.net:13211');
  154. $this->assertEquals(array('schülervz.net', '13211'), $result);
  155. $result = $Memcache->parseServerString('sülül:1111');
  156. $this->assertEquals(array('sülül', '1111'), $result);
  157. }
  158. /**
  159. * test unix sockets.
  160. *
  161. * @return void
  162. */
  163. public function testParseServerStringUnix() {
  164. $Memcache = new TestMemcacheEngine();
  165. $result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
  166. $this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
  167. }
  168. /**
  169. * testReadAndWriteCache method
  170. *
  171. * @return void
  172. */
  173. public function testReadAndWriteCache() {
  174. Cache::set(array('duration' => 1), null, 'memcache');
  175. $result = Cache::read('test', 'memcache');
  176. $expecting = '';
  177. $this->assertEquals($expecting, $result);
  178. $data = 'this is a test of the emergency broadcasting system';
  179. $result = Cache::write('test', $data, 'memcache');
  180. $this->assertTrue($result);
  181. $result = Cache::read('test', 'memcache');
  182. $expecting = $data;
  183. $this->assertEquals($expecting, $result);
  184. Cache::delete('test', 'memcache');
  185. }
  186. /**
  187. * testExpiry method
  188. *
  189. * @return void
  190. */
  191. public function testExpiry() {
  192. Cache::set(array('duration' => 1), 'memcache');
  193. $result = Cache::read('test', 'memcache');
  194. $this->assertFalse($result);
  195. $data = 'this is a test of the emergency broadcasting system';
  196. $result = Cache::write('other_test', $data, 'memcache');
  197. $this->assertTrue($result);
  198. sleep(2);
  199. $result = Cache::read('other_test', 'memcache');
  200. $this->assertFalse($result);
  201. Cache::set(array('duration' => "+1 second"), 'memcache');
  202. $data = 'this is a test of the emergency broadcasting system';
  203. $result = Cache::write('other_test', $data, 'memcache');
  204. $this->assertTrue($result);
  205. sleep(3);
  206. $result = Cache::read('other_test', 'memcache');
  207. $this->assertFalse($result);
  208. Cache::config('memcache', array('duration' => '+1 second'));
  209. $result = Cache::read('other_test', 'memcache');
  210. $this->assertFalse($result);
  211. Cache::config('memcache', array('duration' => '+29 days'));
  212. $data = 'this is a test of the emergency broadcasting system';
  213. $result = Cache::write('long_expiry_test', $data, 'memcache');
  214. $this->assertTrue($result);
  215. sleep(2);
  216. $result = Cache::read('long_expiry_test', 'memcache');
  217. $expecting = $data;
  218. $this->assertEquals($expecting, $result);
  219. Cache::config('memcache', array('duration' => 3600));
  220. }
  221. /**
  222. * testDeleteCache method
  223. *
  224. * @return void
  225. */
  226. public function testDeleteCache() {
  227. $data = 'this is a test of the emergency broadcasting system';
  228. $result = Cache::write('delete_test', $data, 'memcache');
  229. $this->assertTrue($result);
  230. $result = Cache::delete('delete_test', 'memcache');
  231. $this->assertTrue($result);
  232. }
  233. /**
  234. * testDecrement method
  235. *
  236. * @return void
  237. */
  238. public function testDecrement() {
  239. $result = Cache::write('test_decrement', 5, 'memcache');
  240. $this->assertTrue($result);
  241. $result = Cache::decrement('test_decrement', 1, 'memcache');
  242. $this->assertEquals(4, $result);
  243. $result = Cache::read('test_decrement', 'memcache');
  244. $this->assertEquals(4, $result);
  245. $result = Cache::decrement('test_decrement', 2, 'memcache');
  246. $this->assertEquals(2, $result);
  247. $result = Cache::read('test_decrement', 'memcache');
  248. $this->assertEquals(2, $result);
  249. }
  250. /**
  251. * testIncrement method
  252. *
  253. * @return void
  254. */
  255. public function testIncrement() {
  256. $result = Cache::write('test_increment', 5, 'memcache');
  257. $this->assertTrue($result);
  258. $result = Cache::increment('test_increment', 1, 'memcache');
  259. $this->assertEquals(6, $result);
  260. $result = Cache::read('test_increment', 'memcache');
  261. $this->assertEquals(6, $result);
  262. $result = Cache::increment('test_increment', 2, 'memcache');
  263. $this->assertEquals(8, $result);
  264. $result = Cache::read('test_increment', 'memcache');
  265. $this->assertEquals(8, $result);
  266. }
  267. /**
  268. * test that configurations don't conflict, when a file engine is declared after a memcache one.
  269. *
  270. * @return void
  271. */
  272. public function testConfigurationConflict() {
  273. Cache::config('long_memcache', array(
  274. 'engine' => 'Memcache',
  275. 'duration' => '+2 seconds',
  276. 'servers' => array('127.0.0.1:11211'),
  277. ));
  278. Cache::config('short_memcache', array(
  279. 'engine' => 'Memcache',
  280. 'duration' => '+1 seconds',
  281. 'servers' => array('127.0.0.1:11211'),
  282. ));
  283. Cache::config('some_file', array('engine' => 'File'));
  284. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
  285. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
  286. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  287. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
  288. sleep(1);
  289. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  290. sleep(2);
  291. $this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
  292. $this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
  293. Cache::delete('duration_test', 'long_memcache');
  294. Cache::delete('short_duration_test', 'short_memcache');
  295. }
  296. /**
  297. * test clearing memcache.
  298. *
  299. * @return void
  300. */
  301. public function testClear() {
  302. Cache::config('memcache2', array(
  303. 'engine' => 'Memcache',
  304. 'prefix' => 'cake2_',
  305. 'duration' => 3600
  306. ));
  307. Cache::write('some_value', 'cache1', 'memcache');
  308. $result = Cache::clear(true, 'memcache');
  309. $this->assertTrue($result);
  310. $this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
  311. Cache::write('some_value', 'cache2', 'memcache2');
  312. $result = Cache::clear(false, 'memcache');
  313. $this->assertTrue($result);
  314. $this->assertFalse(Cache::read('some_value', 'memcache'));
  315. $this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
  316. Cache::clear(false, 'memcache2');
  317. }
  318. /**
  319. * test that a 0 duration can successfully write.
  320. *
  321. * @return void
  322. */
  323. public function testZeroDuration() {
  324. Cache::config('memcache', array('duration' => 0));
  325. $result = Cache::write('test_key', 'written!', 'memcache');
  326. $this->assertTrue($result);
  327. $result = Cache::read('test_key', 'memcache');
  328. $this->assertEquals('written!', $result);
  329. }
  330. /**
  331. * test that durations greater than 30 days never expire
  332. *
  333. * @return void
  334. */
  335. public function testLongDurationEqualToZero() {
  336. $memcache = new TestMemcacheEngine();
  337. $memcache->settings['compress'] = false;
  338. $mock = $this->getMock('Memcache');
  339. $memcache->setMemcache($mock);
  340. $mock->expects($this->once())
  341. ->method('set')
  342. ->with('key', 'value', false, 0);
  343. $value = 'value';
  344. $memcache->write('key', $value, 50 * DAY);
  345. }
  346. /**
  347. * Tests that configuring groups for stored keys return the correct values when read/written
  348. * Shows that altering the group value is equivalent to deleting all keys under the same
  349. * group
  350. *
  351. * @return void
  352. */
  353. public function testGroupReadWrite() {
  354. Cache::config('memcache_groups', array(
  355. 'engine' => 'Memcache',
  356. 'duration' => 3600,
  357. 'groups' => array('group_a', 'group_b'),
  358. 'prefix' => 'test_'
  359. ));
  360. Cache::config('memcache_helper', array(
  361. 'engine' => 'Memcache',
  362. 'duration' => 3600,
  363. 'prefix' => 'test_'
  364. ));
  365. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  366. $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
  367. Cache::increment('group_a', 1, 'memcache_helper');
  368. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  369. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
  370. $this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
  371. Cache::increment('group_b', 1, 'memcache_helper');
  372. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  373. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
  374. $this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
  375. }
  376. /**
  377. * Tests that deleteing from a groups-enabled config is possible
  378. *
  379. * @return void
  380. */
  381. public function testGroupDelete() {
  382. Cache::config('memcache_groups', array(
  383. 'engine' => 'Memcache',
  384. 'duration' => 3600,
  385. 'groups' => array('group_a', 'group_b')
  386. ));
  387. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  388. $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
  389. $this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
  390. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  391. }
  392. /**
  393. * Test clearing a cache group
  394. *
  395. * @return void
  396. */
  397. public function testGroupClear() {
  398. Cache::config('memcache_groups', array(
  399. 'engine' => 'Memcache',
  400. 'duration' => 3600,
  401. 'groups' => array('group_a', 'group_b')
  402. ));
  403. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  404. $this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
  405. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  406. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
  407. $this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
  408. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  409. }
  410. }