Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpack nested IteratorAggregate objects for Count #2689

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Framework/Constraint/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected function getCountOf($other)
}

if ($other instanceof Traversable) {
if ($other instanceof IteratorAggregate) {
$iterator = $other->getIterator();
} else {
$iterator = $other;
while ($other instanceof IteratorAggregate) {
$other = $other->getIterator();
}

$iterator = $other;

if ($iterator instanceof Generator) {
return $this->getCountOfGenerator($iterator);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Framework/Constraint/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ public function testCount()

$countConstraint = new Count(2);
$it = new \TestIterator([1, 2]);
$ia = new \TestIteratorAggregate($it);
$ia2 = new \TestIteratorAggregate2($ia);

$this->assertTrue($countConstraint->evaluate($it, '', true));
$this->assertTrue($countConstraint->evaluate($ia, '', true));
$this->assertTrue($countConstraint->evaluate($ia2, '', true));
}

public function testCountDoesNotChangeIteratorKey()
Expand Down Expand Up @@ -60,6 +64,39 @@ public function testCountDoesNotChangeIteratorKey()
$it->next();
$countConstraint->evaluate($it, '', true);
$this->assertFalse($it->valid());

// test with IteratorAggregate
$it = new \TestIterator([1, 2]);
$ia = new \TestIteratorAggregate($it);

$countConstraint = new Count(2);
$countConstraint->evaluate($ia, '', true);
$this->assertEquals(1, $it->current());

$it->next();
$countConstraint->evaluate($ia, '', true);
$this->assertEquals(2, $it->current());

$it->next();
$countConstraint->evaluate($ia, '', true);
$this->assertFalse($it->valid());

// test with nested IteratorAggregate
$it = new \TestIterator([1, 2]);
$ia = new \TestIteratorAggregate($it);
$ia2 = new \TestIteratorAggregate2($ia);

$countConstraint = new Count(2);
$countConstraint->evaluate($ia2, '', true);
$this->assertEquals(1, $it->current());

$it->next();
$countConstraint->evaluate($ia2, '', true);
$this->assertEquals(2, $it->current());

$it->next();
$countConstraint->evaluate($ia2, '', true);
$this->assertFalse($it->valid());
}

public function testCountGeneratorsDoNotRewind()
Expand Down
16 changes: 16 additions & 0 deletions tests/_files/TestIteratorAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class TestIteratorAggregate implements IteratorAggregate
{
private $traversable;

public function __construct(\Traversable $traversable)
{
$this->traversable = $traversable;
}

public function getIterator()
{
return $this->traversable;
}
}
19 changes: 19 additions & 0 deletions tests/_files/TestIteratorAggregate2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/* This class is used for testing a chain of IteratorAggregate objects, since
* PHP does allow IteratorAggregate::getIterator() to return an instance of the
* same class. */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: zend_user_it_get_new_iterator(). If IteratorAggregate::getIterator() returns an instance of the same class, PHP throws an exception. This is a basic attempt to prevent infinite recursion, although it is certainly still possible if two different IteratorAggregate implementations return instances of each other.

class TestIteratorAggregate2 implements IteratorAggregate
{
private $traversable;

public function __construct(\Traversable $traversable)
{
$this->traversable = $traversable;
}

public function getIterator()
{
return $this->traversable;
}
}