TestCase.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. abstract class Twig_Tests_Node_TestCase extends PHPUnit_Framework_TestCase
  11. {
  12. abstract public function getTests();
  13. /**
  14. * @dataProvider getTests
  15. */
  16. public function testCompile($node, $source, $environment = null)
  17. {
  18. $this->assertNodeCompilation($source, $node, $environment);
  19. }
  20. public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null)
  21. {
  22. $compiler = $this->getCompiler($environment);
  23. $compiler->compile($node);
  24. $this->assertEquals($source, trim($compiler->getSource()));
  25. }
  26. protected function getCompiler(Twig_Environment $environment = null)
  27. {
  28. return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
  29. }
  30. protected function getEnvironment()
  31. {
  32. return new Twig_Environment();
  33. }
  34. protected function getVariableGetter($name)
  35. {
  36. if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
  37. return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
  38. }
  39. return sprintf('$this->getContext($context, "%s")', $name);
  40. }
  41. protected function getAttributeGetter()
  42. {
  43. if (function_exists('twig_template_get_attributes')) {
  44. return 'twig_template_get_attributes($this, ';
  45. }
  46. return '$this->getAttribute(';
  47. }
  48. }