Open Journal Systems  3.3.0
FileCacheTest.php
1 <?php
2 
18 import('lib.pkp.tests.PKPTestCase');
19 
20 class FileCacheTest extends PKPTestCase {
21 
23  var $cacheManager;
24 
27 
28  var $testCacheContents = array(
29  0 => 'zero',
30  1 => 'one',
31  2 => 'two',
32  3 => 'three',
33  );
34 
38  public function testGetCache() {
39  // Get the file cache.
40  $fileCache = $this->getCache();
41 
42  // No cache misses should be registered.
43  self::assertTrue($this->cacheMisses == 0);
44 
45  // The cache has just been flushed by setUp. Try a get.
46  $val1 = $fileCache->get(1);
47 
48  // Make sure the returned value was correct
49  self::assertTrue($val1 == 'one');
50 
51  // Make sure we registered one cache miss
52  self::assertTrue($this->cacheMisses == 1);
53 
54  // Try another get
55  $val2 = $fileCache->get(2);
56 
57  // Make sure the value was correct
58  self::assertTrue($val2 == 'two');
59 
60  // Make sure we didn't have to register another cache miss
61  self::assertTrue($this->cacheMisses == 1);
62  }
63 
67  public function testCacheMiss() {
68  $this->markTestSkipped();
69 
70  // Get the file cache.
71  $fileCache = $this->getCache();
72 
73  // Try to get an item that's not in the cache
74  $val1 = $fileCache->get(-1);
75 
76  // Make sure we registered one cache miss
77  self::assertTrue($val1 == null);
78  self::assertTrue($this->cacheMisses == 1);
79 
80  // Try another get of the same item
81  $val2 = $fileCache->get(-1);
82 
83  // Check to see that we got it without a second miss
84  self::assertTrue($val2 == null);
85 
86  // WARNING: This will trigger bug #8039 until fixed.
87  self::assertTrue($this->cacheMisses == 1);
88  }
89 
90  //
91  // Helper functions
92  //
93  public function _cacheMiss($cache, $id) {
94  $this->cacheMisses++;
95  $cache->setEntireCache($this->testCacheContents);
96  if (!isset($this->testCacheContents[$id])) {
97  $cache->setCache($id, null);
98  return null;
99  }
100  return $this->testCacheContents[$id];
101  }
102 
103  //
104  // Protected methods.
105  //
106  protected function setUp() : void {
107  $this->cacheManager = CacheManager::getManager();
108  $this->cacheMisses = 0;
109 
110  if (!is_writable($this->cacheManager->getFileCachePath())) {
111  $this->markTestSkipped('File cache path not writable.');
112  } else {
113  parent::setUp();
114  $this->cacheManager->flush();
115  }
116  }
117 
121  protected function getCache() {
122  return $this->cacheManager->getFileCache('testCache', 0, array($this, '_cacheMiss'));
123  }
124 }
125 
126 
FileCacheTest\$cacheManager
$cacheManager
Definition: FileCacheTest.php:26
FileCacheTest
Tests for the FileCache class.
Definition: FileCacheTest.php:20
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
FileCacheTest\testCacheMiss
testCacheMiss()
Definition: FileCacheTest.php:73
FileCacheTest\_cacheMiss
_cacheMiss($cache, $id)
Definition: FileCacheTest.php:99
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
FileCacheTest\testGetCache
testGetCache()
Definition: FileCacheTest.php:44
FileCacheTest\$testCacheContents
$testCacheContents
Definition: FileCacheTest.php:34
FileCacheTest\$cacheMisses
$cacheMisses
Definition: FileCacheTest.php:32
FileCacheTest\setUp
setUp()
Definition: FileCacheTest.php:112
FileCacheTest\getCache
getCache()
Definition: FileCacheTest.php:127