XcacheEngineTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Cache.Engine
  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.Test.Case.Cache.Engine
  24. */
  25. class XcacheEngineTest extends CakeTestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. if (!function_exists('xcache_set')) {
  33. $this->markTestSkipped('Xcache is not installed or configured properly');
  34. }
  35. $this->_cacheDisable = Configure::read('Cache.disable');
  36. Configure::write('Cache.disable', false);
  37. Cache::config('xcache', array('engine' => 'Xcache', 'prefix' => 'cake_'));
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. Configure::write('Cache.disable', $this->_cacheDisable);
  46. Cache::config('default');
  47. }
  48. /**
  49. * testSettings method
  50. *
  51. * @return void
  52. */
  53. public function testSettings() {
  54. $settings = Cache::settings();
  55. $expecting = array(
  56. 'prefix' => 'cake_',
  57. 'duration'=> 3600,
  58. 'probability' => 100,
  59. 'engine' => 'Xcache',
  60. );
  61. $this->assertTrue(isset($settings['PHP_AUTH_USER']));
  62. $this->assertTrue(isset($settings['PHP_AUTH_PW']));
  63. unset($settings['PHP_AUTH_USER'], $settings['PHP_AUTH_PW']);
  64. $this->assertEquals($settings, $expecting);
  65. }
  66. /**
  67. * testReadAndWriteCache method
  68. *
  69. * @return void
  70. */
  71. public function testReadAndWriteCache() {
  72. Cache::set(array('duration' => 1));
  73. $result = Cache::read('test');
  74. $expecting = '';
  75. $this->assertEquals($result, $expecting);
  76. $data = 'this is a test of the emergency broadcasting system';
  77. $result = Cache::write('test', $data);
  78. $this->assertTrue($result);
  79. $result = Cache::read('test');
  80. $expecting = $data;
  81. $this->assertEquals($result, $expecting);
  82. Cache::delete('test');
  83. }
  84. /**
  85. * testExpiry method
  86. *
  87. * @return void
  88. */
  89. public function testExpiry() {
  90. Cache::set(array('duration' => 1));
  91. $result = Cache::read('test');
  92. $this->assertFalse($result);
  93. $data = 'this is a test of the emergency broadcasting system';
  94. $result = Cache::write('other_test', $data);
  95. $this->assertTrue($result);
  96. sleep(2);
  97. $result = Cache::read('other_test');
  98. $this->assertFalse($result);
  99. Cache::set(array('duration' => "+1 second"));
  100. $data = 'this is a test of the emergency broadcasting system';
  101. $result = Cache::write('other_test', $data);
  102. $this->assertTrue($result);
  103. sleep(2);
  104. $result = Cache::read('other_test');
  105. $this->assertFalse($result);
  106. }
  107. /**
  108. * testDeleteCache method
  109. *
  110. * @return void
  111. */
  112. public function testDeleteCache() {
  113. $data = 'this is a test of the emergency broadcasting system';
  114. $result = Cache::write('delete_test', $data);
  115. $this->assertTrue($result);
  116. $result = Cache::delete('delete_test');
  117. $this->assertTrue($result);
  118. }
  119. /**
  120. * testClearCache method
  121. *
  122. * @return void
  123. */
  124. public function testClearCache() {
  125. $data = 'this is a test of the emergency broadcasting system';
  126. $result = Cache::write('clear_test_1', $data);
  127. $this->assertTrue($result);
  128. $result = Cache::write('clear_test_2', $data);
  129. $this->assertTrue($result);
  130. $result = Cache::clear();
  131. $this->assertTrue($result);
  132. }
  133. /**
  134. * testDecrement method
  135. *
  136. * @return void
  137. */
  138. public function testDecrement() {
  139. $result = Cache::write('test_decrement', 5);
  140. $this->assertTrue($result);
  141. $result = Cache::decrement('test_decrement');
  142. $this->assertEquals(4, $result);
  143. $result = Cache::read('test_decrement');
  144. $this->assertEquals(4, $result);
  145. $result = Cache::decrement('test_decrement', 2);
  146. $this->assertEquals(2, $result);
  147. $result = Cache::read('test_decrement');
  148. $this->assertEquals(2, $result);
  149. }
  150. /**
  151. * testIncrement method
  152. *
  153. * @return void
  154. */
  155. public function testIncrement() {
  156. $result = Cache::write('test_increment', 5);
  157. $this->assertTrue($result);
  158. $result = Cache::increment('test_increment');
  159. $this->assertEquals(6, $result);
  160. $result = Cache::read('test_increment');
  161. $this->assertEquals(6, $result);
  162. $result = Cache::increment('test_increment', 2);
  163. $this->assertEquals(8, $result);
  164. $result = Cache::read('test_increment');
  165. $this->assertEquals(8, $result);
  166. }
  167. }