templatesDir = __DIR__ . '/fixtures'; $this->cacheDir = __DIR__ . '/../.scape/cache_test'; if (!is_dir($this->cacheDir)) { mkdir($this->cacheDir, 0777, true); } } protected function tearDown(): void { $this->rmdirRecursive($this->cacheDir); } private function rmdirRecursive(string $dir): void { if (!is_dir($dir)) return; $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . '/' . $file; is_dir($path) ? $this->rmdirRecursive($path) : unlink($path); } rmdir($dir); } public function testCreatesCacheFile(): void { $engine = new Engine([ 'templates_dir' => $this->templatesDir, 'cache_dir' => $this->cacheDir, 'mode' => 'production' ]); $templatePath = $this->templatesDir . '/tests/simple.scape.php'; $cacheKey = md5($templatePath); $cacheFile = $this->cacheDir . '/' . $cacheKey . '.ast'; $this->assertFileDoesNotExist($cacheFile); $engine->render('tests.simple', ['name' => 'Funky', 'items' => []]); $this->assertFileExists($cacheFile); } public function testUsesCacheInProduction(): void { $engine = new Engine([ 'templates_dir' => $this->templatesDir, 'cache_dir' => $this->cacheDir, 'mode' => 'production' ]); $engine->render('tests.simple', ['name' => 'First', 'items' => []]); // Modify template file but production should still use old cache $templatePath = $this->templatesDir . '/tests/simple.scape.php'; $originalContent = file_get_contents($templatePath); file_put_contents($templatePath, 'Modified Content'); $output = $engine->render('tests.simple', ['name' => 'Second', 'items' => []]); $this->assertStringContainsString('Hello Second!', $output); // simple.scape.php has "Hello {{ name }}!" // Restore file_put_contents($templatePath, $originalContent); } public function testInvalidatesCacheInDebug(): void { $engine = new Engine([ 'templates_dir' => $this->templatesDir, 'cache_dir' => $this->cacheDir, 'mode' => 'debug' ]); $templatePath = $this->templatesDir . '/tests/cache_debug.scape.php'; file_put_contents($templatePath, 'Original'); $engine->render('tests.cache_debug'); // Sleep to ensure mtime difference sleep(1); file_put_contents($templatePath, 'Modified'); $output = $engine->render('tests.cache_debug'); $this->assertEquals('Modified', $output); unlink($templatePath); } }