|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bolt\tests\helpers; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Bolt\helpers\FileCache; |
| 7 | + |
| 8 | +class FileCacheTest extends TestCase |
| 9 | +{ |
| 10 | + private FileCache $cache; |
| 11 | + |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + $this->cache = new FileCache(); |
| 15 | + } |
| 16 | + |
| 17 | + public function testConstruct(): void |
| 18 | + { |
| 19 | + $this->assertDirectoryExists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-filecache'); |
| 20 | + $this->assertDirectoryExists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-filecache' . DIRECTORY_SEPARATOR . '.ttl'); |
| 21 | + } |
| 22 | + |
| 23 | + public function testGetAndSet(): void |
| 24 | + { |
| 25 | + $key = uniqid('key_', true); |
| 26 | + $value = uniqid('value_', true); |
| 27 | + $this->assertTrue($this->cache->set($key, $value)); |
| 28 | + $this->assertEquals($value, $this->cache->get($key)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testDelete(): void |
| 32 | + { |
| 33 | + $key = uniqid('key_', true); |
| 34 | + $value = uniqid('value_', true); |
| 35 | + $this->assertTrue($this->cache->set($key, $value)); |
| 36 | + $this->assertTrue($this->cache->delete($key)); |
| 37 | + $this->assertNull($this->cache->get($key)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testClear(): void |
| 41 | + { |
| 42 | + $this->assertTrue($this->cache->set(uniqid('key_', true), uniqid('value_', true))); |
| 43 | + $this->assertTrue($this->cache->set(uniqid('key_', true), uniqid('value_', true))); |
| 44 | + $this->assertTrue($this->cache->clear()); |
| 45 | + $this->assertNull($this->cache->get(uniqid('key_', true))); |
| 46 | + $this->assertNull($this->cache->get(uniqid('key_', true))); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetMultiple(): void |
| 50 | + { |
| 51 | + $key1 = uniqid('key1_', true); |
| 52 | + $key2 = uniqid('key2_', true); |
| 53 | + $value1 = uniqid('value1_', true); |
| 54 | + $value2 = uniqid('value2_', true); |
| 55 | + $this->assertTrue($this->cache->set($key1, $value1)); |
| 56 | + $this->assertTrue($this->cache->set($key2, $value2)); |
| 57 | + $result = iterator_to_array($this->cache->getMultiple([$key1, $key2])); |
| 58 | + $this->assertEquals([$value1, $value2], $result); |
| 59 | + } |
| 60 | + |
| 61 | + public function testSetMultiple(): void |
| 62 | + { |
| 63 | + $values = [ |
| 64 | + uniqid('key1_', true) => uniqid('value1_', true), |
| 65 | + uniqid('key2_', true) => uniqid('value2_', true) |
| 66 | + ]; |
| 67 | + $this->assertTrue($this->cache->setMultiple($values)); |
| 68 | + foreach ($values as $key => $value) { |
| 69 | + $this->assertEquals($value, $this->cache->get($key)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function testDeleteMultiple(): void |
| 74 | + { |
| 75 | + $key1 = uniqid('key1_', true); |
| 76 | + $key2 = uniqid('key2_', true); |
| 77 | + $value1 = uniqid('value1_', true); |
| 78 | + $value2 = uniqid('value2_', true); |
| 79 | + $this->assertTrue($this->cache->set($key1, $value1)); |
| 80 | + $this->assertTrue($this->cache->set($key2, $value2)); |
| 81 | + $this->assertTrue($this->cache->deleteMultiple([$key1, $key2])); |
| 82 | + $this->assertNull($this->cache->get($key1)); |
| 83 | + $this->assertNull($this->cache->get($key2)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testHas(): void |
| 87 | + { |
| 88 | + $key = uniqid('key_', true); |
| 89 | + $value = uniqid('value_', true); |
| 90 | + $this->assertTrue($this->cache->set($key, $value)); |
| 91 | + $this->assertTrue($this->cache->has($key)); |
| 92 | + $this->assertTrue($this->cache->delete($key)); |
| 93 | + $this->assertFalse($this->cache->has($key)); |
| 94 | + } |
| 95 | + |
| 96 | + public function testLockAndUnlock(): void |
| 97 | + { |
| 98 | + $key = uniqid('key_', true); |
| 99 | + $value = uniqid('value_', true); |
| 100 | + $this->assertTrue($this->cache->set($key, $value)); |
| 101 | + $this->assertTrue($this->cache->lock($key)); |
| 102 | + $this->cache->unlock($key); |
| 103 | + |
| 104 | + $reflection = new \ReflectionClass($this->cache); |
| 105 | + $property = $reflection->getProperty('handles'); |
| 106 | + $property->setAccessible(true); |
| 107 | + $handles = $property->getValue($this->cache); |
| 108 | + $this->assertFalse(array_key_exists($key, $handles)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testLockingMechanism(): void |
| 112 | + { |
| 113 | + $key = 'test_lock_key'; |
| 114 | + $this->assertTrue($this->cache->delete($key)); |
| 115 | + |
| 116 | + $t = microtime(true); |
| 117 | + // run another script in background |
| 118 | + exec('php ' . __DIR__ . '/lock.php > /dev/null 2>&1 &'); |
| 119 | + |
| 120 | + if ($this->cache->lock($key)) { |
| 121 | + $this->assertGreaterThan(3.0, microtime(true) - $t); |
| 122 | + $this->assertEquals(123, $this->cache->get($key)); |
| 123 | + $this->cache->unlock($key); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public function testShutdown(): void |
| 128 | + { |
| 129 | + $key = 'test_lock_key'; |
| 130 | + $this->assertTrue($this->cache->delete($key)); |
| 131 | + |
| 132 | + $descriptorspec = array( |
| 133 | + 0 => array("pipe", "r"), |
| 134 | + 1 => array("pipe", "w"), |
| 135 | + ); |
| 136 | + |
| 137 | + $t = microtime(true); |
| 138 | + // Run another script in background |
| 139 | + $proc = proc_open('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'lock.php', $descriptorspec, $pipes); |
| 140 | + $pid = proc_get_status($proc)['pid']; |
| 141 | + sleep(1); |
| 142 | + |
| 143 | + // Terminate the process |
| 144 | + if (strncasecmp(PHP_OS, 'WIN', 3) === 0) { |
| 145 | + exec("taskkill /F /PID $pid /T"); |
| 146 | + } else { |
| 147 | + exec("kill -9 $pid"); |
| 148 | + } |
| 149 | + |
| 150 | + proc_close($proc); |
| 151 | + |
| 152 | + $this->assertLessThan(3.0, microtime(true) - $t); |
| 153 | + $this->assertTrue($this->cache->has($key)); |
| 154 | + $this->assertNull($this->cache->get($key)); |
| 155 | + $this->assertTrue($this->cache->lock($key)); |
| 156 | + $this->cache->unlock($key); |
| 157 | + } |
| 158 | + |
| 159 | + public function testInvalidKey(): void |
| 160 | + { |
| 161 | + $this->expectException(\Psr\SimpleCache\InvalidArgumentException::class); |
| 162 | + $this->expectExceptionMessage('Invalid cache key: invalid key!. Allowed characters are A-Za-z0-9_.'); |
| 163 | + $this->cache->set('invalid key!', 'value'); |
| 164 | + } |
| 165 | +} |
0 commit comments