grammarTest.php 2.2KB

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_String(), array('cache' => false));
  19. $twig->addTokenParser(new SimpleTokenParser($tag, $grammar));
  20. $ok = true;
  21. try {
  22. $template = $twig->loadTemplate($template);
  23. } catch (Exception $e) {
  24. $ok = false;
  25. if (false === $exception) {
  26. $this->fail('Exception not expected');
  27. } else {
  28. $this->assertEquals($exception, get_class($e));
  29. }
  30. }
  31. if ($ok) {
  32. if (false !== $exception) {
  33. $this->fail(sprintf('Exception "%s" expected', $exception));
  34. }
  35. $actual = $template->render(array());
  36. $this->assertEquals($output, $actual);
  37. }
  38. }
  39. public function getTests()
  40. {
  41. return array(
  42. array('foo1', '', '{% foo1 %}', '|', false),
  43. array('foo2', '', '{% foo2 "bar" %}', '|', 'Twig_Error_Syntax'),
  44. array('foo3', '<foo>', '{% foo3 "bar" %}', '|bar|', false),
  45. array('foo4', '<foo>', '{% foo4 1 + 2 %}', '|3|', false),
  46. array('foo5', '<foo:expression>', '{% foo5 1 + 2 %}', '|3|', false),
  47. array('foo6', '<foo:array>', '{% foo6 1 + 2 %}', '|3|', 'Twig_Error_Syntax'),
  48. array('foo7', '<foo>', '{% foo7 %}', '|3|', 'Twig_Error_Syntax'),
  49. array('foo8', '<foo:array>', '{% foo8 [1, 2] %}', '|Array|', false),
  50. array('foo9', '<foo> with <bar>', '{% foo9 "bar" with "foobar" %}', '|bar|with|foobar|', false),
  51. array('foo10', '<foo> [with <bar>]', '{% foo10 "bar" with "foobar" %}', '|bar|with|foobar|', false),
  52. array('foo11', '<foo> [with <bar>]', '{% foo11 "bar" %}', '|bar|', false),
  53. );
  54. }
  55. }