1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Cache\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Contracts\Cache\CacheInterface;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Extra\Cache\CacheExtension;
use Twig\Extra\Cache\CacheRuntime;
use Twig\Loader\ArrayLoader;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
class FunctionalTest extends TestCase
{
public function testIsCached()
{
$cache = new ArrayAdapter();
$twig = $this->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;
}
}
|