XcacheTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * XcacheEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.cache
  16. * @since CakePHP(tm) v 1.2.0.5434
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Cache', 'Cache');
  20. /**
  21. * XcacheEngineTest class
  22. *
  23. * @package cake.tests.cases.libs.cache
  24. */
  25. class XcacheEngineTest extends CakeTestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. function setUp() {
  33. $this->skipUnless(function_exists('xcache_set'), 'Xcache is not installed or configured properly');
  34. $this->_cacheDisable = Configure::read('Cache.disable');
  35. Configure::write('Cache.disable', false);
  36. Cache::config('xcache', array('engine' => 'Xcache', 'prefix' => 'cake_'));
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. function tearDown() {
  45. Configure::write('Cache.disable', $this->_cacheDisable);
  46. Cache::config('default');
  47. }
  48. /**
  49. * testSettings method
  50. *
  51. * @access public
  52. * @return void
  53. */
  54. function testSettings() {
  55. $settings = Cache::settings();
  56. $expecting = array(
  57. 'prefix' => 'cake_',
  58. 'duration'=> 3600,
  59. 'probability' => 100,
  60. 'engine' => 'Xcache',
  61. );
  62. $this->assertTrue(isset($settings['PHP_AUTH_USER']));
  63. $this->assertTrue(isset($settings['PHP_AUTH_PW']));
  64. unset($settings['PHP_AUTH_USER'], $settings['PHP_AUTH_PW']);
  65. $this->assertEqual($settings, $expecting);
  66. }
  67. /**
  68. * testReadAndWriteCache method
  69. *
  70. * @access public
  71. * @return void
  72. */
  73. function testReadAndWriteCache() {
  74. Cache::set(array('duration' => 1));
  75. $result = Cache::read('test');
  76. $expecting = '';
  77. $this->assertEqual($result, $expecting);
  78. $data = 'this is a test of the emergency broadcasting system';
  79. $result = Cache::write('test', $data);
  80. $this->assertTrue($result);
  81. $result = Cache::read('test');
  82. $expecting = $data;
  83. $this->assertEqual($result, $expecting);
  84. Cache::delete('test');
  85. }
  86. /**
  87. * testExpiry method
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. function testExpiry() {
  93. Cache::set(array('duration' => 1));
  94. $result = Cache::read('test');
  95. $this->assertFalse($result);
  96. $data = 'this is a test of the emergency broadcasting system';
  97. $result = Cache::write('other_test', $data);
  98. $this->assertTrue($result);
  99. sleep(2);
  100. $result = Cache::read('other_test');
  101. $this->assertFalse($result);
  102. Cache::set(array('duration' => "+1 second"));
  103. $data = 'this is a test of the emergency broadcasting system';
  104. $result = Cache::write('other_test', $data);
  105. $this->assertTrue($result);
  106. sleep(2);
  107. $result = Cache::read('other_test');
  108. $this->assertFalse($result);
  109. }
  110. /**
  111. * testDeleteCache method
  112. *
  113. * @access public
  114. * @return void
  115. */
  116. function testDeleteCache() {
  117. $data = 'this is a test of the emergency broadcasting system';
  118. $result = Cache::write('delete_test', $data);
  119. $this->assertTrue($result);
  120. $result = Cache::delete('delete_test');
  121. $this->assertTrue($result);
  122. }
  123. /**
  124. * testClearCache method
  125. *
  126. * @access public
  127. * @return void
  128. */
  129. function testClearCache() {
  130. $data = 'this is a test of the emergency broadcasting system';
  131. $result = Cache::write('clear_test_1', $data);
  132. $this->assertTrue($result);
  133. $result = Cache::write('clear_test_2', $data);
  134. $this->assertTrue($result);
  135. $result = Cache::clear();
  136. $this->assertTrue($result);
  137. }
  138. /**
  139. * testDecrement method
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. function testDecrement() {
  145. $result = Cache::write('test_decrement', 5);
  146. $this->assertTrue($result);
  147. $result = Cache::decrement('test_decrement');
  148. $this->assertEqual(4, $result);
  149. $result = Cache::read('test_decrement');
  150. $this->assertEqual(4, $result);
  151. $result = Cache::decrement('test_decrement', 2);
  152. $this->assertEqual(2, $result);
  153. $result = Cache::read('test_decrement');
  154. $this->assertEqual(2, $result);
  155. }
  156. /**
  157. * testIncrement method
  158. *
  159. * @access public
  160. * @return void
  161. */
  162. function testIncrement() {
  163. $result = Cache::write('test_increment', 5);
  164. $this->assertTrue($result);
  165. $result = Cache::increment('test_increment');
  166. $this->assertEqual(6, $result);
  167. $result = Cache::read('test_increment');
  168. $this->assertEqual(6, $result);
  169. $result = Cache::increment('test_increment', 2);
  170. $this->assertEqual(8, $result);
  171. $result = Cache::read('test_increment');
  172. $this->assertEqual(8, $result);
  173. }
  174. }