php-dba-cache /tests/CacheGarbageCollectorTest.php

Language PHP Lines 221
MD5 Hash 8413e5fefab60bfd0f2906a5ed230d8a Estimated Cost $3,326 (why?)
Repository https://bitbucket.org/gjerokrsteski/php-dba-cache.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
require_once dirname(dirname(__FILE__)) . '/src/CacheGarbageCollector.php';
require_once dirname(__FILE__) .'/DummyFixtures.php';

class CacheGarbageCollectorTest extends PHPUnit_Framework_TestCase
{
  /**
   * @var CacheDba
   */
  private $_cache;

  /**
   * Prepares the environment before running a test.
   */
  protected function setUp()
  {
    parent::setUp();

    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/garbage-collection-test-cache.flat';

    try {
      $this->_cache = new CacheDba($path);
    } catch(RuntimeException $e) {
      $this->markTestSkipped($e->getMessage());
    }
  }

  /**
   * Cleans up the environment after running a test.
   */
  protected function tearDown()
  {
	if ($this->_cache) {
	   $this->_cache->closeDba();
	}
    parent::tearDown();
  }

  public function testCreatingGarbageCollectionObject()
  {
    $garbageCollection = new CacheGarbageCollector($this->_cache);

    $this->assertInstanceOf('CacheGarbageCollector', $garbageCollection);
  }

  /**
   * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
   */
  public function testCleanAllFromTheGarbageCollection()
  {
    $dba = $this->_cache->getDba();

    // prepare data.
    $stdClass        = new stdClass();
    $stdClass->title = 'Hi firend, i am cached.';
    $stdClass->from  = 'Joe';
    $stdClass->to    = 'Hover';
    $stdClass->body  = 'Yes, it works!';

    // put some data to the cache.
    $this->_cache->put(md5('stdClass'), $stdClass);
    $this->_cache->put(md5('ZipArchive'), new ZipArchive());
    $this->_cache->put(md5('XMLReader'), new XMLReader());

    $garbageCollection = new CacheGarbageCollector($this->_cache);
    $garbageCollection->cleanAll();

    $this->assertFalse(dba_fetch(md5('stdClass'), $dba));
    $this->assertFalse(dba_fetch(md5('ZipArchive'), $dba));
    $this->assertFalse(dba_fetch(md5('XMLReader'), $dba));
  }

  /**
   * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
   */
  public function testCleanTheGarbageCollectionBySuitableExpirationTime()
  {
    // prepare data.
    $stdClass        = new stdClass();
    $stdClass->title = 'I am cached.';
    $stdClass->from  = 'Mike';
    $stdClass->to    = 'Gates';
    $stdClass->body  = 'Yes, it works fine!';

    // put some data to the cache.
    $this->_cache->put(md5('stdClass'), $stdClass, 2);
    $this->_cache->put(md5('ZipArchive'), new ZipArchive(), 2);
    $this->_cache->put(md5('XMLReader'), new XMLReader(), 2);

    // wait two seconds to force the expiration-time-calculation.
    sleep(2);

    $garbageCollection = new CacheGarbageCollector($this->_cache);
    $garbageCollection->cleanByExpiration(1);

    $this->assertFalse($this->_cache->has(md5('stdClass')));
    $this->assertFalse($this->_cache->has(md5('ZipArchive')));
    $this->assertFalse($this->_cache->has(md5('XMLReader')));
  }

  /**
   * @depends CacheGarbageCollectorTest::testCreatingGarbageCollectionObject
   */
  public function testCleanTheGarbageCollectionByNotSuitableExpirationTime()
  {
    // prepare data.
    $stdClass        = new stdClass();
    $stdClass->title = 'I am cached.';
    $stdClass->from  = 'Mike';
    $stdClass->to    = 'Gates';
    $stdClass->body  = 'Yes, it works fine!';

    // put some data to the cache.
    $this->_cache->put(md5('stdClass'), $stdClass);
    $this->_cache->put(md5('ZipArchive'), new ZipArchive());
    $this->_cache->put(md5('XMLReader'), new XMLReader());

    // wait one second to force the expiration-time-calculation.
    sleep(1);

    $garbageCollection = new CacheGarbageCollector($this->_cache);
    $garbageCollection->cleanByExpiration(3);

    $this->assertInstanceOf('stdClass', $this->_cache->get(md5('stdClass')));
    $this->assertInstanceOf('ZipArchive', $this->_cache->get(md5('ZipArchive')));
    $this->assertInstanceOf('XMLReader', $this->_cache->get(md5('XMLReader')));
  }

  /**
   * Tests support for CDB - Tiny Constant Database.
   * CDB can not be deleted - clear garbage manually.
   */
  public function testCleanTheGarbageCollectionWithCdbHandler()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';

    // create cdb-handler to write.
    try {
      $cacheMake = new CacheDba($path, 'cdb_make', 'n');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertInstanceOf('CacheDba', $cacheMake);

    $testIdentifier1 = md5('ZipArchive' . time());
    $testIdentifier2 = md5('XMLReader' . time());

    $this->assertTrue($cacheMake->put($testIdentifier1, new ZipArchive()));
    $this->assertTrue($cacheMake->put($testIdentifier2, new XMLReader()));

    // CacheGarbageCollector has no effect.
    $garbageCollection = new CacheGarbageCollector($cacheMake);
    $garbageCollection->cleanAll();

    // deleting has no effect.
    $cacheMake->delete($testIdentifier1);
    $cacheMake->delete($testIdentifier2);

    // for read we close the handler.
    $cacheMake->closeDba();

    // create cdb-handler to read.
    try {
      $cacheRead = new CacheDba($path, 'cdb', 'r');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertTrue($cacheRead->has($testIdentifier1));
    $this->assertTrue($cacheRead->has($testIdentifier2));

    $this->assertInstanceOf('ZipArchive', $cacheRead->get($testIdentifier1));
    $this->assertInstanceOf('XMLReader', $cacheRead->get($testIdentifier2));

    $cacheRead->closeDba();
  }

  /**
   * Tests support for DB4 - Oracle Berkeley DB 4.
   */
  public function testCleanTheGarbageCollectionWithDb4Handler()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';

    try {
      $cache = new CacheDba($path, 'db4', 'c', false);
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertInstanceOf('CacheDba', $cache);

    $cache->put(md5('ZipArchive'), new ZipArchive());
    $cache->put(md5('XMLReader'), new XMLReader());

    $this->assertInstanceOf('ZipArchive', $cache->get(md5('ZipArchive')));
    $this->assertInstanceOf('XMLReader', $cache->get(md5('XMLReader')));

    $garbageCollection = new CacheGarbageCollector($cache);
    $garbageCollection->cleanAll();

    $this->assertFalse($cache->get(md5('ZipArchive')));
    $this->assertFalse($cache->get(md5('XMLReader')));

    $cache->closeDba();

    unlink($path);
  }

  public function testUtilMethods()
  {
    $garbageCollection = new CacheGarbageCollector($this->_cache);

    $this->assertTrue(($garbageCollection->getFillingPercentage() > 0));
    $this->assertTrue($garbageCollection->flush());
  }
}
Back to Top