Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 24, 2018
1 parent 3fd1b23 commit d0a2613
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
9 changes: 1 addition & 8 deletions src/Illuminate/View/Concerns/ManagesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\View\Concerns;

use Countable;
use Traversable;
use Illuminate\Support\Arr;

trait ManagesLoops
Expand All @@ -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);

Expand Down
26 changes: 12 additions & 14 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,26 +555,24 @@ public function testAddingLoops()
$this->assertEquals([$expectedLoop], $factory->getLoopStack());
}

public function testAddingTraversableLoop()
public function testAddingLoopDoesNotCloseGenerator()
{
$factory = $this->getFactory();

$data = new \DatePeriod(\Carbon\Carbon::today(), \Carbon\CarbonInterval::day(), \Carbon\Carbon::tomorrow()->endOfDay());
$data = (new class {
public function generate()
{
for($count = 0; $count < 3; $count++) {
yield ['a', 'b'];
}
}
})->generate();

$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());
foreach ($data as $chunk) {
$this->assertEquals(['a', 'b'], $chunk);
}
}

public function testAddingUncountableLoop()
Expand Down

0 comments on commit d0a2613

Please sign in to comment.