4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 use PHPUnit\Framework\TestCase;
14 abstract class Twig_Test_NodeTestCase extends TestCase
16 abstract public function getTests();
19 * @dataProvider getTests
21 public function testCompile($node, $source, $environment = null, $isPattern = false)
23 $this->assertNodeCompilation($source, $node, $environment, $isPattern);
26 public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
28 $compiler = $this->getCompiler($environment);
29 $compiler->compile($node);
32 $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
34 $this->assertEquals($source, trim($compiler->getSource()));
38 protected function getCompiler(Twig_Environment $environment = null)
40 return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
43 protected function getEnvironment()
45 return new Twig_Environment(new Twig_Loader_Array(array()));
48 protected function getVariableGetter($name, $line = false)
50 $line = $line > 0 ? "// line {$line}\n" : '';
52 if (PHP_VERSION_ID >= 70000) {
53 return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
56 if (PHP_VERSION_ID >= 50400) {
57 return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
60 return sprintf('%s$this->getContext($context, "%s")', $line, $name);
63 protected function getAttributeGetter()
65 if (function_exists('twig_template_get_attributes')) {
66 return 'twig_template_get_attributes($this, ';
69 return '$this->getAttribute(';
73 class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
74 class_exists('Twig_Environment');
75 class_exists('Twig_Node');