ApcEngineTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * ApcEngineTest 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. * ApcEngineTest class
  22. *
  23. * @package cake.tests.cases.libs.cache
  24. */
  25. class ApcEngineTest extends CakeTestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. function setUp() {
  33. $this->skipIf(!function_exists('apc_store'), '%s Apc is not installed or configured properly');
  34. $this->_cacheDisable = Configure::read('Cache.disable');
  35. Configure::write('Cache.disable', false);
  36. Cache::config('apc', array('engine' => 'Apc', '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::drop('apc');
  47. Cache::config('default');
  48. }
  49. /**
  50. * testReadAndWriteCache method
  51. *
  52. * @access public
  53. * @return void
  54. */
  55. function testReadAndWriteCache() {
  56. Cache::set(array('duration' => 1), 'apc');
  57. $result = Cache::read('test', 'apc');
  58. $expecting = '';
  59. $this->assertEqual($result, $expecting);
  60. $data = 'this is a test of the emergency broadcasting system';
  61. $result = Cache::write('test', $data, 'apc');
  62. $this->assertTrue($result);
  63. $result = Cache::read('test', 'apc');
  64. $expecting = $data;
  65. $this->assertEqual($result, $expecting);
  66. Cache::delete('test', 'apc');
  67. }
  68. /**
  69. * testExpiry method
  70. *
  71. * @access public
  72. * @return void
  73. */
  74. function testExpiry() {
  75. Cache::set(array('duration' => 1), 'apc');
  76. $result = Cache::read('test', 'apc');
  77. $this->assertFalse($result);
  78. $data = 'this is a test of the emergency broadcasting system';
  79. $result = Cache::write('other_test', $data, 'apc');
  80. $this->assertTrue($result);
  81. sleep(2);
  82. $result = Cache::read('other_test', 'apc');
  83. $this->assertFalse($result);
  84. Cache::set(array('duration' => 1), 'apc');
  85. $data = 'this is a test of the emergency broadcasting system';
  86. $result = Cache::write('other_test', $data, 'apc');
  87. $this->assertTrue($result);
  88. sleep(2);
  89. $result = Cache::read('other_test', 'apc');
  90. $this->assertFalse($result);
  91. sleep(2);
  92. $result = Cache::read('other_test', 'apc');
  93. $this->assertFalse($result);
  94. }
  95. /**
  96. * testDeleteCache method
  97. *
  98. * @access public
  99. * @return void
  100. */
  101. function testDeleteCache() {
  102. $data = 'this is a test of the emergency broadcasting system';
  103. $result = Cache::write('delete_test', $data, 'apc');
  104. $this->assertTrue($result);
  105. $result = Cache::delete('delete_test', 'apc');
  106. $this->assertTrue($result);
  107. }
  108. /**
  109. * testDecrement method
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. function testDecrement() {
  115. if ($this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement() %s')) {
  116. return;
  117. }
  118. $result = Cache::write('test_decrement', 5, 'apc');
  119. $this->assertTrue($result);
  120. $result = Cache::decrement('test_decrement', 1, 'apc');
  121. $this->assertEqual(4, $result);
  122. $result = Cache::read('test_decrement', 'apc');
  123. $this->assertEqual(4, $result);
  124. $result = Cache::decrement('test_decrement', 2, 'apc');
  125. $this->assertEqual(2, $result);
  126. $result = Cache::read('test_decrement', 'apc');
  127. $this->assertEqual(2, $result);
  128. }
  129. /**
  130. * testIncrement method
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. function testIncrement() {
  136. if ($this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment() %s')) {
  137. return;
  138. }
  139. $result = Cache::write('test_increment', 5, 'apc');
  140. $this->assertTrue($result);
  141. $result = Cache::increment('test_increment', 1, 'apc');
  142. $this->assertEqual(6, $result);
  143. $result = Cache::read('test_increment', 'apc');
  144. $this->assertEqual(6, $result);
  145. $result = Cache::increment('test_increment', 2, 'apc');
  146. $this->assertEqual(8, $result);
  147. $result = Cache::read('test_increment', 'apc');
  148. $this->assertEqual(8, $result);
  149. }
  150. /**
  151. * test the clearing of cache keys
  152. *
  153. * @return void
  154. */
  155. function testClear() {
  156. Cache::write('some_value', 'value', 'apc');
  157. $result = Cache::clear(false, 'apc');
  158. $this->assertTrue($result);
  159. $this->assertFalse(Cache::read('some_value', 'apc'));
  160. }
  161. }