ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NodeTestCase.php
Go to the documentation of this file.
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
12use PHPUnit\Framework\TestCase;
13
14abstract class Twig_Test_NodeTestCase extends TestCase
15{
16 abstract public function getTests();
17
21 public function testCompile($node, $source, $environment = null, $isPattern = false)
22 {
23 $this->assertNodeCompilation($source, $node, $environment, $isPattern);
24 }
25
26 public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
27 {
28 $compiler = $this->getCompiler($environment);
29 $compiler->compile($node);
30
31 if ($isPattern) {
32 $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
33 } else {
34 $this->assertEquals($source, trim($compiler->getSource()));
35 }
36 }
37
38 protected function getCompiler(Twig_Environment $environment = null)
39 {
40 return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
41 }
42
43 protected function getEnvironment()
44 {
45 return new Twig_Environment(new Twig_Loader_Array(array()));
46 }
47
48 protected function getVariableGetter($name, $line = false)
49 {
50 $line = $line > 0 ? "// line {$line}\n" : '';
51
52 if (PHP_VERSION_ID >= 70000) {
53 return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
54 }
55
56 if (PHP_VERSION_ID >= 50400) {
57 return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
58 }
59
60 return sprintf('%s$this->getContext($context, "%s")', $line, $name);
61 }
62
63 protected function getAttributeGetter()
64 {
65 if (function_exists('twig_template_get_attributes')) {
66 return 'twig_template_get_attributes($this, ';
67 }
68
69 return '$this->getAttribute(';
70 }
71}
72
73class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
74class_exists('Twig_Environment');
75class_exists('Twig_Node');
$source
Definition: linkback.php:22
An exception for terminatinating execution or to throw for unit testing.
Compiles a node to PHP code.
Definition: Compiler.php:19
Stores the Twig configuration.
Definition: Environment.php:18
Loads a template from an array.
Definition: Array.php:27
Represents a node in the AST.
Definition: Node.php:19
getCompiler(Twig_Environment $environment=null)
assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment=null, $isPattern=false)
getVariableGetter($name, $line=false)
testCompile($node, $source, $environment=null, $isPattern=false)
@dataProvider getTests