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\ConstantExpression;
14use Twig\Node\Expression\GetAttrExpression;
15use Twig\Node\Expression\NameExpression;
16use Twig\Template;
17use Twig\Test\NodeTestCase;
18
19class Twig_Tests_Node_Expression_GetAttrTest extends NodeTestCase
20{
21    public function testConstructor()
22    {
23        $expr = new NameExpression('foo', 1);
24        $attr = new ConstantExpression('bar', 1);
25        $args = new ArrayExpression([], 1);
26        $args->addElement(new NameExpression('foo', 1));
27        $args->addElement(new ConstantExpression('bar', 1));
28        $node = new GetAttrExpression($expr, $attr, $args, Template::ARRAY_CALL, 1);
29
30        $this->assertEquals($expr, $node->getNode('node'));
31        $this->assertEquals($attr, $node->getNode('attribute'));
32        $this->assertEquals($args, $node->getNode('arguments'));
33        $this->assertEquals(Template::ARRAY_CALL, $node->getAttribute('type'));
34    }
35
36    public function getTests()
37    {
38        $tests = [];
39
40        $expr = new NameExpression('foo', 1);
41        $attr = new ConstantExpression('bar', 1);
42        $args = new ArrayExpression([], 1);
43        $node = new GetAttrExpression($expr, $attr, $args, Template::ANY_CALL, 1);
44        $tests[] = [$node, sprintf('%s%s, "bar", [])', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))];
45
46        $node = new GetAttrExpression($expr, $attr, $args, Template::ARRAY_CALL, 1);
47        $tests[] = [$node, sprintf('%s%s, "bar", [], "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))];
48
49        $args = new ArrayExpression([], 1);
50        $args->addElement(new NameExpression('foo', 1));
51        $args->addElement(new ConstantExpression('bar', 1));
52        $node = new GetAttrExpression($expr, $attr, $args, Template::METHOD_CALL, 1);
53        $tests[] = [$node, sprintf('%s%s, "bar", [0 => %s, 1 => "bar"], "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo'))];
54
55        return $tests;
56    }
57}
58