*/ private FileSystemCache $cache; protected function setUp(): void { parent::setUp(); $this->files = vfsStream::setup('cache-dir'); $this->cache = new FileSystemCache($this->files->url()); $this->injectFakeCache(); } public function test_cache_entries_are_handled_properly(): void { $classDefinition = FakeClassDefinition::new(); $functionDefinition = FakeFunctionDefinition::new(); self::assertFalse($this->cache->has('class-definition')); self::assertFalse($this->cache->has('function-definition')); self::assertTrue($this->cache->set('class-definition', $classDefinition)); self::assertTrue($this->cache->set('function-definition', $functionDefinition)); self::assertTrue($this->cache->has('class-definition')); self::assertTrue($this->cache->has('function-definition')); /** @var ClassDefinition $cachedClassDefinition */ $cachedClassDefinition = $this->cache->get('class-definition'); /** @var FunctionDefinition $cachedFunctionDefinition */ $cachedFunctionDefinition = $this->cache->get('function-definition'); self::assertSame($classDefinition->name(), $cachedClassDefinition->name()); self::assertSame($functionDefinition->signature(), $cachedFunctionDefinition->signature()); self::assertTrue($this->cache->delete('class-definition')); self::assertTrue($this->cache->delete('function-definition')); self::assertFalse($this->cache->has('class-definition')); self::assertFalse($this->cache->has('function-definition')); } public function test_clear_cache_clears_all_caches(): void { $classDefinition = FakeClassDefinition::new(); $functionDefinition = FakeFunctionDefinition::new(); $this->cache->set('class-definition', $classDefinition); $this->cache->set('function-definition', $functionDefinition); self::assertTrue($this->cache->has('class-definition')); self::assertTrue($this->cache->has('function-definition')); self::assertTrue($this->cache->clear()); self::assertFalse($this->cache->has('class-definition')); self::assertFalse($this->cache->has('function-definition')); } public function test_multiple_cache_entries_are_handled_properly(): void { $classDefinition = FakeClassDefinition::new(); $functionDefinition = FakeFunctionDefinition::new(); self::assertTrue($this->cache->setMultiple([ 'class-definition' => $classDefinition, 'function-definition' => $functionDefinition, ])); $cached = iterator_to_array($this->cache->getMultiple(['class-definition', 'function-definition'])); /** @var ClassDefinition $cachedClassDefinition */ $cachedClassDefinition = $cached['class-definition']; /** @var FunctionDefinition $cachedFunctionDefinition */ $cachedFunctionDefinition = $cached['function-definition']; self::assertSame($classDefinition->name(), $cachedClassDefinition->name()); self::assertSame($functionDefinition->signature(), $cachedFunctionDefinition->signature()); self::assertTrue($this->cache->deleteMultiple(['class-definition', 'function-definition'])); self::assertFalse($this->cache->has('class-definition')); self::assertFalse($this->cache->has('function-definition')); } public function test_methods_returns_false_if_delegates_fail(): void { $this->injectFakeCache(true); $classDefinition = FakeClassDefinition::new(); $functionDefinition = FakeFunctionDefinition::new(); self::assertTrue($this->cache->set('class-definition', $classDefinition)); self::assertFalse($this->cache->set('function-definition', $functionDefinition)); self::assertFalse($this->cache->delete('class-definition')); self::assertFalse($this->cache->delete('function-definition')); self::assertFalse($this->cache->clear()); self::assertFalse($this->cache->setMultiple([ 'class-definition' => $classDefinition, 'function-definition' => $functionDefinition, ])); self::assertFalse($this->cache->deleteMultiple(['class-definition', 'function-definition'])); } public function test_get_non_existing_entry_returns_default_value(): void { $defaultValue = FakeClassDefinition::new(); self::assertSame($defaultValue, $this->cache->get('non-existing-entry', $defaultValue)); } private function injectFakeCache(bool $withFailingCache = false): void { (function () use ($withFailingCache) { $this->delegates = [ '*' => new FakeCache(), ClassDefinition::class => new FakeCache(), FunctionDefinition::class => $withFailingCache ? new FakeFailingCache() : new FakeCache(), ]; })->call($this->cache); } }