1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12use Twig\Node\Expression\ConstantExpression;
13use Twig\Node\Expression\NameExpression;
14use Twig\Node\IfNode;
15use Twig\Node\Node;
16use Twig\Node\PrintNode;
17use Twig\Test\NodeTestCase;
18
19class Twig_Tests_Node_IfTest extends NodeTestCase
20{
21    public function testConstructor()
22    {
23        $t = new Node([
24            new ConstantExpression(true, 1),
25            new PrintNode(new NameExpression('foo', 1), 1),
26        ], [], 1);
27        $else = null;
28        $node = new IfNode($t, $else, 1);
29
30        $this->assertEquals($t, $node->getNode('tests'));
31        $this->assertFalse($node->hasNode('else'));
32
33        $else = new PrintNode(new NameExpression('bar', 1), 1);
34        $node = new IfNode($t, $else, 1);
35        $this->assertEquals($else, $node->getNode('else'));
36    }
37
38    public function getTests()
39    {
40        $tests = [];
41
42        $t = new Node([
43            new ConstantExpression(true, 1),
44            new PrintNode(new NameExpression('foo', 1), 1),
45        ], [], 1);
46        $else = null;
47        $node = new IfNode($t, $else, 1);
48
49        $tests[] = [$node, <<<EOF
50// line 1
51if (true) {
52    echo {$this->getVariableGetter('foo')};
53}
54EOF
55        ];
56
57        $t = new Node([
58            new ConstantExpression(true, 1),
59            new PrintNode(new NameExpression('foo', 1), 1),
60            new ConstantExpression(false, 1),
61            new PrintNode(new NameExpression('bar', 1), 1),
62        ], [], 1);
63        $else = null;
64        $node = new IfNode($t, $else, 1);
65
66        $tests[] = [$node, <<<EOF
67// line 1
68if (true) {
69    echo {$this->getVariableGetter('foo')};
70} elseif (false) {
71    echo {$this->getVariableGetter('bar')};
72}
73EOF
74        ];
75
76        $t = new Node([
77            new ConstantExpression(true, 1),
78            new PrintNode(new NameExpression('foo', 1), 1),
79        ], [], 1);
80        $else = new PrintNode(new NameExpression('bar', 1), 1);
81        $node = new IfNode($t, $else, 1);
82
83        $tests[] = [$node, <<<EOF
84// line 1
85if (true) {
86    echo {$this->getVariableGetter('foo')};
87} else {
88    echo {$this->getVariableGetter('bar')};
89}
90EOF
91        ];
92
93        return $tests;
94    }
95}
96