883bb61273ccacda639eb2f5808d4f95d8f25e62
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / Node / Expression / CallTest.php
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_CallTest extends \PHPUnit\Framework\TestCase
13 {
14     public function testGetArguments()
15     {
16         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
17         $this->assertEquals(array('U', null), $node->getArguments('date', array('format' => 'U', 'timestamp' => null)));
18     }
19
20     /**
21      * @expectedException        Twig_Error_Syntax
22      * @expectedExceptionMessage Positional arguments cannot be used after named arguments for function "date".
23      */
24     public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments()
25     {
26         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
27         $node->getArguments('date', array('timestamp' => 123456, 'Y-m-d'));
28     }
29
30     /**
31      * @expectedException        Twig_Error_Syntax
32      * @expectedExceptionMessage Argument "format" is defined twice for function "date".
33      */
34     public function testGetArgumentsWhenArgumentIsDefinedTwice()
35     {
36         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
37         $node->getArguments('date', array('Y-m-d', 'format' => 'U'));
38     }
39
40     /**
41      * @expectedException        Twig_Error_Syntax
42      * @expectedExceptionMessage Unknown argument "unknown" for function "date(format, timestamp)".
43      */
44     public function testGetArgumentsWithWrongNamedArgumentName()
45     {
46         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
47         $node->getArguments('date', array('Y-m-d', 'timestamp' => null, 'unknown' => ''));
48     }
49
50     /**
51      * @expectedException        Twig_Error_Syntax
52      * @expectedExceptionMessage Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)".
53      */
54     public function testGetArgumentsWithWrongNamedArgumentNames()
55     {
56         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
57         $node->getArguments('date', array('Y-m-d', 'timestamp' => null, 'unknown1' => '', 'unknown2' => ''));
58     }
59
60     /**
61      * @expectedException        Twig_Error_Syntax
62      * @expectedExceptionMessage Argument "case_sensitivity" could not be assigned for function "substr_compare(main_str, str, offset, length, case_sensitivity)" because it is mapped to an internal PHP function which cannot determine default value for optional argument "length".
63      */
64     public function testResolveArgumentsWithMissingValueForOptionalArgument()
65     {
66         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'substr_compare'));
67         $node->getArguments('substr_compare', array('abcd', 'bc', 'offset' => 1, 'case_sensitivity' => true));
68     }
69
70     public function testResolveArgumentsOnlyNecessaryArgumentsForCustomFunction()
71     {
72         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'custom_function'));
73
74         $this->assertEquals(array('arg1'), $node->getArguments(array($this, 'customFunction'), array('arg1' => 'arg1')));
75     }
76
77     public function testGetArgumentsForStaticMethod()
78     {
79         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'custom_static_function'));
80         $this->assertEquals(array('arg1'), $node->getArguments(__CLASS__.'::customStaticFunction', array('arg1' => 'arg1')));
81     }
82
83     /**
84      * @expectedException        LogicException
85      * @expectedExceptionMessage The last parameter of "Twig_Tests_Node_Expression_CallTest::customFunctionWithArbitraryArguments" for function "foo" must be an array with default value, eg. "array $arg = array()".
86      */
87     public function testResolveArgumentsWithMissingParameterForArbitraryArguments()
88     {
89         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'foo', 'is_variadic' => true));
90         $node->getArguments(array($this, 'customFunctionWithArbitraryArguments'), array());
91     }
92
93     public static function customStaticFunction($arg1, $arg2 = 'default', $arg3 = array())
94     {
95     }
96
97     public function customFunction($arg1, $arg2 = 'default', $arg3 = array())
98     {
99     }
100
101     public function customFunctionWithArbitraryArguments()
102     {
103     }
104
105     /**
106      * @expectedException              LogicException
107      * @expectedExceptionMessageRegExp #^The last parameter of "custom_Twig_Tests_Node_Expression_CallTest_function" for function "foo" must be an array with default value, eg\. "array \$arg \= array\(\)"\.$#
108      */
109     public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnFunction()
110     {
111         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'foo', 'is_variadic' => true));
112         $node->getArguments('custom_Twig_Tests_Node_Expression_CallTest_function', array());
113     }
114
115     /**
116      * @expectedException              LogicException
117      * @expectedExceptionMessageRegExp #^The last parameter of "CallableTestClass\:\:__invoke" for function "foo" must be an array with default value, eg\. "array \$arg \= array\(\)"\.$#
118      */
119     public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnObject()
120     {
121         $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'foo', 'is_variadic' => true));
122         $node->getArguments(new CallableTestClass(), array());
123     }
124 }
125
126 class Twig_Tests_Node_Expression_Call extends Twig_Node_Expression_Call
127 {
128     public function getArguments($callable, $arguments)
129     {
130         return parent::getArguments($callable, $arguments);
131     }
132 }
133
134 class CallableTestClass
135 {
136     public function __invoke($required)
137     {
138     }
139 }
140
141 function custom_Twig_Tests_Node_Expression_CallTest_function($required)
142 {
143 }