PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/CacheGarbageCollectorTest.php

https://bitbucket.org/gjerokrsteski/php-dba-cache
PHP | 220 lines | 137 code | 45 blank | 38 comment | 1 complexity | 8413e5fefab60bfd0f2906a5ed230d8a MD5 | raw file
  1. <?php
  2. require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
  3. require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
  4. require_once dirname(dirname(__FILE__)) . '/src/CacheGarbageCollector.php';
  5. require_once dirname(__FILE__) .'/DummyFixtures.php';
  6. class CacheGarbageCollectorTest extends PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @var CacheDba
  10. */
  11. private $_cache;
  12. /**
  13. * Prepares the environment before running a test.
  14. */
  15. protected function setUp()
  16. {
  17. parent::setUp();
  18. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/garbage-collection-test-cache.flat';
  19. try {
  20. $this->_cache = new CacheDba($path);
  21. } catch(RuntimeException $e) {
  22. $this->markTestSkipped($e->getMessage());
  23. }
  24. }
  25. /**
  26. * Cleans up the environment after running a test.
  27. */
  28. protected function tearDown()
  29. {
  30. if ($this->_cache) {
  31. $this->_cache->closeDba();
  32. }
  33. parent::tearDown();
  34. }
  35. public function testCreatingGarbageCollectionObject()
  36. {
  37. $garbageCollection = new CacheGarbageCollector($this->_cache);
  38. $this->assertInstanceOf('CacheGarbageCollector', $garbageCollection);
  39. }
  40. /**
  41. * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
  42. */
  43. public function testCleanAllFromTheGarbageCollection()
  44. {
  45. $dba = $this->_cache->getDba();
  46. // prepare data.
  47. $stdClass = new stdClass();
  48. $stdClass->title = 'Hi firend, i am cached.';
  49. $stdClass->from = 'Joe';
  50. $stdClass->to = 'Hover';
  51. $stdClass->body = 'Yes, it works!';
  52. // put some data to the cache.
  53. $this->_cache->put(md5('stdClass'), $stdClass);
  54. $this->_cache->put(md5('ZipArchive'), new ZipArchive());
  55. $this->_cache->put(md5('XMLReader'), new XMLReader());
  56. $garbageCollection = new CacheGarbageCollector($this->_cache);
  57. $garbageCollection->cleanAll();
  58. $this->assertFalse(dba_fetch(md5('stdClass'), $dba));
  59. $this->assertFalse(dba_fetch(md5('ZipArchive'), $dba));
  60. $this->assertFalse(dba_fetch(md5('XMLReader'), $dba));
  61. }
  62. /**
  63. * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
  64. */
  65. public function testCleanTheGarbageCollectionBySuitableExpirationTime()
  66. {
  67. // prepare data.
  68. $stdClass = new stdClass();
  69. $stdClass->title = 'I am cached.';
  70. $stdClass->from = 'Mike';
  71. $stdClass->to = 'Gates';
  72. $stdClass->body = 'Yes, it works fine!';
  73. // put some data to the cache.
  74. $this->_cache->put(md5('stdClass'), $stdClass, 2);
  75. $this->_cache->put(md5('ZipArchive'), new ZipArchive(), 2);
  76. $this->_cache->put(md5('XMLReader'), new XMLReader(), 2);
  77. // wait two seconds to force the expiration-time-calculation.
  78. sleep(2);
  79. $garbageCollection = new CacheGarbageCollector($this->_cache);
  80. $garbageCollection->cleanByExpiration(1);
  81. $this->assertFalse($this->_cache->has(md5('stdClass')));
  82. $this->assertFalse($this->_cache->has(md5('ZipArchive')));
  83. $this->assertFalse($this->_cache->has(md5('XMLReader')));
  84. }
  85. /**
  86. * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
  87. */
  88. public function testCleanTheGarbageCollectionByNotSuitableExpirationTime()
  89. {
  90. // prepare data.
  91. $stdClass = new stdClass();
  92. $stdClass->title = 'I am cached.';
  93. $stdClass->from = 'Mike';
  94. $stdClass->to = 'Gates';
  95. $stdClass->body = 'Yes, it works fine!';
  96. // put some data to the cache.
  97. $this->_cache->put(md5('stdClass'), $stdClass);
  98. $this->_cache->put(md5('ZipArchive'), new ZipArchive());
  99. $this->_cache->put(md5('XMLReader'), new XMLReader());
  100. // wait one second to force the expiration-time-calculation.
  101. sleep(1);
  102. $garbageCollection = new CacheGarbageCollector($this->_cache);
  103. $garbageCollection->cleanByExpiration(3);
  104. $this->assertInstanceOf('stdClass', $this->_cache->get(md5('stdClass')));
  105. $this->assertInstanceOf('ZipArchive', $this->_cache->get(md5('ZipArchive')));
  106. $this->assertInstanceOf('XMLReader', $this->_cache->get(md5('XMLReader')));
  107. }
  108. /**
  109. * Tests support for CDB - Tiny Constant Database.
  110. * CDB can not be deleted - clear garbage manually.
  111. */
  112. public function testCleanTheGarbageCollectionWithCdbHandler()
  113. {
  114. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';
  115. // create cdb-handler to write.
  116. try {
  117. $cacheMake = new CacheDba($path, 'cdb_make', 'n');
  118. } catch(RuntimeException $e) {
  119. $this->markTestSkipped($e->getMessage());
  120. }
  121. $this->assertInstanceOf('CacheDba', $cacheMake);
  122. $testIdentifier1 = md5('ZipArchive' . time());
  123. $testIdentifier2 = md5('XMLReader' . time());
  124. $this->assertTrue($cacheMake->put($testIdentifier1, new ZipArchive()));
  125. $this->assertTrue($cacheMake->put($testIdentifier2, new XMLReader()));
  126. // CacheGarbageCollector has no effect.
  127. $garbageCollection = new CacheGarbageCollector($cacheMake);
  128. $garbageCollection->cleanAll();
  129. // deleting has no effect.
  130. $cacheMake->delete($testIdentifier1);
  131. $cacheMake->delete($testIdentifier2);
  132. // for read we close the handler.
  133. $cacheMake->closeDba();
  134. // create cdb-handler to read.
  135. try {
  136. $cacheRead = new CacheDba($path, 'cdb', 'r');
  137. } catch(RuntimeException $e) {
  138. $this->markTestSkipped($e->getMessage());
  139. }
  140. $this->assertTrue($cacheRead->has($testIdentifier1));
  141. $this->assertTrue($cacheRead->has($testIdentifier2));
  142. $this->assertInstanceOf('ZipArchive', $cacheRead->get($testIdentifier1));
  143. $this->assertInstanceOf('XMLReader', $cacheRead->get($testIdentifier2));
  144. $cacheRead->closeDba();
  145. }
  146. /**
  147. * Tests support for DB4 - Oracle Berkeley DB 4.
  148. */
  149. public function testCleanTheGarbageCollectionWithDb4Handler()
  150. {
  151. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';
  152. try {
  153. $cache = new CacheDba($path, 'db4', 'c', false);
  154. } catch(RuntimeException $e) {
  155. $this->markTestSkipped($e->getMessage());
  156. }
  157. $this->assertInstanceOf('CacheDba', $cache);
  158. $cache->put(md5('ZipArchive'), new ZipArchive());
  159. $cache->put(md5('XMLReader'), new XMLReader());
  160. $this->assertInstanceOf('ZipArchive', $cache->get(md5('ZipArchive')));
  161. $this->assertInstanceOf('XMLReader', $cache->get(md5('XMLReader')));
  162. $garbageCollection = new CacheGarbageCollector($cache);
  163. $garbageCollection->cleanAll();
  164. $this->assertFalse($cache->get(md5('ZipArchive')));
  165. $this->assertFalse($cache->get(md5('XMLReader')));
  166. $cache->closeDba();
  167. unlink($path);
  168. }
  169. public function testUtilMethods()
  170. {
  171. $garbageCollection = new CacheGarbageCollector($this->_cache);
  172. $this->assertTrue(($garbageCollection->getFillingPercentage() > 0));
  173. $this->assertTrue($garbageCollection->flush());
  174. }
  175. }