ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
13 
14 abstract 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 
73 class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
74 class_exists('Twig_Environment');
75 class_exists('Twig_Node');
Represents a node in the AST.
Definition: Node.php:18
assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment=null, $isPattern=false)
testCompile($node, $source, $environment=null, $isPattern=false)
getTests
if($format !==null) $name
Definition: metadata.php:146
Create styles array
The data for the language used.
getVariableGetter($name, $line=false)
$source
Definition: linkback.php:22
Stores the Twig configuration.
Definition: Environment.php:17
Loads a template from an array.
Definition: Array.php:26
getCompiler(Twig_Environment $environment=null)