/cake/tests/cases/libs/cache/memcache.test.php

https://github.com/hardsshah/bookmarks · PHP · 221 lines · 109 code · 22 blank · 90 comment · 4 complexity · 1feb9ea1763b0bcefd3ca61969762140 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * MemcacheEngineTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.cache
  21. * @since CakePHP(tm) v 1.2.0.5434
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!class_exists('Cache')) {
  28. require LIBS . 'cache.php';
  29. }/**
  30. * MemcacheEngineTest class
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.cache
  34. */
  35. /**
  36. * MemcacheEngineTest class
  37. *
  38. * @package cake
  39. * @subpackage cake.tests.cases.libs.cache
  40. */
  41. class MemcacheEngineTest extends CakeTestCase {
  42. /**
  43. * skip method
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function skip() {
  49. $skip = true;
  50. if (Cache::engine('Memcache')) {
  51. $skip = false;
  52. }
  53. $this->skipIf($skip, 'Memcache is not installed or configured properly');
  54. }
  55. /**
  56. * setUp method
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. function setUp() {
  62. Cache::config('memcache', array('engine'=>'Memcache', 'prefix' => 'cake_'));
  63. }
  64. /**
  65. * tearDown method
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. function tearDown() {
  71. Cache::config('default');
  72. }
  73. /**
  74. * testSettings method
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. function testSettings() {
  80. $settings = Cache::settings();
  81. $expecting = array('prefix' => 'cake_',
  82. 'duration'=> 3600,
  83. 'probability' => 100,
  84. 'servers' => array('127.0.0.1'),
  85. 'compress' => false,
  86. 'engine' => 'Memcache'
  87. );
  88. $this->assertEqual($settings, $expecting);
  89. }
  90. /**
  91. * testSettings method
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. function testMultipleServers() {
  97. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  98. $Cache =& Cache::getInstance();
  99. $MemCache =& $Cache->_Engine['Memcache'];
  100. $available = true;
  101. foreach($servers as $server) {
  102. list($host, $port) = explode(':', $server);
  103. if (!@$MemCache->__Memcache->connect($host, $port)) {
  104. $available = false;
  105. }
  106. }
  107. if ($this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test')) {
  108. return;
  109. }
  110. unset($MemCache->__Memcache);
  111. $MemCache->init(array('engine' => 'Memcache', 'servers' => $servers));
  112. $servers = array_keys($MemCache->__Memcache->getExtendedStats());
  113. $settings = Cache::settings();
  114. $this->assertEqual($servers, $settings['servers']);
  115. }
  116. /**
  117. * testConnect method
  118. *
  119. * @access public
  120. * @return void
  121. */
  122. function testConnect() {
  123. $Cache =& Cache::getInstance();
  124. $result = $Cache->_Engine['Memcache']->connect('127.0.0.1');
  125. $this->assertTrue($result);
  126. }
  127. /**
  128. * testReadAndWriteCache method
  129. *
  130. * @access public
  131. * @return void
  132. */
  133. function testReadAndWriteCache() {
  134. Cache::set(array('duration' => 1));
  135. $result = Cache::read('test');
  136. $expecting = '';
  137. $this->assertEqual($result, $expecting);
  138. $data = 'this is a test of the emergency broadcasting system';
  139. $result = Cache::write('test', $data);
  140. $this->assertTrue($result);
  141. $result = Cache::read('test');
  142. $expecting = $data;
  143. $this->assertEqual($result, $expecting);
  144. Cache::delete('test');
  145. }
  146. /**
  147. * testExpiry method
  148. *
  149. * @access public
  150. * @return void
  151. */
  152. function testExpiry() {
  153. Cache::set(array('duration' => 1));
  154. $result = Cache::read('test');
  155. $this->assertFalse($result);
  156. $data = 'this is a test of the emergency broadcasting system';
  157. $result = Cache::write('other_test', $data);
  158. $this->assertTrue($result);
  159. sleep(2);
  160. $result = Cache::read('other_test');
  161. $this->assertFalse($result);
  162. Cache::set(array('duration' => "+1 second"));
  163. $data = 'this is a test of the emergency broadcasting system';
  164. $result = Cache::write('other_test', $data);
  165. $this->assertTrue($result);
  166. sleep(2);
  167. $result = Cache::read('other_test');
  168. $this->assertFalse($result);
  169. Cache::engine('Memcache', array('duration' => '+1 second'));
  170. sleep(2);
  171. $result = Cache::read('other_test');
  172. $this->assertFalse($result);
  173. Cache::engine('Memcache', array('duration' => '+31 day'));
  174. $data = 'this is a test of the emergency broadcasting system';
  175. $result = Cache::write('long_expiry_test', $data);
  176. $this->assertTrue($result);
  177. sleep(2);
  178. $result = Cache::read('long_expiry_test');
  179. $expecting = $data;
  180. $this->assertEqual($result, $expecting);
  181. $result = Cache::read('long_expiry_test');
  182. $this->assertTrue($result);
  183. Cache::engine('Memcache', array('duration' => 3600));
  184. }
  185. /**
  186. * testDeleteCache method
  187. *
  188. * @access public
  189. * @return void
  190. */
  191. function testDeleteCache() {
  192. $data = 'this is a test of the emergency broadcasting system';
  193. $result = Cache::write('delete_test', $data);
  194. $this->assertTrue($result);
  195. $result = Cache::delete('delete_test');
  196. $this->assertTrue($result);
  197. }
  198. }
  199. ?>