ForTest.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_Node_ForTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_For::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $keyTarget = new Twig_Node_Expression_AssignName('key', 1);
  18. $valueTarget = new Twig_Node_Expression_AssignName('item', 1);
  19. $seq = new Twig_Node_Expression_Name('items', 1);
  20. $ifexpr = new Twig_Node_Expression_Constant(true, 1);
  21. $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
  22. $else = null;
  23. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  24. $node->setAttribute('with_loop', false);
  25. $this->assertEquals($keyTarget, $node->getNode('key_target'));
  26. $this->assertEquals($valueTarget, $node->getNode('value_target'));
  27. $this->assertEquals($seq, $node->getNode('seq'));
  28. $this->assertTrue($node->getAttribute('ifexpr'));
  29. $this->assertEquals('Twig_Node_If', get_class($node->getNode('body')));
  30. $this->assertEquals($body, $node->getNode('body')->getNode('tests')->getNode(1)->getNode(0));
  31. $this->assertEquals(null, $node->getNode('else'));
  32. $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1);
  33. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  34. $node->setAttribute('with_loop', false);
  35. $this->assertEquals($else, $node->getNode('else'));
  36. }
  37. /**
  38. * @covers Twig_Node_For::compile
  39. * @dataProvider getTests
  40. */
  41. public function testCompile($node, $source, $environment = null)
  42. {
  43. parent::testCompile($node, $source, $environment);
  44. }
  45. public function getTests()
  46. {
  47. $tests = array();
  48. $keyTarget = new Twig_Node_Expression_AssignName('key', 1);
  49. $valueTarget = new Twig_Node_Expression_AssignName('item', 1);
  50. $seq = new Twig_Node_Expression_Name('items', 1);
  51. $ifexpr = null;
  52. $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
  53. $else = null;
  54. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  55. $node->setAttribute('with_loop', false);
  56. $tests[] = array($node, <<<EOF
  57. // line 1
  58. \$context['_parent'] = (array) \$context;
  59. \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('items')});
  60. foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) {
  61. echo {$this->getVariableGetter('foo')};
  62. }
  63. \$_parent = \$context['_parent'];
  64. unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
  65. \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));
  66. EOF
  67. );
  68. $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
  69. $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
  70. $seq = new Twig_Node_Expression_Name('values', 1);
  71. $ifexpr = null;
  72. $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
  73. $else = null;
  74. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  75. $node->setAttribute('with_loop', true);
  76. $tests[] = array($node, <<<EOF
  77. // line 1
  78. \$context['_parent'] = (array) \$context;
  79. \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
  80. \$context['loop'] = array(
  81. 'parent' => \$context['_parent'],
  82. 'index0' => 0,
  83. 'index' => 1,
  84. 'first' => true,
  85. );
  86. if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
  87. \$length = count(\$context['_seq']);
  88. \$context['loop']['revindex0'] = \$length - 1;
  89. \$context['loop']['revindex'] = \$length;
  90. \$context['loop']['length'] = \$length;
  91. \$context['loop']['last'] = 1 === \$length;
  92. }
  93. foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
  94. echo {$this->getVariableGetter('foo')};
  95. ++\$context['loop']['index0'];
  96. ++\$context['loop']['index'];
  97. \$context['loop']['first'] = false;
  98. if (isset(\$context['loop']['length'])) {
  99. --\$context['loop']['revindex0'];
  100. --\$context['loop']['revindex'];
  101. \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
  102. }
  103. }
  104. \$_parent = \$context['_parent'];
  105. unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
  106. \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));
  107. EOF
  108. );
  109. $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
  110. $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
  111. $seq = new Twig_Node_Expression_Name('values', 1);
  112. $ifexpr = new Twig_Node_Expression_Constant(true, 1);
  113. $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
  114. $else = null;
  115. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  116. $node->setAttribute('with_loop', true);
  117. $tests[] = array($node, <<<EOF
  118. // line 1
  119. \$context['_parent'] = (array) \$context;
  120. \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
  121. \$context['loop'] = array(
  122. 'parent' => \$context['_parent'],
  123. 'index0' => 0,
  124. 'index' => 1,
  125. 'first' => true,
  126. );
  127. foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
  128. if (true) {
  129. echo {$this->getVariableGetter('foo')};
  130. ++\$context['loop']['index0'];
  131. ++\$context['loop']['index'];
  132. \$context['loop']['first'] = false;
  133. }
  134. }
  135. \$_parent = \$context['_parent'];
  136. unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
  137. \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));
  138. EOF
  139. );
  140. $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
  141. $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
  142. $seq = new Twig_Node_Expression_Name('values', 1);
  143. $ifexpr = null;
  144. $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
  145. $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1);
  146. $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
  147. $node->setAttribute('with_loop', true);
  148. $tests[] = array($node, <<<EOF
  149. // line 1
  150. \$context['_parent'] = (array) \$context;
  151. \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
  152. \$context['_iterated'] = false;
  153. \$context['loop'] = array(
  154. 'parent' => \$context['_parent'],
  155. 'index0' => 0,
  156. 'index' => 1,
  157. 'first' => true,
  158. );
  159. if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
  160. \$length = count(\$context['_seq']);
  161. \$context['loop']['revindex0'] = \$length - 1;
  162. \$context['loop']['revindex'] = \$length;
  163. \$context['loop']['length'] = \$length;
  164. \$context['loop']['last'] = 1 === \$length;
  165. }
  166. foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
  167. echo {$this->getVariableGetter('foo')};
  168. \$context['_iterated'] = true;
  169. ++\$context['loop']['index0'];
  170. ++\$context['loop']['index'];
  171. \$context['loop']['first'] = false;
  172. if (isset(\$context['loop']['length'])) {
  173. --\$context['loop']['revindex0'];
  174. --\$context['loop']['revindex'];
  175. \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
  176. }
  177. }
  178. if (!\$context['_iterated']) {
  179. echo {$this->getVariableGetter('foo')};
  180. }
  181. \$_parent = \$context['_parent'];
  182. unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
  183. \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));
  184. EOF
  185. );
  186. return $tests;
  187. }
  188. }