Refactor some tests

This commit is contained in:
Fabien Potencier
2024-07-23 23:18:02 +02:00
parent a656293915
commit ebcd5034ee
3 changed files with 50 additions and 37 deletions
+41 -28
View File
@@ -23,6 +23,8 @@ use Twig\TwigFilter;
class FilterTest extends NodeTestCase
{
private $extension = null;
public function testConstructor()
{
$expr = new ConstantExpression('foo', 1);
@@ -35,35 +37,14 @@ class FilterTest extends NodeTestCase
$this->assertEquals($args, $node->getNode('arguments'));
}
protected function tearDown(): void
{
$this->extension = null;
}
public function getTests()
{
$environment = new Environment(new ArrayLoader());
$environment->addFilter(new TwigFilter('bar', 'twig_tests_filter_dummy', ['needs_environment' => true]));
$environment->addFilter(new TwigFilter('bar_closure', \Closure::fromCallable(twig_tests_filter_dummy::class), ['needs_environment' => true]));
$environment->addFilter(new TwigFilter('barbar', 'Twig\Tests\Node\Expression\twig_tests_filter_barbar', ['needs_context' => true, 'is_variadic' => true]));
$environment->addFilter(new TwigFilter('magic_static', __NAMESPACE__.'\ChildMagicCallStub::magicStaticCall'));
if (\PHP_VERSION_ID >= 80111) {
$environment->addExtension(new FilterTestExtension());
}
$extension = new class() extends AbstractExtension {
public function getFilters(): array
{
return [
new TwigFilter('foo', \Closure::fromCallable([$this, 'foo'])),
new TwigFilter('foobar', \Closure::fromCallable([$this, 'foobar'])),
];
}
public function foo()
{
}
protected function foobar()
{
}
};
$environment->addExtension($extension);
$environment = $this->getEnvironment();
$tests = [];
@@ -141,7 +122,7 @@ class FilterTest extends NodeTestCase
// from extension
$node = $this->createFilter($string, 'foo');
$tests[] = [$node, \sprintf('$this->extensions[\'%s\']->foo("abc")', \get_class($extension)), $environment];
$tests[] = [$node, \sprintf('$this->extensions[\'%s\']->foo("abc")', \get_class($this->getExtension())), $environment];
$node = $this->createFilter($string, 'foobar');
$tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];
@@ -192,9 +173,41 @@ class FilterTest extends NodeTestCase
{
$env = new Environment(new ArrayLoader());
$env->addFilter(new TwigFilter('anonymous', function () {}));
$env->addFilter(new TwigFilter('bar', 'twig_tests_filter_dummy', ['needs_environment' => true]));
$env->addFilter(new TwigFilter('bar_closure', \Closure::fromCallable(twig_tests_filter_dummy::class), ['needs_environment' => true]));
$env->addFilter(new TwigFilter('barbar', 'Twig\Tests\Node\Expression\twig_tests_filter_barbar', ['needs_context' => true, 'is_variadic' => true]));
$env->addFilter(new TwigFilter('magic_static', __NAMESPACE__.'\ChildMagicCallStub::magicStaticCall'));
if (\PHP_VERSION_ID >= 80111) {
$env->addExtension(new FilterTestExtension());
}
$env->addExtension($this->getExtension());
return $env;
}
private function getExtension()
{
if ($this->extension) {
return $this->extension;
}
return $this->extension = new class() extends AbstractExtension {
public function getFilters(): array
{
return [
new TwigFilter('foo', \Closure::fromCallable([$this, 'foo'])),
new TwigFilter('foobar', \Closure::fromCallable([$this, 'foobar'])),
];
}
public function foo()
{
}
protected function foobar()
{
}
};
}
}
function twig_tests_filter_dummy()
+7 -7
View File
@@ -33,13 +33,7 @@ class FunctionTest extends NodeTestCase
public function getTests()
{
$environment = new Environment(new ArrayLoader());
$environment->addFunction(new TwigFunction('foo', 'twig_tests_function_dummy', []));
$environment->addFunction(new TwigFunction('foo_closure', \Closure::fromCallable(twig_tests_function_dummy::class), []));
$environment->addFunction(new TwigFunction('bar', 'twig_tests_function_dummy', ['needs_environment' => true]));
$environment->addFunction(new TwigFunction('foofoo', 'twig_tests_function_dummy', ['needs_context' => true]));
$environment->addFunction(new TwigFunction('foobar', 'twig_tests_function_dummy', ['needs_environment' => true, 'needs_context' => true]));
$environment->addFunction(new TwigFunction('barbar', 'Twig\Tests\Node\Expression\twig_tests_function_barbar', ['is_variadic' => true]));
$environment = $this->getEnvironment();
$tests = [];
@@ -111,6 +105,12 @@ class FunctionTest extends NodeTestCase
{
$env = new Environment(new ArrayLoader());
$env->addFunction(new TwigFunction('anonymous', function () {}));
$env->addFunction(new TwigFunction('foo', 'twig_tests_function_dummy', []));
$env->addFunction(new TwigFunction('foo_closure', \Closure::fromCallable(twig_tests_function_dummy::class), []));
$env->addFunction(new TwigFunction('bar', 'twig_tests_function_dummy', ['needs_environment' => true]));
$env->addFunction(new TwigFunction('foofoo', 'twig_tests_function_dummy', ['needs_context' => true]));
$env->addFunction(new TwigFunction('foobar', 'twig_tests_function_dummy', ['needs_environment' => true, 'needs_context' => true]));
$env->addFunction(new TwigFunction('barbar', 'Twig\Tests\Node\Expression\twig_tests_function_barbar', ['is_variadic' => true]));
return $env;
}
+2 -2
View File
@@ -36,8 +36,7 @@ class TestTest extends NodeTestCase
public function getTests()
{
$environment = new Environment(new ArrayLoader());
$environment->addTest(new TwigTest('barbar', 'Twig\Tests\Node\Expression\twig_tests_test_barbar', ['is_variadic' => true, 'need_context' => true]));
$environment = $this->getEnvironment();
$tests = [];
@@ -80,6 +79,7 @@ class TestTest extends NodeTestCase
{
$env = new Environment(new ArrayLoader());
$env->addTest(new TwigTest('anonymous', function () {}));
$env->addTest(new TwigTest('barbar', 'Twig\Tests\Node\Expression\twig_tests_test_barbar', ['is_variadic' => true, 'need_context' => true]));
return $env;
}