ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
FunctionTest.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  public function testConstructor()
15  {
16  $name = 'function';
17  $args = new Twig_Node();
18  $node = new Twig_Node_Expression_Function($name, $args, 1);
19 
20  $this->assertEquals($name, $node->getAttribute('name'));
21  $this->assertEquals($args, $node->getNode('arguments'));
22  }
23 
24  public function getTests()
25  {
26  $environment = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
27  $environment->addFunction(new Twig_SimpleFunction('foo', 'foo', array()));
28  $environment->addFunction(new Twig_SimpleFunction('bar', 'bar', array('needs_environment' => true)));
29  $environment->addFunction(new Twig_SimpleFunction('foofoo', 'foofoo', array('needs_context' => true)));
30  $environment->addFunction(new Twig_SimpleFunction('foobar', 'foobar', array('needs_environment' => true, 'needs_context' => true)));
31  $environment->addFunction(new Twig_SimpleFunction('barbar', 'twig_tests_function_barbar', array('is_variadic' => true)));
32 
33  $tests = array();
34 
35  $node = $this->createFunction('foo');
36  $tests[] = array($node, 'foo()', $environment);
37 
38  $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
39  $tests[] = array($node, 'foo("bar", "foobar")', $environment);
40 
41  $node = $this->createFunction('bar');
42  $tests[] = array($node, 'bar($this->env)', $environment);
43 
44  $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1)));
45  $tests[] = array($node, 'bar($this->env, "bar")', $environment);
46 
47  $node = $this->createFunction('foofoo');
48  $tests[] = array($node, 'foofoo($context)', $environment);
49 
50  $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1)));
51  $tests[] = array($node, 'foofoo($context, "bar")', $environment);
52 
53  $node = $this->createFunction('foobar');
54  $tests[] = array($node, 'foobar($this->env, $context)', $environment);
55 
56  $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1)));
57  $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
58 
59  // named arguments
60  $node = $this->createFunction('date', array(
61  'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
62  'date' => new Twig_Node_Expression_Constant(0, 1),
63  ));
64  $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")');
65 
66  // arbitrary named arguments
67  $node = $this->createFunction('barbar');
68  $tests[] = array($node, 'twig_tests_function_barbar()', $environment);
69 
70  $node = $this->createFunction('barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
71  $tests[] = array($node, 'twig_tests_function_barbar(null, null, array("foo" => "bar"))', $environment);
72 
73  $node = $this->createFunction('barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
74  $tests[] = array($node, 'twig_tests_function_barbar(null, "bar")', $environment);
75 
76  $node = $this->createFunction('barbar', array(
80  'foo' => new Twig_Node_Expression_Constant('bar', 1),
81  ));
82  $tests[] = array($node, 'twig_tests_function_barbar("1", "2", array(0 => "3", "foo" => "bar"))', $environment);
83 
84  // function as an anonymous function
85  if (PHP_VERSION_ID >= 50300) {
86  $node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
87  $tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))');
88  }
89 
90  return $tests;
91  }
92 
93  protected function createFunction($name, array $arguments = array())
94  {
95  return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 1);
96  }
97 
98  protected function getEnvironment()
99  {
100  if (PHP_VERSION_ID >= 50300) {
101  return include 'PHP53/FunctionInclude.php';
102  }
103 
104  return parent::getEnvironment();
105  }
106 }
107 
108 function twig_tests_function_barbar($arg1 = null, $arg2 = null, array $args = array())
109 {
110 }
Represents a template function.
Represents a node in the AST.
Definition: Node.php:18
if($format !==null) $name
Definition: metadata.php:146
Create styles array
The data for the language used.
Stores the Twig configuration.
Definition: Environment.php:17
twig_tests_function_barbar($arg1=null, $arg2=null, array $args=array())
createFunction($name, array $arguments=array())