GimmeNodeTest::testConstructor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Tests\Twig\Node;
16
17
use Doctrine\Common\Cache\ArrayCache;
18
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
19
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory;
20
use SWP\Component\TemplatesSystem\Gimme\Loader\ArticleLoader;
21
use SWP\Component\TemplatesSystem\Gimme\Loader\ChainLoader;
22
use SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension;
23
use SWP\Component\TemplatesSystem\Twig\Node\GimmeNode;
24
use Symfony\Component\EventDispatcher\EventDispatcher;
25
use Twig\Node\Expression\ArrayExpression;
26
use Twig\Test\NodeTestCase;
27
use Twig\Loader\ArrayLoader;
28
use Twig\Environment;
29
use Twig\Node\Node;
30
use Twig\Node\TextNode;
31
use Twig\Node\Expression\ConstantExpression;
32
use Twig\Node\Expression\AssignNameExpression;
33
34
class GimmeNodeTest extends NodeTestCase
35
{
36 View Code Duplication
    public function testConstructor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $annotation = new Node([new AssignNameExpression('article', 1)]);
39
        $parameters = new ArrayExpression([], 1);
40
        $body = new TextNode('', 1);
41
        $node = new GimmeNode($annotation, $parameters, null, $body, 1, 'gimme');
42
        $this->assertEquals($annotation, $node->getNode('annotation'));
43
        $this->assertEquals($parameters, $node->getNode('parameters'));
44
        $this->assertEquals($body, $node->getNode('body'));
45
    }
46
47
    public function getTests()
48
    {
49
        $annotation1 = new Node([new AssignNameExpression('article', 1)]);
50
        $parameters1 = new ArrayExpression([], 1);
51
        $body1 = new TextNode('Test body', 1);
52
        $node1 = new GimmeNode($annotation1, $parameters1, null, $body1, 1, 'gimme');
53
54
        $annotation2 = new Node([new AssignNameExpression('article', 2)]);
55
        $body2 = new TextNode('Test body', 2);
56
        $node2 = new GimmeNode($annotation2, null, null, $body2, 2, 'gimme');
57
58
        $annotation3 = new Node([new AssignNameExpression('article', 3)]);
59
        $parameters3 = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
60
        $body3 = new TextNode('Test body', 3);
61
        $node3 = new GimmeNode($annotation3, $parameters3, null, $body3, 3, 'gimme');
62
63
        $annotation4 = new Node([new AssignNameExpression('article', 3)]);
64
        $parameters4 = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
65
        $ignoreContext4 = new ArrayExpression([], 1);
66
        $body4 = new TextNode('Test body', 4);
67
        $node4 = new GimmeNode($annotation4, $parameters4, $ignoreContext4, $body4, 4, 'gimme');
68
69
        return [
70
            [$node1, <<<'EOF'
71
// line 1
72
$swpMetaLoader3 = $this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();
73
$context["article"] = $swpMetaLoader3->load("article", []);
74
if ($context["article"] !== false) {
75
    echo "Test body";
76
}
77
unset($context["article"]);
78
EOF
79
            ],
80
            [$node2, <<<'EOF'
81
// line 2
82
$swpMetaLoader4 = $this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();
83
$context["article"] = $swpMetaLoader4->load("article", null);
84
if ($context["article"] !== false) {
85
    echo "Test body";
86
}
87
unset($context["article"]);
88
EOF
89
            ],
90
            [$node3, <<<'EOF'
91
// line 3
92
$swpMetaLoader5 = $this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();
93
$context["article"] = $swpMetaLoader5->load("article", ["foo" => true]);
94
if ($context["article"] !== false) {
95
    echo "Test body";
96
}
97
unset($context["article"]);
98
EOF
99
            ],
100
            [$node4, <<<'EOF'
101
// line 4
102
$swpMetaLoader6 = $this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();
103
$swpContext6Gimme = $this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getContext();
104
$swpIgnoreContext6Gimme = $swpContext6Gimme->temporaryUnset([]);
105
$context["article"] = $swpMetaLoader6->load("article", ["foo" => true]);
106
if ($context["article"] !== false) {
107
    echo "Test body";
108
}
109
$swpContext6Gimme->restoreTemporaryUnset($swpIgnoreContext6Gimme);
110
unset($context["article"]);
111
EOF
112
            ],
113
        ];
114
    }
115
116
    public function testTemplateString()
117
    {
118
        $loader = new ArrayLoader([
119
            'clear_gimme' => '{% gimme article %}{{ article.title }}{% endgimme %}',
120
            'gimme_with_parameters' => '{% gimme article with {id: 1} %}{{ article.title }}{% endgimme %}',
121
        ]);
122
        $metaLoader = new ChainLoader();
123
        $context = new Context(new EventDispatcher(), new ArrayCache());
124
        $metaLoader->addLoader(new ArticleLoader(__DIR__, new MetaFactory($context)));
125
        $twig = new Environment($loader);
126
        $twig->addExtension(new GimmeExtension($context, $metaLoader));
127
128
        $this->assertEquals($twig->render('clear_gimme'), 'New article');
129
        $this->assertEquals($twig->render('gimme_with_parameters'), 'New article');
130
    }
131
132
    public function testBrokenTemplate()
133
    {
134
        $loader = new ArrayLoader([
135
            'error_gimme' => '{% gimme article {id: 1} %}{{ article.title }}{% endgimme %}',
136
        ]);
137
        $metaLoader = new ChainLoader();
138
        $context = new Context(new EventDispatcher(), new ArrayCache());
139
        $metaLoader->addLoader(new ArticleLoader(__DIR__, new MetaFactory($context)));
140
        $twig = new Environment($loader);
141
        $twig->addExtension(new GimmeExtension($context, $metaLoader));
142
143
        $this->expectException(\Twig\Error\SyntaxError::class);
144
        $twig->render('error_gimme');
145
    }
146
147 View Code Duplication
    protected function tearDown()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $reflection = new \ReflectionObject($this);
150
        foreach ($reflection->getProperties() as $prop) {
151
            if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
0 ignored issues
show
introduced by
Consider using $prop->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
152
                $prop->setAccessible(true);
153
                $prop->setValue($this, null);
154
            }
155
        }
156
    }
157
}
158