testDiscoverException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Twig\NodeVisitor;
12
13
use Twig\Node\Node;
14
use Twig\Node\PrintNode;
15
use Twig\Node\Expression\ArrayExpression;
16
use Twig\Node\Expression\ConstantExpression;
17
use Twig\Node\Expression\NameExpression;
18
use Twig\Error\SyntaxError;
19
use Twig\TwigFunction;
20
use Yarhon\RouteGuardBundle\Tests\Twig\AbstractNodeTest;
21
use Yarhon\RouteGuardBundle\Twig\TokenParser\RouteTokenParser;
22
use Yarhon\RouteGuardBundle\Twig\Node\RouteNode;
23
use Yarhon\RouteGuardBundle\Twig\Node\RouteExpression;
24
use Yarhon\RouteGuardBundle\Twig\NodeVisitor\DiscoverRoutingFunctionNodeVisitor;
25
26
class DiscoverRoutingFunctionNodeVisitorTest extends AbstractNodeTest
27
{
28
    private $tagName = 'routewithdiscover';
29
30
    private $tagVariableName = '_routewithdiscover';
31
32
    public function setUp()
33
    {
34
        parent::setUp();
35
36
        $this->environment->addFunction(new TwigFunction('url', function () {}));
37
        $this->environment->addFunction(new TwigFunction('path', function () {}));
38
39
        $nodeVisitor = new DiscoverRoutingFunctionNodeVisitor($this->tagName, $this->tagVariableName);
40
        $this->environment->addNodeVisitor($nodeVisitor);
41
42
        $this->environment->addTokenParser(new RouteTokenParser($this->tagName, true));
43
    }
44
45
    /**
46
     * @dataProvider discoverDataProvider
47
     */
48
    public function testDiscover($source, $expected)
49
    {
50
        $node = $this->parse($source);
51
52
        $this->assertEquals($expected, $node);
53
    }
54
55
    public function discoverDataProvider()
56
    {
57
        $nameExpression = new NameExpression($this->tagVariableName, 0);
58
        $nameExpression->setAttribute('always_defined', true);
59
60
        return [
61
            [
62
                // path function test
63
                '{% routewithdiscover discover %}{{ path("secure1") }}{% endroutewithdiscover %}',
64
                new RouteNode(
65
                    (new RouteExpression(
66
                        new Node([
67
                            new ConstantExpression('secure1', 0),
68
                        ])
69
                    ))->setGenerateAs('path'),
70
                    new PrintNode($nameExpression, 0)
0 ignored issues
show
Bug introduced by
new Twig\Node\PrintNode($nameExpression, 0) of type Twig\Node\PrintNode is incompatible with the type Twig\Node\Node expected by parameter $bodyNode of Yarhon\RouteGuardBundle\...outeNode::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                    /** @scrutinizer ignore-type */ new PrintNode($nameExpression, 0)
Loading history...
71
                ),
72
            ],
73
74
            [
75
                // url function test
76
                '{% routewithdiscover discover %}{{ url("secure1") }}{% endroutewithdiscover %}',
77
                new RouteNode(
78
                    (new RouteExpression(
79
                        new Node([
80
                            new ConstantExpression('secure1', 0),
81
                        ])
82
                    ))->setGenerateAs('url'),
83
                    new PrintNode($nameExpression, 0)
84
                ),
85
            ],
86
87
            [
88
                // relative parameter test
89
                '{% routewithdiscover discover %}{{ path("secure1", {}, true) }}{% endroutewithdiscover %}',
90
                new RouteNode(
91
                    (new RouteExpression(
92
                        new Node([
93
                            new ConstantExpression('secure1', 0),
94
                            new ArrayExpression([], 0),
95
                            new ConstantExpression('GET', 0),
96
                        ])
97
                    ))->setGenerateAs('path', true),
98
                    new PrintNode($nameExpression, 0)
99
                ),
100
            ],
101
        ];
102
    }
103
104
    /**
105
     * @dataProvider discoverExceptionDataProvider
106
     */
107
    public function testDiscoverException($source, $expected)
108
    {
109
        $this->expectException($expected[0]);
110
        if (isset($expected[1])) {
111
            $this->expectExceptionMessage($expected[1]);
112
        }
113
114
        $this->parse($source);
115
    }
116
117
    public function discoverExceptionDataProvider()
118
    {
119
        return [
120
            [
121
                // without any routing function
122
                '{% routewithdiscover discover %}test{% endroutewithdiscover %}',
123
                [SyntaxError::class, sprintf('"%s" tag with discover option must contain one "url()" / "path()" call.', $this->tagName)],
124
            ],
125
            [
126
                // with 2 routing functions
127
                '{% routewithdiscover discover %}{{ url("secure1") }}{{ url("secure2") }}{% endroutewithdiscover %}',
128
                [SyntaxError::class, sprintf('"%s" tag with discover option must contain only one "url()" / "path()" call.', $this->tagName)],
129
            ],
130
        ];
131
    }
132
}
133