|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | +/** |
| 5 | + * Tests for SieveCache |
| 6 | + */ |
| 7 | +class Hm_Test_sieve_cache extends TestCase { |
| 8 | + |
| 9 | + private $mockCache; |
| 10 | + |
| 11 | + public function setUp(): void { |
| 12 | + require 'bootstrap.php'; |
| 13 | + |
| 14 | + $this->mockCache = new MockCache(); |
| 15 | + SieveScriptCache::setCache($this->mockCache); |
| 16 | + |
| 17 | + SieveScriptCache::setCacheTTL(3600); |
| 18 | + } |
| 19 | + |
| 20 | + public function tearDown(): void { |
| 21 | + $this->mockCache->clear(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @preserveGlobalState disabled |
| 26 | + * @runInSeparateProcess |
| 27 | + */ |
| 28 | + public function test_setCache() { |
| 29 | + $cache = new MockCache(); |
| 30 | + SieveScriptCache::setCache($cache); |
| 31 | + |
| 32 | + $this->assertTrue(SieveScriptCache::cacheScript('test_key', 'test_script', 'test_content')); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @preserveGlobalState disabled |
| 37 | + * @runInSeparateProcess |
| 38 | + */ |
| 39 | + public function test_setCache_invalid() { |
| 40 | + $originalCache = new MockCache(); |
| 41 | + SieveScriptCache::setCache($originalCache); |
| 42 | + |
| 43 | + SieveScriptCache::setCache("invalid_cache"); |
| 44 | + |
| 45 | + $this->assertTrue(SieveScriptCache::cacheScript('test_key', 'test_script', 'test_content')); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @preserveGlobalState disabled |
| 50 | + * @runInSeparateProcess |
| 51 | + */ |
| 52 | + public function test_setCacheTTL() { |
| 53 | + SieveScriptCache::setCacheTTL(1800); |
| 54 | + |
| 55 | + SieveScriptCache::cacheScript('test_key', 'test_script', 'test_content'); |
| 56 | + |
| 57 | + $this->assertEquals('test_content', SieveScriptCache::getCachedScript('test_key', 'test_script')); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @preserveGlobalState disabled |
| 62 | + * @runInSeparateProcess |
| 63 | + */ |
| 64 | + public function test_cacheScript_and_getCachedScript() { |
| 65 | + $key = 'test_server'; |
| 66 | + $scriptName = 'vacation.sieve'; |
| 67 | + $scriptContent = 'require "vacation"; vacation :days 10 "I\'m on vacation";'; |
| 68 | + |
| 69 | + $result = SieveScriptCache::cacheScript($key, $scriptName, $scriptContent); |
| 70 | + $this->assertTrue($result); |
| 71 | + |
| 72 | + $cached = SieveScriptCache::getCachedScript($key, $scriptName); |
| 73 | + $this->assertEquals($scriptContent, $cached); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @preserveGlobalState disabled |
| 78 | + * @runInSeparateProcess |
| 79 | + */ |
| 80 | + public function test_getCachedScript_not_found() { |
| 81 | + $result = SieveScriptCache::getCachedScript('nonexistent_key', 'nonexistent_script'); |
| 82 | + $this->assertFalse($result); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @preserveGlobalState disabled |
| 87 | + * @runInSeparateProcess |
| 88 | + */ |
| 89 | + public function test_getCachedScript_expired() { |
| 90 | + // Set a very short TTL |
| 91 | + SieveScriptCache::setCacheTTL(1); |
| 92 | + |
| 93 | + $key = 'test_server'; |
| 94 | + $scriptName = 'test.sieve'; |
| 95 | + $scriptContent = 'test content'; |
| 96 | + |
| 97 | + SieveScriptCache::cacheScript($key, $scriptName, $scriptContent); |
| 98 | + |
| 99 | + // Wait for expiration |
| 100 | + sleep(2); |
| 101 | + |
| 102 | + $result = SieveScriptCache::getCachedScript($key, $scriptName); |
| 103 | + $this->assertFalse($result); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @preserveGlobalState disabled |
| 108 | + * @runInSeparateProcess |
| 109 | + */ |
| 110 | + public function test_invalidateScript() { |
| 111 | + $key = 'test_server'; |
| 112 | + $scriptName = 'test.sieve'; |
| 113 | + $scriptContent = 'test content'; |
| 114 | + |
| 115 | + SieveScriptCache::cacheScript($key, $scriptName, $scriptContent); |
| 116 | + |
| 117 | + $this->assertEquals($scriptContent, SieveScriptCache::getCachedScript($key, $scriptName)); |
| 118 | + |
| 119 | + $result = SieveScriptCache::invalidateScript($key, $scriptName); |
| 120 | + $this->assertTrue($result); |
| 121 | + |
| 122 | + $this->assertFalse(SieveScriptCache::getCachedScript($key, $scriptName)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @preserveGlobalState disabled |
| 127 | + * @runInSeparateProcess |
| 128 | + */ |
| 129 | + public function test_cacheScriptsList_and_getCachedScriptsList() { |
| 130 | + $key = 'test_server'; |
| 131 | + $scripts = ['vacation.sieve', 'spam.sieve', 'forward.sieve']; |
| 132 | + |
| 133 | + $result = SieveScriptCache::cacheScriptsList($key, $scripts); |
| 134 | + $this->assertTrue($result); |
| 135 | + |
| 136 | + $cached = SieveScriptCache::getCachedScriptsList($key); |
| 137 | + $this->assertEquals($scripts, $cached); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @preserveGlobalState disabled |
| 142 | + * @runInSeparateProcess |
| 143 | + */ |
| 144 | + public function test_invalidateScriptsList() { |
| 145 | + $key = 'test_server'; |
| 146 | + $scripts = ['test1.sieve', 'test2.sieve']; |
| 147 | + |
| 148 | + SieveScriptCache::cacheScriptsList($key, $scripts); |
| 149 | + |
| 150 | + $this->assertEquals($scripts, SieveScriptCache::getCachedScriptsList($key)); |
| 151 | + |
| 152 | + $result = SieveScriptCache::invalidateScriptsList($key); |
| 153 | + $this->assertTrue($result); |
| 154 | + |
| 155 | + $this->assertFalse(SieveScriptCache::getCachedScriptsList($key)); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @preserveGlobalState disabled |
| 160 | + * @runInSeparateProcess |
| 161 | + */ |
| 162 | + public function test_isCached() { |
| 163 | + $key = 'test_server'; |
| 164 | + $scriptName = 'test.sieve'; |
| 165 | + $scriptContent = 'test content'; |
| 166 | + |
| 167 | + $this->assertFalse(SieveScriptCache::isCached($key, $scriptName)); |
| 168 | + |
| 169 | + SieveScriptCache::cacheScript($key, $scriptName, $scriptContent); |
| 170 | + |
| 171 | + $this->assertTrue(SieveScriptCache::isCached($key, $scriptName)); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @preserveGlobalState disabled |
| 176 | + * @runInSeparateProcess |
| 177 | + */ |
| 178 | + public function test_clearScriptCache() { |
| 179 | + $key = 'test_server'; |
| 180 | + $scriptName = 'test.sieve'; |
| 181 | + $scriptContent = 'test content'; |
| 182 | + |
| 183 | + SieveScriptCache::cacheScript($key, $scriptName, $scriptContent); |
| 184 | + |
| 185 | + $result = SieveScriptCache::clearScriptCache($key, $scriptName); |
| 186 | + $this->assertTrue($result); |
| 187 | + |
| 188 | + $this->assertFalse(SieveScriptCache::getCachedScript($key, $scriptName)); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @preserveGlobalState disabled |
| 193 | + * @runInSeparateProcess |
| 194 | + */ |
| 195 | + public function test_clearAllCache() { |
| 196 | + $key = 'test_server'; |
| 197 | + |
| 198 | + SieveScriptCache::cacheScript($key, 'script1.sieve', 'content1'); |
| 199 | + SieveScriptCache::cacheScript($key, 'script2.sieve', 'content2'); |
| 200 | + SieveScriptCache::cacheScriptsList($key, ['script1.sieve', 'script2.sieve']); |
| 201 | + |
| 202 | + $result = SieveScriptCache::clearAllCache($key); |
| 203 | + $this->assertTrue($result); |
| 204 | + |
| 205 | + $this->assertFalse(SieveScriptCache::getCachedScriptsList($key)); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @preserveGlobalState disabled |
| 210 | + * @runInSeparateProcess |
| 211 | + */ |
| 212 | + public function test_operations_without_cache() { |
| 213 | + // Force cache to null using reflection since setCache(null) doesn't work |
| 214 | + $reflection = new ReflectionClass('SieveScriptCache'); |
| 215 | + $cacheProperty = $reflection->getProperty('cache'); |
| 216 | + $cacheProperty->setAccessible(true); |
| 217 | + $cacheProperty->setValue(null); |
| 218 | + |
| 219 | + // All operations should return false |
| 220 | + $this->assertFalse(SieveScriptCache::cacheScript('key', 'script', 'content')); |
| 221 | + $this->assertFalse(SieveScriptCache::getCachedScript('key', 'script')); |
| 222 | + $this->assertFalse(SieveScriptCache::invalidateScript('key', 'script')); |
| 223 | + $this->assertFalse(SieveScriptCache::cacheScriptsList('key', [])); |
| 224 | + $this->assertFalse(SieveScriptCache::getCachedScriptsList('key')); |
| 225 | + $this->assertFalse(SieveScriptCache::invalidateScriptsList('key')); |
| 226 | + $this->assertFalse(SieveScriptCache::clearScriptCache('key', 'script')); |
| 227 | + $this->assertFalse(SieveScriptCache::clearAllCache('key')); |
| 228 | + |
| 229 | + // Restore cache for other tests |
| 230 | + $this->mockCache = new MockCache(); |
| 231 | + SieveScriptCache::setCache($this->mockCache); |
| 232 | + } |
| 233 | +} |
0 commit comments