diff options
Diffstat (limited to 'vendor/twig/twig/test/Twig/Tests/Node/Expression/FunctionTest.php')
-rw-r--r-- | vendor/twig/twig/test/Twig/Tests/Node/Expression/FunctionTest.php | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/vendor/twig/twig/test/Twig/Tests/Node/Expression/FunctionTest.php b/vendor/twig/twig/test/Twig/Tests/Node/Expression/FunctionTest.php new file mode 100644 index 00000000..431dc387 --- /dev/null +++ b/vendor/twig/twig/test/Twig/Tests/Node/Expression/FunctionTest.php | |||
@@ -0,0 +1,99 @@ | |||
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 | |||
12 | class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase | ||
13 | { | ||
14 | /** | ||
15 | * @covers Twig_Node_Expression_Function::__construct | ||
16 | */ | ||
17 | public function testConstructor() | ||
18 | { | ||
19 | $name = 'function'; | ||
20 | $args = new Twig_Node(); | ||
21 | $node = new Twig_Node_Expression_Function($name, $args, 1); | ||
22 | |||
23 | $this->assertEquals($name, $node->getAttribute('name')); | ||
24 | $this->assertEquals($args, $node->getNode('arguments')); | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * @covers Twig_Node_Expression_Function::compile | ||
29 | * @dataProvider getTests | ||
30 | */ | ||
31 | public function testCompile($node, $source, $environment = null) | ||
32 | { | ||
33 | parent::testCompile($node, $source, $environment); | ||
34 | } | ||
35 | |||
36 | public function getTests() | ||
37 | { | ||
38 | $environment = new Twig_Environment(); | ||
39 | $environment->addFunction('foo', new Twig_Function_Function('foo', array())); | ||
40 | $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true))); | ||
41 | $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true))); | ||
42 | $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true))); | ||
43 | |||
44 | $tests = array(); | ||
45 | |||
46 | $node = $this->createFunction('foo'); | ||
47 | $tests[] = array($node, 'foo()', $environment); | ||
48 | |||
49 | $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1))); | ||
50 | $tests[] = array($node, 'foo("bar", "foobar")', $environment); | ||
51 | |||
52 | $node = $this->createFunction('bar'); | ||
53 | $tests[] = array($node, 'bar($this->env)', $environment); | ||
54 | |||
55 | $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1))); | ||
56 | $tests[] = array($node, 'bar($this->env, "bar")', $environment); | ||
57 | |||
58 | $node = $this->createFunction('foofoo'); | ||
59 | $tests[] = array($node, 'foofoo($context)', $environment); | ||
60 | |||
61 | $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1))); | ||
62 | $tests[] = array($node, 'foofoo($context, "bar")', $environment); | ||
63 | |||
64 | $node = $this->createFunction('foobar'); | ||
65 | $tests[] = array($node, 'foobar($this->env, $context)', $environment); | ||
66 | |||
67 | $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1))); | ||
68 | $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment); | ||
69 | |||
70 | // named arguments | ||
71 | $node = $this->createFunction('date', array( | ||
72 | 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), | ||
73 | 'date' => new Twig_Node_Expression_Constant(0, 1), | ||
74 | )); | ||
75 | $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")'); | ||
76 | |||
77 | // function as an anonymous function | ||
78 | if (version_compare(phpversion(), '5.3.0', '>=')) { | ||
79 | $node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1))); | ||
80 | $tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))'); | ||
81 | } | ||
82 | |||
83 | return $tests; | ||
84 | } | ||
85 | |||
86 | protected function createFunction($name, array $arguments = array()) | ||
87 | { | ||
88 | return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 1); | ||
89 | } | ||
90 | |||
91 | protected function getEnvironment() | ||
92 | { | ||
93 | if (version_compare(phpversion(), '5.3.0', '>=')) { | ||
94 | return include 'PHP53/FunctionInclude.php'; | ||
95 | } | ||
96 | |||
97 | return parent::getEnvironment(); | ||
98 | } | ||
99 | } | ||