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.
11 abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
13 abstract public function getTests();
16 * @dataProvider getTests
18 public function testCompile($node, $source, $environment = null, $isPattern = false)
20 $this->assertNodeCompilation($source, $node, $environment, $isPattern);
23 public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
25 $compiler = $this->getCompiler($environment);
26 $compiler->compile($node);
29 $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
31 $this->assertEquals($source, trim($compiler->getSource()));
35 protected function getCompiler(Twig_Environment $environment = null)
37 return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
40 protected function getEnvironment()
42 return new Twig_Environment(new Twig_Loader_Array(array()));
45 protected function getVariableGetter($name, $line = false)
47 $line = $line > 0 ? "// line {$line}\n" : '';
49 if (PHP_VERSION_ID >= 70000) {
50 return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
53 if (PHP_VERSION_ID >= 50400) {
54 return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
57 return sprintf('%s$this->getContext($context, "%s")', $line, $name);
60 protected function getAttributeGetter()
62 if (function_exists('twig_template_get_attributes')) {
63 return 'twig_template_get_attributes($this, ';
66 return '$this->getAttribute(';