dir = sys_get_temp_dir() . '/weerscherm-cache-' . uniqid(); } protected function tearDown(): void { array_map('unlink', glob($this->dir . '/*') ?: []); @rmdir($this->dir); } public function testWriteThenReadRoundTrip(): void { $cache = new Cache($this->dir); $cache->write('stations', ['a' => 1.5, 'b' => null], 1000); self::assertSame(['data' => ['a' => 1.5, 'b' => null], 'stored_at' => 1000], $cache->read('stations')); } public function testMissingKeyReadsNull(): void { self::assertNull((new Cache($this->dir))->read('nope')); } public function testCorruptFileReadsNull(): void { mkdir($this->dir, 0700, true); file_put_contents($this->dir . '/broken.json', '{not json'); self::assertNull((new Cache($this->dir))->read('broken')); } public function testFreshness(): void { $cache = new Cache($this->dir); $cache->write('k', [], 1000); self::assertTrue($cache->isFresh('k', 300, 1299)); self::assertFalse($cache->isFresh('k', 300, 1300)); self::assertFalse($cache->isFresh('missing', 300, 1000)); } public function testKeyIsSanitised(): void { $cache = new Cache($this->dir); $cache->write('../Evil Key', ['x' => 1], 1); self::assertFileExists($this->dir . '/evil_key.json'); self::assertSame(['x' => 1], $cache->read('../Evil Key')['data']); } public function testWriteCreatesDirectory(): void { (new Cache($this->dir . '/nested'))->write('k', [], 1); self::assertDirectoryExists($this->dir . '/nested'); unlink($this->dir . '/nested/k.json'); rmdir($this->dir . '/nested'); } }