a6b550cf8cdd6568de416ca69aaf855596e00495
[yaffs-website] / vendor / twig / twig / lib / Twig / Test / NodeTestCase.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
12 {
13     abstract public function getTests();
14
15     /**
16      * @dataProvider getTests
17      */
18     public function testCompile($node, $source, $environment = null, $isPattern = false)
19     {
20         $this->assertNodeCompilation($source, $node, $environment, $isPattern);
21     }
22
23     public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
24     {
25         $compiler = $this->getCompiler($environment);
26         $compiler->compile($node);
27
28         if ($isPattern) {
29             $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
30         } else {
31             $this->assertEquals($source, trim($compiler->getSource()));
32         }
33     }
34
35     protected function getCompiler(Twig_Environment $environment = null)
36     {
37         return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
38     }
39
40     protected function getEnvironment()
41     {
42         return new Twig_Environment(new Twig_Loader_Array(array()));
43     }
44
45     protected function getVariableGetter($name, $line = false)
46     {
47         $line = $line > 0 ? "// line {$line}\n" : '';
48
49         if (PHP_VERSION_ID >= 70000) {
50             return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
51         }
52
53         if (PHP_VERSION_ID >= 50400) {
54             return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
55         }
56
57         return sprintf('%s$this->getContext($context, "%s")', $line, $name);
58     }
59
60     protected function getAttributeGetter()
61     {
62         if (function_exists('twig_template_get_attributes')) {
63             return 'twig_template_get_attributes($this, ';
64         }
65
66         return '$this->getAttribute(';
67     }
68 }