OptimizerTest.php 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testRenderBlockOptimizer()
  13. {
  14. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
  15. $env->addExtension(new Twig_Extension_Optimizer());
  16. $stream = $env->parse($env->tokenize('{{ block("foo") }}', 'index'));
  17. $node = $stream->getNode('body');
  18. $this->assertInstanceOf('Twig_Node_Expression_BlockReference', $node);
  19. $this->assertTrue($node->getAttribute('output'));
  20. }
  21. public function testRenderVariableBlockOptimizer()
  22. {
  23. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
  24. $env->addExtension(new Twig_Extension_Optimizer());
  25. $stream = $env->parse($env->tokenize('{{ block(name|lower) }}', 'index'));
  26. $node = $stream->getNode('body');
  27. $this->assertInstanceOf('Twig_Node_Expression_BlockReference', $node);
  28. $this->assertTrue($node->getAttribute('output'));
  29. }
  30. /**
  31. * @dataProvider getTestsForForOptimizer
  32. */
  33. public function testForOptimizer($template, $expected)
  34. {
  35. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false));
  36. $env->addExtension(new Twig_Extension_Optimizer());
  37. $stream = $env->parse($env->tokenize($template, 'index'));
  38. foreach ($expected as $target => $withLoop) {
  39. $this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
  40. }
  41. }
  42. public function getTestsForForOptimizer()
  43. {
  44. return array(
  45. array('{% for i in foo %}{% endfor %}', array('i' => false)),
  46. array('{% for i in foo %}{{ loop.index }}{% endfor %}', array('i' => true)),
  47. array('{% for i in foo %}{% for j in foo %}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
  48. array('{% for i in foo %}{% include "foo" %}{% endfor %}', array('i' => true)),
  49. array('{% for i in foo %}{% include "foo" only %}{% endfor %}', array('i' => false)),
  50. array('{% for i in foo %}{% for j in foo %}{{ loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => true)),
  51. array('{% for i in foo %}{% for j in foo %}{{ loop.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
  52. array('{% for i in foo %}{% set l = loop %}{% for j in foo %}{{ l.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => false)),
  53. array('{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
  54. array('{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
  55. );
  56. }
  57. public function checkForConfiguration(Twig_NodeInterface $node = null, $target, $withLoop)
  58. {
  59. if (null === $node) {
  60. return;
  61. }
  62. foreach ($node as $n) {
  63. if ($n instanceof Twig_Node_For) {
  64. if ($target === $n->getNode('value_target')->getAttribute('name')) {
  65. return $withLoop == $n->getAttribute('with_loop');
  66. }
  67. }
  68. $ret = $this->checkForConfiguration($n, $target, $withLoop);
  69. if (null !== $ret) {
  70. return $ret;
  71. }
  72. }
  73. }
  74. }