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\TokenParser; |
||
12 | |||
13 | use Twig\Node\Node; |
||
14 | use Twig\Node\TextNode; |
||
15 | use Twig\Node\PrintNode; |
||
16 | use Twig\Node\Expression\NameExpression; |
||
17 | use Twig\Error\SyntaxError; |
||
18 | use Yarhon\RouteGuardBundle\Tests\Twig\AbstractNodeTest; |
||
19 | use Yarhon\RouteGuardBundle\Twig\Node\RouteNode; |
||
20 | use Yarhon\RouteGuardBundle\Twig\TokenParser\RouteTokenParser; |
||
21 | |||
22 | class RouteTokenParserTest extends AbstractNodeTest |
||
23 | { |
||
24 | public function testConstruct() |
||
25 | { |
||
26 | $tokenParser = new RouteTokenParser('foo'); |
||
27 | |||
28 | $this->assertEquals('foo', $tokenParser->getTag()); |
||
29 | } |
||
30 | |||
31 | public function testGetTag() |
||
32 | { |
||
33 | $tokenParser = new RouteTokenParser('foo'); |
||
34 | $this->assertEquals('foo', $tokenParser->getTag()); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @dataProvider parseDataProvider |
||
39 | */ |
||
40 | public function testParse($source, $expected) |
||
41 | { |
||
42 | $node = $this->parse($source); |
||
43 | $node->removeNode('condition'); |
||
44 | |||
45 | $this->assertEquals($expected, $node); |
||
46 | } |
||
47 | |||
48 | public function parseDataProvider() |
||
49 | { |
||
50 | return [ |
||
51 | [ |
||
52 | // body node test |
||
53 | '{% $tagName "secure1" %}<a href="{{ link }}">Link</a>{% end$tagName %}', |
||
54 | new RouteNode( |
||
55 | null, |
||
56 | new Node([ |
||
57 | new TextNode('<a href="', 0), |
||
58 | new PrintNode(new NameExpression('link', 0), 0), |
||
59 | new TextNode('">Link</a>', 0), |
||
60 | ]) |
||
61 | ), |
||
62 | ], |
||
63 | |||
64 | [ |
||
65 | // else node test |
||
66 | '{% $tagName "secure1" %}{% else %}else text{% end$tagName %}', |
||
67 | new RouteNode( |
||
68 | null, |
||
69 | new Node(), |
||
70 | new TextNode('else text', 0) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
71 | ), |
||
72 | ], |
||
73 | ]; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @dataProvider parseExceptionDataProvider |
||
78 | */ |
||
79 | public function testParseException($source, $expected) |
||
80 | { |
||
81 | $this->expectException($expected[0]); |
||
82 | if (isset($expected[1])) { |
||
83 | $this->expectExceptionMessage($expected[1]); |
||
84 | } |
||
85 | |||
86 | $this->parse($source); |
||
87 | } |
||
88 | |||
89 | public function parseExceptionDataProvider() |
||
90 | { |
||
91 | return [ |
||
92 | [ |
||
93 | // without end tag |
||
94 | '{% $tagName "secure1" %}{% end %}', |
||
95 | [SyntaxError::class], |
||
96 | ], |
||
97 | [ |
||
98 | // without arguments and "discover" |
||
99 | '{% $tagName %}{% end$tagName %}', |
||
100 | [SyntaxError::class], |
||
101 | ], |
||
102 | ]; |
||
103 | } |
||
104 | } |
||
105 |