assertEquals(['U', null], $this->getArguments('date', 'date', ['format' => 'U', 'timestamp' => null])); } public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('Positional arguments cannot be used after named arguments for function "date" in "test.twig" at line 2.'); $this->getArguments('date', 'date', ['timestamp' => 123456, 'Y-m-d']); } public function testGetArgumentsWhenArgumentIsDefinedTwice(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('Argument "format" is defined twice for function "date" in "test.twig" at line 2.'); $this->getArguments('date', 'date', ['Y-m-d', 'format' => 'U']); } public function testGetArgumentsWithWrongNamedArgumentName(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('Unknown argument "unknown" for function "date(format, timestamp)".'); $this->getArguments('date', 'date', ['Y-m-d', 'timestamp' => null, 'unknown' => '']); } public function testGetArgumentsWithWrongNamedArgumentNames(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)".'); $this->getArguments('date', 'date', ['Y-m-d', 'timestamp' => null, 'unknown1' => '', 'unknown2' => '']); } public function testResolveArgumentsOnlyNecessaryArgumentsForCustomFunction(): void { $this->assertEquals(['arg1'], $this->getArguments('custom_function', [$this, 'customFunction'], ['arg1' => 'arg1'])); } public function testGetArgumentsForStaticMethod(): void { $this->assertEquals(['arg1'], $this->getArguments('custom_static_function', __CLASS__.'::customStaticFunction', ['arg1' => 'arg1'])); } #[DataProvider('getGetArgumentsConversionData')] public function testGetArgumentsConversion($arg1, $arg2): void { $this->assertEquals([null], $this->getArguments('custom', eval("return fn (\$$arg1) => '';"), [$arg1 => null])); $this->assertEquals([null], $this->getArguments('custom', eval("return fn (\$$arg2) => '';"), [$arg2 => null])); $this->assertEquals([null], $this->getArguments('custom', eval("return fn (\$$arg1) => '';"), [$arg2 => null])); $this->assertEquals([null], $this->getArguments('custom', eval("return fn (\$$arg2) => '';"), [$arg1 => null])); } public static function getGetArgumentsConversionData() { yield ['some_name', 'some_name']; yield ['someName', 'some_name']; yield ['no_svg', 'noSVG']; yield ['error_404', 'error404']; yield ['errCode_404', 'err_code_404']; yield ['errCode404', 'err_code_404']; yield ['aBc', 'a_b_c']; yield ['aBC', 'a_b_c']; } public function testGetArgumentsConversionForVariadics(): void { $this->assertEquals([ new ConstantExpression('a', 0), new ConstantExpression(12, 0), new VariadicExpression([ new ConstantExpression('some_text_variadic', 2), new ConstantExpression('a', 0), new ConstantExpression('someNumberVariadic', 2), new ConstantExpression(12, 0), ], 2), ], $this->getArguments('custom', eval("return fn (string \$someText, int \$some_number, ...\$args) => '';"), ['some_text' => 'a', 'someNumber' => 12, 'some_text_variadic' => 'a', 'someNumberVariadic' => 12], true)); } public function testGetArgumentsError(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('Value for argument "some_name" is required for function "custom_static_function" in "test.twig" at line 2.'); $this->getArguments('custom_static_function', [$this, 'customFunctionSnakeCamel'], ['someCity' => 'Paris']); } public function testResolveArgumentsWithMissingParameterForArbitraryArguments(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessage('The last parameter of "Twig\Tests\Util\CallableArgumentsExtractorTest::customFunctionWithArbitraryArguments" for function "foo" must be an array with default value, eg. "array $arg = []".'); $this->getArguments('foo', [$this, 'customFunctionWithArbitraryArguments'], [], true); } public function testGetArgumentsWithInvalidCallable(): void { $this->expectException(\LogicException::class); $this->expectExceptionMessage('Callback for function "foo" is not callable in the current scope.'); $this->getArguments('foo', '', [], true); } public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnFunction(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessageMatches('#^The last parameter of "Twig\\\\Tests\\\\Util\\\\custom_call_test_function" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#'); $this->getArguments('foo', 'Twig\Tests\Util\custom_call_test_function', [], true); } public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnObject(): void { $this->expectException(SyntaxError::class); $this->expectExceptionMessageMatches('#^The last parameter of "Twig\\\\Tests\\\\Util\\\\CallableTestClass\\:\\:__invoke" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#'); $this->getArguments('foo', new CallableTestClass(), [], true); } public static function customStaticFunction($arg1, $arg2 = 'default', $arg3 = []): void { } public function customFunction($arg1, $arg2 = 'default', $arg3 = []): void { } public function customFunctionSnakeCamel($someName, $some_city): void { } public function customFunctionWithArbitraryArguments(): void { } private function getArguments(string $name, $callable, array $args, bool $isVariadic = false): array { $function = new TwigFunction($name, $callable, ['is_variadic' => $isVariadic]); $node = new ExpressionCall($function, new EmptyNode(), 2); $node->setSourceContext(new Source('', 'test.twig')); foreach ($args as $name => $arg) { $args[$name] = new ConstantExpression($arg, 0); } $arguments = (new CallableArgumentsExtractor($node, $function))->extractArguments(new Nodes($args)); foreach ($arguments as $name => $argument) { $arguments[$name] = $isVariadic ? $argument : $argument->getAttribute('value'); } return $arguments; } } class ExpressionCall extends FunctionExpression { } class CallableTestClass { public function __invoke($required): void { } } function custom_call_test_function($required): void { }