This commit is contained in:
Fabien Potencier
2024-09-25 16:15:25 +02:00
parent dca5099aad
commit 00ba8046b0
+48
View File
@@ -22,6 +22,7 @@ use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\GlobalsInterface;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;
use Twig\Node\Node;
use Twig\NodeVisitor\NodeVisitorInterface;
@@ -497,6 +498,53 @@ EOF
$g3 = $twig->getGlobals();
$this->assertNotSame($g3['global_ext'], $g2['global_ext']);
}
public function testHotCache()
{
$dir = sys_get_temp_dir().'/twig-hot-cache-test';
if (is_dir($dir)) {
FilesystemHelper::removeDir($dir);
}
mkdir($dir);
file_put_contents($dir.'/index.twig', 'x');
try {
$twig = new Environment(new FilesystemLoader($dir), [
'debug' => false,
'auto_reload' => false,
'cache' => $dir.'/cache',
]);
// prime the cache
$this->assertSame('x', $twig->load('index.twig')->render([]));
// update the template
file_put_contents($dir.'/index.twig', 'y');
// re-render, should use the cached version
$this->assertSame('x', $twig->load('index.twig')->render([]));
// clear the cache
$twig->removeCache('index.twig');
// re-render, should use the updated template
$this->assertSame('y', $twig->load('index.twig')->render([]));
// the new template should not be cached
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir.'/cache', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
$count = 0;
foreach ($iterator as $fileInfo) {
if (!$fileInfo->isDir()) {
++$count;
}
}
$this->assertSame(0, $count);
// re-render, should use the updated template
$this->assertSame('y', $twig->load('index.twig')->render([]));
} finally {
FilesystemHelper::removeDir($dir);
}
}
}
class EnvironmentTest_Extension_WithGlobals extends AbstractExtension