NodeTestCase.php 1.8 KB

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