mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
Refactor some tests
This commit is contained in:
@@ -23,6 +23,8 @@ use Twig\TwigFilter;
|
|||||||
|
|
||||||
class FilterTest extends NodeTestCase
|
class FilterTest extends NodeTestCase
|
||||||
{
|
{
|
||||||
|
private $extension = null;
|
||||||
|
|
||||||
public function testConstructor()
|
public function testConstructor()
|
||||||
{
|
{
|
||||||
$expr = new ConstantExpression('foo', 1);
|
$expr = new ConstantExpression('foo', 1);
|
||||||
@@ -35,35 +37,14 @@ class FilterTest extends NodeTestCase
|
|||||||
$this->assertEquals($args, $node->getNode('arguments'));
|
$this->assertEquals($args, $node->getNode('arguments'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
$this->extension = null;
|
||||||
|
}
|
||||||
|
|
||||||
public function getTests()
|
public function getTests()
|
||||||
{
|
{
|
||||||
$environment = new Environment(new ArrayLoader());
|
$environment = $this->getEnvironment();
|
||||||
$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);
|
|
||||||
|
|
||||||
$tests = [];
|
$tests = [];
|
||||||
|
|
||||||
@@ -141,7 +122,7 @@ class FilterTest extends NodeTestCase
|
|||||||
|
|
||||||
// from extension
|
// from extension
|
||||||
$node = $this->createFilter($string, 'foo');
|
$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');
|
$node = $this->createFilter($string, 'foobar');
|
||||||
$tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];
|
$tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];
|
||||||
@@ -192,9 +173,41 @@ class FilterTest extends NodeTestCase
|
|||||||
{
|
{
|
||||||
$env = new Environment(new ArrayLoader());
|
$env = new Environment(new ArrayLoader());
|
||||||
$env->addFilter(new TwigFilter('anonymous', function () {}));
|
$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;
|
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()
|
function twig_tests_filter_dummy()
|
||||||
|
|||||||
@@ -33,13 +33,7 @@ class FunctionTest extends NodeTestCase
|
|||||||
|
|
||||||
public function getTests()
|
public function getTests()
|
||||||
{
|
{
|
||||||
$environment = new Environment(new ArrayLoader());
|
$environment = $this->getEnvironment();
|
||||||
$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]));
|
|
||||||
|
|
||||||
$tests = [];
|
$tests = [];
|
||||||
|
|
||||||
@@ -111,6 +105,12 @@ class FunctionTest extends NodeTestCase
|
|||||||
{
|
{
|
||||||
$env = new Environment(new ArrayLoader());
|
$env = new Environment(new ArrayLoader());
|
||||||
$env->addFunction(new TwigFunction('anonymous', function () {}));
|
$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;
|
return $env;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ class TestTest extends NodeTestCase
|
|||||||
|
|
||||||
public function getTests()
|
public function getTests()
|
||||||
{
|
{
|
||||||
$environment = new Environment(new ArrayLoader());
|
$environment = $this->getEnvironment();
|
||||||
$environment->addTest(new TwigTest('barbar', 'Twig\Tests\Node\Expression\twig_tests_test_barbar', ['is_variadic' => true, 'need_context' => true]));
|
|
||||||
|
|
||||||
$tests = [];
|
$tests = [];
|
||||||
|
|
||||||
@@ -80,6 +79,7 @@ class TestTest extends NodeTestCase
|
|||||||
{
|
{
|
||||||
$env = new Environment(new ArrayLoader());
|
$env = new Environment(new ArrayLoader());
|
||||||
$env->addTest(new TwigTest('anonymous', function () {}));
|
$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;
|
return $env;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user