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 Twig\Environment;
13use Twig\Node\Expression\ConstantExpression;
14use Twig\Node\Expression\FunctionExpression;
15use Twig\Node\Node;
16use Twig\Test\NodeTestCase;
17use Twig\TwigFunction;
18
19class Twig_Tests_Node_Expression_FunctionTest extends NodeTestCase
20{
21    public function testConstructor()
22    {
23        $name = 'function';
24        $args = new Node();
25        $node = new FunctionExpression($name, $args, 1);
26
27        $this->assertEquals($name, $node->getAttribute('name'));
28        $this->assertEquals($args, $node->getNode('arguments'));
29    }
30
31    public function getTests()
32    {
33        $environment = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock());
34        $environment->addFunction(new TwigFunction('foo', 'foo', []));
35        $environment->addFunction(new TwigFunction('bar', 'bar', ['needs_environment' => true]));
36        $environment->addFunction(new TwigFunction('foofoo', 'foofoo', ['needs_context' => true]));
37        $environment->addFunction(new TwigFunction('foobar', 'foobar', ['needs_environment' => true, 'needs_context' => true]));
38        $environment->addFunction(new TwigFunction('barbar', 'twig_tests_function_barbar', ['is_variadic' => true]));
39
40        $tests = [];
41
42        $node = $this->createFunction('foo');
43        $tests[] = [$node, 'foo()', $environment];
44
45        $node = $this->createFunction('foo', [new ConstantExpression('bar', 1), new ConstantExpression('foobar', 1)]);
46        $tests[] = [$node, 'foo("bar", "foobar")', $environment];
47
48        $node = $this->createFunction('bar');
49        $tests[] = [$node, 'bar($this->env)', $environment];
50
51        $node = $this->createFunction('bar', [new ConstantExpression('bar', 1)]);
52        $tests[] = [$node, 'bar($this->env, "bar")', $environment];
53
54        $node = $this->createFunction('foofoo');
55        $tests[] = [$node, 'foofoo($context)', $environment];
56
57        $node = $this->createFunction('foofoo', [new ConstantExpression('bar', 1)]);
58        $tests[] = [$node, 'foofoo($context, "bar")', $environment];
59
60        $node = $this->createFunction('foobar');
61        $tests[] = [$node, 'foobar($this->env, $context)', $environment];
62
63        $node = $this->createFunction('foobar', [new ConstantExpression('bar', 1)]);
64        $tests[] = [$node, 'foobar($this->env, $context, "bar")', $environment];
65
66        // named arguments
67        $node = $this->createFunction('date', [
68            'timezone' => new ConstantExpression('America/Chicago', 1),
69            'date' => new ConstantExpression(0, 1),
70        ]);
71        $tests[] = [$node, 'twig_date_converter($this->env, 0, "America/Chicago")'];
72
73        // arbitrary named arguments
74        $node = $this->createFunction('barbar');
75        $tests[] = [$node, 'twig_tests_function_barbar()', $environment];
76
77        $node = $this->createFunction('barbar', ['foo' => new ConstantExpression('bar', 1)]);
78        $tests[] = [$node, 'twig_tests_function_barbar(null, null, ["foo" => "bar"])', $environment];
79
80        $node = $this->createFunction('barbar', ['arg2' => new ConstantExpression('bar', 1)]);
81        $tests[] = [$node, 'twig_tests_function_barbar(null, "bar")', $environment];
82
83        $node = $this->createFunction('barbar', [
84            new ConstantExpression('1', 1),
85            new ConstantExpression('2', 1),
86            new ConstantExpression('3', 1),
87            'foo' => new ConstantExpression('bar', 1),
88        ]);
89        $tests[] = [$node, 'twig_tests_function_barbar("1", "2", [0 => "3", "foo" => "bar"])', $environment];
90
91        // function as an anonymous function
92        if (PHP_VERSION_ID >= 50300) {
93            $node = $this->createFunction('anonymous', [new ConstantExpression('foo', 1)]);
94            $tests[] = [$node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), ["foo"])'];
95        }
96
97        return $tests;
98    }
99
100    protected function createFunction($name, array $arguments = [])
101    {
102        return new FunctionExpression($name, new Node($arguments), 1);
103    }
104
105    protected function getEnvironment()
106    {
107        if (PHP_VERSION_ID >= 50300) {
108            return include 'PHP53/FunctionInclude.php';
109        }
110
111        return parent::getEnvironment();
112    }
113}
114
115function twig_tests_function_barbar($arg1 = null, $arg2 = null, array $args = [])
116{
117}
118