ForTest.php 8.0KB

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