From 3ba7b5ca4e834d224f4c3187f583852c0499c7fe Mon Sep 17 00:00:00 2001 From: acarpio Date: Sat, 24 Mar 2018 22:37:55 +1100 Subject: [PATCH 1/3] Failing test for views looping over generators --- tests/View/ViewFactoryTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index b4e91ec5ab14..3ae6ed406e3b 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -577,6 +577,25 @@ public function testAddingTraversableLoop() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } + public function testAddingLoopDoesNotCloseGenerator() + { + $factory = $this->getFactory(); + + $data = (new class { + public function generate() { + for($count = 0; $count < 3; $count++) { + yield ['a', 'b']; + } + } + })->generate(); + + $factory->addLoop($data); + + foreach ($data as $chunk) { + $this->assertEquals(['a', 'b'], $chunk); + } + } + public function testAddingUncountableLoop() { $factory = $this->getFactory(); From cfe56b863c502a6eecd520a69b8ca7afb14e8147 Mon Sep 17 00:00:00 2001 From: acarpio Date: Sat, 24 Mar 2018 22:39:27 +1100 Subject: [PATCH 2/3] Revert "Set up loop variable correctly on all Traversable objects (#23388)" This reverts commit 12dcc578596da6bc983b9ce30f7ee0bc2c5d9987. --- src/Illuminate/View/Concerns/ManagesLoops.php | 9 +------ tests/View/ViewFactoryTest.php | 24 +------------------ 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index ff3332c3802e..5f50b247efd9 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -3,7 +3,6 @@ namespace Illuminate\View\Concerns; use Countable; -use Traversable; use Illuminate\Support\Arr; trait ManagesLoops @@ -23,13 +22,7 @@ trait ManagesLoops */ public function addLoop($data) { - $length = null; - - if (is_array($data) || $data instanceof Countable) { - $length = count($data); - } elseif ($data instanceof Traversable) { - $length = iterator_count($data); - } + $length = is_array($data) || $data instanceof Countable ? count($data) : null; $parent = Arr::last($this->loopsStack); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index 3ae6ed406e3b..df9c5e08c481 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -555,28 +555,6 @@ public function testAddingLoops() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } - public function testAddingTraversableLoop() - { - $factory = $this->getFactory(); - - $data = new \DatePeriod(\Carbon\Carbon::today(), \Carbon\CarbonInterval::day(), \Carbon\Carbon::tomorrow()->endOfDay()); - - $factory->addLoop($data); - - $expectedLoop = [ - 'iteration' => 0, - 'index' => 0, - 'remaining' => 2, - 'count' => 2, - 'first' => true, - 'last' => false, - 'depth' => 1, - 'parent' => null, - ]; - - $this->assertEquals([$expectedLoop], $factory->getLoopStack()); - } - public function testAddingLoopDoesNotCloseGenerator() { $factory = $this->getFactory(); @@ -595,7 +573,7 @@ public function generate() { $this->assertEquals(['a', 'b'], $chunk); } } - + public function testAddingUncountableLoop() { $factory = $this->getFactory(); From ada9aaeb273c7530070c1207f87961bc89ba0ba4 Mon Sep 17 00:00:00 2001 From: acarpio Date: Sat, 24 Mar 2018 23:02:12 +1100 Subject: [PATCH 3/3] Style fixes --- tests/View/ViewFactoryTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index df9c5e08c481..c88a45727e2e 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -560,7 +560,8 @@ public function testAddingLoopDoesNotCloseGenerator() $factory = $this->getFactory(); $data = (new class { - public function generate() { + public function generate() + { for($count = 0; $count < 3; $count++) { yield ['a', 'b']; } @@ -573,7 +574,7 @@ public function generate() { $this->assertEquals(['a', 'b'], $chunk); } } - + public function testAddingUncountableLoop() { $factory = $this->getFactory();