grammarTest.php 2.4 KB

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