You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

grammarTest.php 2.3KB

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