4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_Node_Expression_CallTest extends \PHPUnit\Framework\TestCase
14 public function testGetArguments()
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)));
21 * @expectedException Twig_Error_Syntax
22 * @expectedExceptionMessage Positional arguments cannot be used after named arguments for function "date".
24 public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments()
26 $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
27 $node->getArguments('date', array('timestamp' => 123456, 'Y-m-d'));
31 * @expectedException Twig_Error_Syntax
32 * @expectedExceptionMessage Argument "format" is defined twice for function "date".
34 public function testGetArgumentsWhenArgumentIsDefinedTwice()
36 $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
37 $node->getArguments('date', array('Y-m-d', 'format' => 'U'));
41 * @expectedException Twig_Error_Syntax
42 * @expectedExceptionMessage Unknown argument "unknown" for function "date(format, timestamp)".
44 public function testGetArgumentsWithWrongNamedArgumentName()
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' => ''));
51 * @expectedException Twig_Error_Syntax
52 * @expectedExceptionMessage Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)".
54 public function testGetArgumentsWithWrongNamedArgumentNames()
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' => ''));
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".
64 public function testResolveArgumentsWithMissingValueForOptionalArgument()
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));
70 public function testResolveArgumentsOnlyNecessaryArgumentsForCustomFunction()
72 $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'custom_function'));
74 $this->assertEquals(array('arg1'), $node->getArguments(array($this, 'customFunction'), array('arg1' => 'arg1')));
77 public function testGetArgumentsForStaticMethod()
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')));
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()".
87 public function testResolveArgumentsWithMissingParameterForArbitraryArguments()
89 $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'foo', 'is_variadic' => true));
90 $node->getArguments(array($this, 'customFunctionWithArbitraryArguments'), array());
93 public static function customStaticFunction($arg1, $arg2 = 'default', $arg3 = array())
97 public function customFunction($arg1, $arg2 = 'default', $arg3 = array())
101 public function customFunctionWithArbitraryArguments()
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\(\)"\.$#
109 public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnFunction()
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());
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\(\)"\.$#
119 public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnObject()
121 $node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'foo', 'is_variadic' => true));
122 $node->getArguments(new CallableTestClass(), array());
126 class Twig_Tests_Node_Expression_Call extends Twig_Node_Expression_Call
128 public function getArguments($callable, $arguments)
130 return parent::getArguments($callable, $arguments);
134 class CallableTestClass
136 public function __invoke($required)
141 function custom_Twig_Tests_Node_Expression_CallTest_function($required)