mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 10:26:32 +00:00
f640320202
* 3.x: (26 commits) Remove the documentation comments compilation overhead Clarify source function trust requirements Throw on PCRE errors in the matches operator Document that reusing a non-rewindable iterator after destructuring is unsupported Release destructuring temporaries after assignment Deprecate prefixed macro definedness checks Fix duplicate macro deprecation wording Throw when list formatting fails Document that sequence destructuring consumes one value per pattern slot Fix the html_attr documentation about iterables in data attributes Warn about untrusted input with the default Tempest markdown converter Document that overriding MacroNode::compile() is not supported anymore Merge overlapping CHANGELOG entries for the destructuring fatal error fix Document that include_only keeps global variables available Remove lazy macro import resolution Honor date formatter prototype calendars Fix Stringable keys for ArrayAccess implementations Fix repeated object destructuring evaluation Restore void return type compatibility for extension points Reject destructuring patterns containing no variables ... # Conflicts: # CHANGELOG # doc/deprecated.rst # doc/filters/format_datetime.rst # extra/twig-extra-bundle/DependencyInjection/Compiler/MissingExtensionSuggestorPass.php # extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php # extra/twig-extra-bundle/TwigExtraBundle.php # src/MacroNamespace.php # src/Node/MacrosNode.php # src/Parser.php # src/Test/IntegrationTestCase.php # src/Test/NodeTestCase.php # tests/CallMacroTest.php # tests/ExpressionParserTest.php # tests/Fixtures/macros/duplicate_definition.legacy.test # tests/Node/MacrosTest.php # tests/ParserTest.php
85 lines
2.7 KiB
PHP
85 lines
2.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Twig.
|
|
*
|
|
* (c) Fabien Potencier
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Twig\Tests\Node;
|
|
|
|
use Twig\Environment;
|
|
use Twig\Loader\ArrayLoader;
|
|
use Twig\Node\BodyNode;
|
|
use Twig\Node\Expression\ArrayExpression;
|
|
use Twig\Node\Expression\ConstantExpression;
|
|
use Twig\Node\Expression\Variable\LocalVariable;
|
|
use Twig\Node\MacroNode;
|
|
use Twig\Node\MacrosNode;
|
|
use Twig\Node\TextNode;
|
|
use Twig\Test\NodeTestCase;
|
|
|
|
class MacrosTest extends NodeTestCase
|
|
{
|
|
public function testItRejectsNonMacroNodesAtConstruction(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Using "Twig\Node\TextNode" for the macro "foo" of "Twig\Node\MacrosNode" is not supported. You must pass a "Twig\Node\MacroNode" instance.');
|
|
|
|
new MacrosNode(['foo' => new TextNode('foo', 1)]);
|
|
}
|
|
|
|
public function testItRejectsReplacingAMacroWithANonMacroNode(): void
|
|
{
|
|
$macros = new MacrosNode(['foo' => self::createMacro()]);
|
|
|
|
$this->expectException(\LogicException::class);
|
|
$this->expectExceptionMessage('A "Twig\Node\MacrosNode" can only contain "Twig\Node\MacroNode" nodes; replacing the macro "foo" with a "Twig\Node\TextNode" node is not supported.');
|
|
|
|
$macros->setNode('foo', new TextNode('foo', 1));
|
|
}
|
|
|
|
private static function createMacro(): MacroNode
|
|
{
|
|
$arguments = new ArrayExpression([
|
|
new LocalVariable('foo', 1),
|
|
new ConstantExpression(null, 1),
|
|
], 1);
|
|
|
|
return new MacroNode('foo', new BodyNode([new TextNode('foo', 1)]), $arguments, 1);
|
|
}
|
|
|
|
public static function provideTests(): iterable
|
|
{
|
|
yield 'without macros, no method is compiled' => [new MacrosNode(), ''];
|
|
|
|
$macro = self::createMacro();
|
|
|
|
yield 'with macros, the registry method is compiled' => [new MacrosNode(['foo' => $macro]), <<<EOF
|
|
protected function loadDeclaredMacros(): array
|
|
{
|
|
return [
|
|
"foo" => new \\Twig\\TwigMacro("foo", function (\$foo = null): string|Markup {
|
|
// line 1
|
|
\$macros = \$this->macros;
|
|
\$context = [
|
|
"foo" => \$foo,
|
|
] + \$this->env->getGlobals();
|
|
|
|
\$blocks = [];
|
|
|
|
return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
|
|
yield "foo";
|
|
yield from [];
|
|
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
|
|
}, ["foo" => true], false),
|
|
];
|
|
}
|
|
EOF, new Environment(new ArrayLoader(), ['use_yield' => true]),
|
|
];
|
|
}
|
|
}
|