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\Environment;
13use Twig\Node\DeprecatedNode;
14use Twig\Node\Expression\ConstantExpression;
15use Twig\Node\Expression\FunctionExpression;
16use Twig\Node\IfNode;
17use Twig\Node\Node;
18use Twig\Test\NodeTestCase;
19use Twig\TwigFunction;
20
21class Twig_Tests_Node_DeprecatedTest extends NodeTestCase
22{
23    public function testConstructor()
24    {
25        $expr = new ConstantExpression('foo', 1);
26        $node = new DeprecatedNode($expr, 1);
27
28        $this->assertEquals($expr, $node->getNode('expr'));
29    }
30
31    public function getTests()
32    {
33        $tests = [];
34
35        $expr = new ConstantExpression('This section is deprecated', 1);
36        $node = new DeprecatedNode($expr, 1, 'deprecated');
37        $node->setTemplateName('foo.twig');
38
39        $tests[] = [$node, <<<EOF
40// line 1
41@trigger_error("This section is deprecated"." (\"foo.twig\" at line 1).", E_USER_DEPRECATED);
42EOF
43        ];
44
45        $t = new Node([
46            new ConstantExpression(true, 1),
47            new DeprecatedNode($expr, 2, 'deprecated'),
48        ], [], 1);
49        $node = new IfNode($t, null, 1);
50        $node->setTemplateName('foo.twig');
51
52        $tests[] = [$node, <<<EOF
53// line 1
54if (true) {
55    // line 2
56    @trigger_error("This section is deprecated"." (\"foo.twig\" at line 2).", E_USER_DEPRECATED);
57}
58EOF
59        ];
60
61        $environment = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock());
62        $environment->addFunction(new TwigFunction('foo', 'foo', []));
63
64        $expr = new FunctionExpression('foo', new Node(), 1);
65        $node = new DeprecatedNode($expr, 1, 'deprecated');
66        $node->setTemplateName('foo.twig');
67
68        $compiler = $this->getCompiler($environment);
69        $varName = $compiler->getVarName();
70
71        $tests[] = [$node, <<<EOF
72// line 1
73\$$varName = foo();
74@trigger_error(\$$varName." (\"foo.twig\" at line 1).", E_USER_DEPRECATED);
75EOF
76        , $environment];
77
78        return $tests;
79    }
80}
81