createEnvironment(['index' => '{% cache "city;v1" %}{{- city -}}{% endcache %}'], $cache); $this->assertSame('Paris', $twig->render('index', ['city' => 'Paris'])); $value = $cache->get('city;v1', function () { throw new \RuntimeException('Key should be in the cache'); }); $this->assertSame('Paris', $value); } public function testTtlNoArgs() { $twig = $this->createEnvironment(['index' => '{% cache "ttl_no_args" ttl() %}{% endcache %}']); $this->expectException(SyntaxError::class); $this->expectExceptionMessage('The "ttl" modifier takes exactly one argument (0 given) in "index" at line 1.'); $twig->render('index'); } public function testTtlTooManyArgs() { $twig = $this->createEnvironment(['index' => '{% cache "ttl_too_many_args" ttl(0, 1) %}{% endcache %}']); $this->expectException(SyntaxError::class); $this->expectExceptionMessage('The "ttl" modifier takes exactly one argument (2 given) in "index" at line 1.'); $twig->render('index'); } public function testTagsNoArgs() { $twig = $this->createEnvironment(['index' => '{% cache "tags_no_args" tags() %}{% endcache %}']); $this->expectException(SyntaxError::class); $this->expectExceptionMessage('The "tags" modifier takes exactly one argument (0 given) in "index" at line 1.'); $twig->render('index'); } public function testTagsTooManyArgs() { $twig = $this->createEnvironment(['index' => '{% cache "tags_too_many_args" tags(["foo"], 1) %}{% endcache %}']); $this->expectException(SyntaxError::class); $this->expectExceptionMessage('The "tags" modifier takes exactly one argument (2 given) in "index" at line 1.'); $twig->render('index'); } private function createEnvironment(array $templates, ?ArrayAdapter $cache = null): Environment { $twig = new Environment(new ArrayLoader($templates)); $cache = $cache ?? new ArrayAdapter(); $twig->addExtension(new CacheExtension()); $twig->addRuntimeLoader(new class($cache) implements RuntimeLoaderInterface { private $cache; public function __construct(CacheInterface $cache) { $this->cache = $cache; } public function load(string $class): ?object { return CacheRuntime::class === $class ? new CacheRuntime($this->cache) : null; } }); return $twig; } }