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 grammarTest extends \PHPUnit\Framework\TestCase
16{
17    protected function setUp()
18    {
19        require_once __DIR__.'/SimpleTokenParser.php';
20    }
21
22    /**
23     * @dataProvider getTests
24     */
25    public function testGrammar($tag, $grammar, $template, $output, $exception)
26    {
27        $twig = new Twig_Environment(new Twig_Loader_Array(array('template' => $template)), array('cache' => false, 'autoescape' => false, 'debug' => true));
28        $twig->addExtension(new Twig_Extension_Debug());
29        $twig->addTokenParser(new SimpleTokenParser($tag, $grammar));
30
31        $ok = true;
32        try {
33            $template = $twig->loadTemplate('template');
34        } catch (Exception $e) {
35            $ok = false;
36
37            if (false === $exception) {
38                $this->fail('Exception not expected');
39            } else {
40                $this->assertEquals($exception, get_class($e));
41            }
42        }
43
44        if ($ok) {
45            if (false !== $exception) {
46                $this->fail(sprintf('Exception "%s" expected', $exception));
47            }
48
49            $actual = $template->render(array());
50            $this->assertEquals($output, $actual);
51        }
52    }
53
54    public function getTests()
55    {
56        return array(
57            array('foo1', '', '{% foo1 %}', '|', false),
58            array('foo2', '', '{% foo2 "bar" %}', '|', 'Twig_Error_Syntax'),
59            array('foo3', '<foo>', '{% foo3 "bar" %}', '|bar|', false),
60            array('foo4', '<foo>', '{% foo4 1 + 2 %}', '|3|', false),
61            array('foo5', '<foo:expression>', '{% foo5 1 + 2 %}', '|3|', false),
62            array('foo6', '<foo:array>', '{% foo6 1 + 2 %}', '|3|', 'Twig_Error_Syntax'),
63            array('foo7', '<foo>', '{% foo7 %}', '|3|', 'Twig_Error_Syntax'),
64            array('foo8', '<foo:array>', '{% foo8 [1, 2] %}', "|int(0)\nint(1)\nint(1)\nint(2)\n|", false),
65            array('foo9', '<foo> with <bar>', '{% foo9 "bar" with "foobar" %}', '|bar|with|foobar|', false),
66            array('foo10', '<foo> [with <bar>]', '{% foo10 "bar" with "foobar" %}', '|bar|with|foobar|', false),
67            array('foo11', '<foo> [with <bar>]', '{% foo11 "bar" %}', '|bar|', false),
68        );
69    }
70}
71