[Finder] Fix unexpected duplicate sub path related AppendIterator issue

This commit is contained in:
alquerci 2013-05-24 10:59:59 +02:00 committed by Fabien Potencier
parent e789a02d5a
commit 45b68e02bd
6 changed files with 71 additions and 0 deletions

View File

@ -38,4 +38,12 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
{
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
}
public function rewind()
{
// @see https://bugs.php.net/bug.php?id=49104
parent::next();
parent::rewind();
}
}

View File

@ -459,4 +459,30 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals(1, count($finder));
}
/**
* Searching in multiple locations with sub directories involves
* AppendIterator which does an unnecessary rewind which leaves
* FilterIterator with inner FilesystemIterator in an ivalid state.
*
* @see https://bugs.php.net/bug.php?id=49104
*/
public function testMultipleLocationsWithSubDirectories()
{
$locations = array(
__DIR__.'/Fixtures/one',
self::$files[8],
);
$finder = new Finder();
$finder->in($locations)->depth('< 10')->name('*.neon');
$expected = array(
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
);
$this->assertIterator($expected, $finder);
$this->assertIteratorInForeach($expected, $finder);
}
}

View File

@ -29,4 +29,41 @@ abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, array_values($values));
}
/**
* Same as IteratorTestCase::assertIterator with foreach usage
*
* @param array $expected
* @param \Traversable $iterator
*/
protected function assertIteratorInForeach($expected, \Traversable $iterator)
{
$values = array();
foreach ($iterator as $file) {
$this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
$values[] = $file->getPathname();
}
sort($values);
sort($expected);
$this->assertEquals($expected, array_values($values));
}
/**
* Same as IteratorTestCase::assertOrderedIterator with foreach usage
*
* @param array $expected
* @param \Traversable $iterator
*/
protected function assertOrderedIteratorInForeach($expected, \Traversable $iterator)
{
$values = array();
foreach ($iterator as $file) {
$this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
$values[] = $file->getPathname();
}
$this->assertEquals($expected, array_values($values));
}
}