setAttribute('with_loop', false); $this->assertEquals($keyTarget, $node->getNode('key_target')); $this->assertEquals($valueTarget, $node->getNode('value_target')); $this->assertEquals($seq, $node->getNode('seq')); $this->assertEquals($body, $node->getNode('body')->getNode('0')); $this->assertFalse($node->hasNode('else')); $else = new ForElseNode(new PrintNode(new ContextVariable('foo', 1), 1), 5); $node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1); $node->setAttribute('with_loop', false); $this->assertEquals($else, $node->getNode('else')); } public static function provideTests(): iterable { $tests = []; $keyTarget = new AssignContextVariable('key', 1); $valueTarget = new AssignContextVariable('item', 1); $seq = new ContextVariable('items', 1); $body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1); $else = null; $node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1); $node->setAttribute('with_loop', false); $itemsGetter = self::createVariableGetter('items'); $fooGetter = self::createVariableGetter('foo'); $valuesGetter = self::createVariableGetter('values'); $tests[] = [$node, << \$context["item"]) { yield $fooGetter; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['key'], \$context['item'], \$context['_parent']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ]; $keyTarget = new AssignContextVariable('k', 1); $valueTarget = new AssignContextVariable('v', 1); $seq = new ContextVariable('values', 1); $body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1); $else = null; $node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = [$node, << \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ]; if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { yield $fooGetter; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['revindex0'], \$context['loop']['revindex'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ]; $keyTarget = new AssignContextVariable('k', 1); $valueTarget = new AssignContextVariable('v', 1); $seq = new ContextVariable('values', 1); $body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1); $else = null; $node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = [$node, << \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ]; if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { yield $fooGetter; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['revindex0'], \$context['loop']['revindex'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ]; $keyTarget = new AssignContextVariable('k', 1); $valueTarget = new AssignContextVariable('v', 1); $seq = new ContextVariable('values', 1); $body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1); $else = new ForElseNode(new PrintNode(new ContextVariable('foo', 6), 6), 5); $node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = [$node, << \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ]; if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { yield $fooGetter; \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['revindex0'], \$context['loop']['revindex'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } // line 5 if (!\$context['_iterated']) { // line 6 yield $fooGetter; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['_iterated'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ]; return $tests; } }