Files
Twig/tests/Node/Expression/MacroReferenceTest.php
Fabien Potencier f3bacf3877 Merge branch '3.x' into 4.x
* 3.x:
  Reuse the template wrapper of an already loaded template
  Stop the escaping safe analysis from retaining every analyzed node
  Allow to call TemplateWrapper::unwrap() without arguments
  Expose the escaping strategy a template was compiled with
  Improve macro call performance
  Fix IntlExtension inheriting values derived by ICU from a date formatter prototype
  Fix array access with a Stringable key on subclasses of ArrayObject and ArrayIterator
  Ignore the PHPUnit 10+ cache directory
  Cover and document the matches operator throwing on an invalid UTF-8 subject
  Report a clear error when a string cannot be split into characters
  Deprecate cloning a Twig environment
  Report the macro call parentheses deprecation once per call site and name the macro
  Bump version
  Prepare the 3.29.0 release
  Update CHANGELOG

# Conflicts:
#	.gitignore
#	CHANGELOG
#	doc/deprecated.rst
#	src/Environment.php
#	src/NodeVisitor/CorrectnessNodeVisitor.php
#	src/NodeVisitor/SafeAnalysisNodeVisitor.php
#	src/Template.php
#	tests/Fixtures/macros/call_without_parentheses.legacy.test
#	tests/ParserTest.php
#	tests/TemplateTest.php
#	tests/TwigMacroTest.php
2026-09-22 21:10:53 +01:00

50 lines
1.6 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\Expression;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\MacroReferenceExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Expression\Variable\MacroVariable;
class MacroReferenceTest extends TestCase
{
public function testConstructorAcceptsAnExpressionAsName(): void
{
$node = new MacroReferenceExpression(new MacroVariable('foo', 1), new ContextVariable('name', 1), new ArrayExpression([], 1), 1);
$this->assertTrue($node->hasNode('name'));
$this->assertNull($node->getAttribute('name'));
}
public function testDynamicNameResolvesMacroAtRuntime(): void
{
$env = new Environment(new ArrayLoader());
$compiler = new \Twig\Compiler($env);
$node = new MacroReferenceExpression(
new MacroVariable('mac', 1),
new ContextVariable('name', 1),
new ArrayExpression([], 1),
1,
);
$compiler->compile($node);
$this->assertStringContainsString('->call(', $compiler->getSource());
$this->assertStringContainsString('($context["name"] ?? null), [], $context, 1, $this->source)', $compiler->getSource());
$this->assertStringNotContainsString("'macro_'.", $compiler->getSource());
}
}