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
12/**
13 * @group legacy
14 */
15class Twig_Tests_SimpleTokenParserTest extends \PHPUnit\Framework\TestCase
16{
17    /**
18     * @dataProvider getTests
19     */
20    public function testParseGrammar($str, $grammar)
21    {
22        $this->assertEquals($grammar, Twig_Extensions_SimpleTokenParser::parseGrammar($str), '::parseGrammar() parses a grammar');
23    }
24
25    public function testParseGrammarExceptions()
26    {
27        try {
28            Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo>');
29            $this->fail();
30        } catch (Exception $e) {
31            $this->assertEquals('Twig_Error_Runtime', get_class($e));
32        }
33
34        try {
35            Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo');
36            $this->fail();
37        } catch (Exception $e) {
38            $this->assertEquals('Twig_Error_Runtime', get_class($e));
39        }
40
41        try {
42            Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo> (with');
43            $this->fail();
44        } catch (Exception $e) {
45            $this->assertEquals('Twig_Error_Runtime', get_class($e));
46        }
47    }
48
49    public function getTests()
50    {
51        return array(
52            array('', new Twig_Extensions_Grammar_Tag()),
53            array('const', new Twig_Extensions_Grammar_Tag(
54                new Twig_Extensions_Grammar_Constant('const')
55            )),
56            array('   const   ', new Twig_Extensions_Grammar_Tag(
57                new Twig_Extensions_Grammar_Constant('const')
58            )),
59            array('<expr>', new Twig_Extensions_Grammar_Tag(
60                new Twig_Extensions_Grammar_Expression('expr')
61            )),
62            array('<expr:expression>', new Twig_Extensions_Grammar_Tag(
63                new Twig_Extensions_Grammar_Expression('expr')
64            )),
65            array('   <expr:expression>   ', new Twig_Extensions_Grammar_Tag(
66                new Twig_Extensions_Grammar_Expression('expr')
67            )),
68            array('<nb:number>', new Twig_Extensions_Grammar_Tag(
69                new Twig_Extensions_Grammar_Number('nb')
70            )),
71            array('<bool:boolean>', new Twig_Extensions_Grammar_Tag(
72                new Twig_Extensions_Grammar_Boolean('bool')
73            )),
74            array('<content:body>', new Twig_Extensions_Grammar_Tag(
75                new Twig_Extensions_Grammar_Body('content')
76            )),
77            array('<expr:expression> [with <arguments:array>]', new Twig_Extensions_Grammar_Tag(
78                new Twig_Extensions_Grammar_Expression('expr'),
79                new Twig_Extensions_Grammar_Optional(
80                    new Twig_Extensions_Grammar_Constant('with'),
81                    new Twig_Extensions_Grammar_Array('arguments')
82                )
83            )),
84            array('  <expr:expression>   [  with   <arguments:array> ]  ', new Twig_Extensions_Grammar_Tag(
85                new Twig_Extensions_Grammar_Expression('expr'),
86                new Twig_Extensions_Grammar_Optional(
87                    new Twig_Extensions_Grammar_Constant('with'),
88                    new Twig_Extensions_Grammar_Array('arguments')
89                )
90            )),
91            array('<expr:expression> [with <arguments:array> [or <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
92                new Twig_Extensions_Grammar_Expression('expr'),
93                new Twig_Extensions_Grammar_Optional(
94                    new Twig_Extensions_Grammar_Constant('with'),
95                    new Twig_Extensions_Grammar_Array('arguments'),
96                    new Twig_Extensions_Grammar_Optional(
97                        new Twig_Extensions_Grammar_Constant('or'),
98                        new Twig_Extensions_Grammar_Expression('optional')
99                    )
100                )
101            )),
102            array('<expr:expression> [with <arguments:array> [, <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
103                new Twig_Extensions_Grammar_Expression('expr'),
104                new Twig_Extensions_Grammar_Optional(
105                    new Twig_Extensions_Grammar_Constant('with'),
106                    new Twig_Extensions_Grammar_Array('arguments'),
107                    new Twig_Extensions_Grammar_Optional(
108                        new Twig_Extensions_Grammar_Constant(',', Twig_Token::PUNCTUATION_TYPE),
109                        new Twig_Extensions_Grammar_Expression('optional')
110                    )
111                )
112            )),
113        );
114    }
115}
116