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\ArrayExpression;
13use Twig\Node\Expression\ConditionalExpression;
14use Twig\Node\Expression\ConstantExpression;
15use Twig\Node\IncludeNode;
16use Twig\Test\NodeTestCase;
17
18class Twig_Tests_Node_IncludeTest extends NodeTestCase
19{
20    public function testConstructor()
21    {
22        $expr = new ConstantExpression('foo.twig', 1);
23        $node = new IncludeNode($expr, null, false, false, 1);
24
25        $this->assertFalse($node->hasNode('variables'));
26        $this->assertEquals($expr, $node->getNode('expr'));
27        $this->assertFalse($node->getAttribute('only'));
28
29        $vars = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
30        $node = new IncludeNode($expr, $vars, true, false, 1);
31        $this->assertEquals($vars, $node->getNode('variables'));
32        $this->assertTrue($node->getAttribute('only'));
33    }
34
35    public function getTests()
36    {
37        $tests = [];
38
39        $expr = new ConstantExpression('foo.twig', 1);
40        $node = new IncludeNode($expr, null, false, false, 1);
41        $tests[] = [$node, <<<EOF
42// line 1
43\$this->loadTemplate("foo.twig", null, 1)->display(\$context);
44EOF
45        ];
46
47        $expr = new ConditionalExpression(
48                        new ConstantExpression(true, 1),
49                        new ConstantExpression('foo', 1),
50                        new ConstantExpression('foo', 1),
51                        0
52                    );
53        $node = new IncludeNode($expr, null, false, false, 1);
54        $tests[] = [$node, <<<EOF
55// line 1
56\$this->loadTemplate(((true) ? ("foo") : ("foo")), null, 1)->display(\$context);
57EOF
58        ];
59
60        $expr = new ConstantExpression('foo.twig', 1);
61        $vars = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
62        $node = new IncludeNode($expr, $vars, false, false, 1);
63        $tests[] = [$node, <<<EOF
64// line 1
65\$this->loadTemplate("foo.twig", null, 1)->display(twig_array_merge(\$context, ["foo" => true]));
66EOF
67        ];
68
69        $node = new IncludeNode($expr, $vars, true, false, 1);
70        $tests[] = [$node, <<<EOF
71// line 1
72\$this->loadTemplate("foo.twig", null, 1)->display(twig_to_array(["foo" => true]));
73EOF
74        ];
75
76        $node = new IncludeNode($expr, $vars, true, true, 1);
77        $tests[] = [$node, <<<EOF
78// line 1
79try {
80    \$this->loadTemplate("foo.twig", null, 1)->display(twig_to_array(["foo" => true]));
81} catch (LoaderError \$e) {
82    // ignore missing template
83}
84EOF
85        ];
86
87        return $tests;
88    }
89}
90