Skip to content

Commit

Permalink
[TwigBridge] Remove usage of Node() instantiations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 27, 2024
1 parent 5b5f22b commit 29d16b0
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Twig\Node\Expression\NameExpression;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\Node\SetNode;
use Twig\NodeVisitor\NodeVisitorInterface;

Expand Down Expand Up @@ -53,7 +54,11 @@ public function enterNode(Node $node, Environment $env): Node
$name = new AssignNameExpression($var, $node->getTemplateLine());
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));

return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
if (class_exists(Nodes::class)) {
return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine());
} else {
return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
}
}
}

Expand Down
30 changes: 23 additions & 7 deletions src/Symfony/Bridge/Twig/Tests/Node/DumpNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\Nodes;

class DumpNodeTest extends TestCase
{
Expand Down Expand Up @@ -71,9 +72,16 @@ public function testIndented()

public function testOneVar()
{
$vars = new Node([
new NameExpression('foo', 7),
]);
if (class_exists(Nodes::class)) {
$vars = new Nodes([
new NameExpression('foo', 7),
]);
} else {
$vars = new Node([
new NameExpression('foo', 7),
]);
}

$node = new DumpNode('bar', $vars, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
Expand All @@ -94,10 +102,18 @@ public function testOneVar()

public function testMultiVars()
{
$vars = new Node([
new NameExpression('foo', 7),
new NameExpression('bar', 7),
]);
if (class_exists(Nodes::class)) {
$vars = new Nodes([
new NameExpression('foo', 7),
new NameExpression('bar', 7),
]);
} else {
$vars = new Node([
new NameExpression('foo', 7),
new NameExpression('bar', 7),
]);
}

$node = new DumpNode('bar', $vars, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\Nodes;

class FormThemeTest extends TestCase
{
Expand All @@ -31,10 +32,17 @@ class FormThemeTest extends TestCase
public function testConstructor()
{
$form = new NameExpression('form', 0);
$resources = new Node([
new ConstantExpression('tpl1', 0),
new ConstantExpression('tpl2', 0),
]);
if (class_exists(Nodes::class)) {
$resources = new Nodes([
new ConstantExpression('tpl1', 0),
new ConstantExpression('tpl2', 0),
]);
} else {
$resources = new Node([
new ConstantExpression('tpl1', 0),
new ConstantExpression('tpl2', 0),
]);
}

$node = new FormThemeNode($form, $resources, 0);

Expand Down
181 changes: 141 additions & 40 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\TwigFunction;

class SearchAndRenderBlockNodeTest extends TestCase
{
public function testCompileWidget()
{
$arguments = new Node([
new NameExpression('form', 0),
]);
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
Expand All @@ -52,13 +59,23 @@ public function testCompileWidget()

public function testCompileWidgetWithVariables()
{
$arguments = new Node([
new NameExpression('form', 0),
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
], 0),
]);
], 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
], 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
Expand All @@ -79,10 +96,17 @@ public function testCompileWidgetWithVariables()

public function testCompileLabelWithLabel()
{
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('my label', 0),
]);
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConstantExpression('my label', 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('my label', 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -103,10 +127,17 @@ public function testCompileLabelWithLabel()

public function testCompileLabelWithNullLabel()
{
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
]);
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -129,10 +160,17 @@ public function testCompileLabelWithNullLabel()

public function testCompileLabelWithEmptyStringLabel()
{
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('', 0),
]);
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConstantExpression('', 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('', 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -155,9 +193,15 @@ public function testCompileLabelWithEmptyStringLabel()

public function testCompileLabelWithDefaultLabel()
{
$arguments = new Node([
new NameExpression('form', 0),
]);
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -178,14 +222,25 @@ public function testCompileLabelWithDefaultLabel()

public function testCompileLabelWithAttributes()
{
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
], 0),
]);
], 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression(null, 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
], 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -209,16 +264,29 @@ public function testCompileLabelWithAttributes()

public function testCompileLabelWithLabelAndAttributes()
{
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('value in argument', 0),
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConstantExpression('value in argument', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
], 0),
]);
], 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConstantExpression('value in argument', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
], 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -239,8 +307,9 @@ public function testCompileLabelWithLabelAndAttributes()

public function testCompileLabelWithLabelThatEvaluatesToNull()
{
$arguments = new Node([
new NameExpression('form', 0),
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
Expand All @@ -249,8 +318,22 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
// else
new ConstantExpression(null, 0),
0
),
]);
),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
// then
new ConstantExpression(null, 0),
// else
new ConstantExpression(null, 0),
0
),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand All @@ -275,8 +358,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()

public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
{
$arguments = new Node([
new NameExpression('form', 0),
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
Expand All @@ -291,8 +375,25 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
], 0),
]);
], 0),
]);
} else {
$arguments = new Node([
new NameExpression('form', 0),
new ConditionalExpression(
new ConstantExpression(true, 0),
new ConstantExpression(null, 0),
new ConstantExpression(null, 0),
0
),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
], 0),
]);
}

if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
Expand Down
Loading

0 comments on commit 29d16b0

Please sign in to comment.