bug #40023 [Finder]  use proper keys to not override appended files (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Finder]  use proper keys to not override appended files

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #40022
| License       | MIT
| Doc PR        |

Commits
-------

036c8d71fd use proper keys to not override appended files
This commit is contained in:
Nicolas Grekas 2021-01-28 17:58:58 +01:00
commit e2428326e3
2 changed files with 13 additions and 1 deletions

View File

@ -669,7 +669,8 @@ class Finder implements \IteratorAggregate, \Countable
} elseif ($iterator instanceof \Traversable || \is_array($iterator)) {
$it = new \ArrayIterator();
foreach ($iterator as $file) {
$it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file));
$file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
$it[$file->getPathname()] = $file;
}
$this->iterators[] = $it;
} else {

View File

@ -1119,6 +1119,17 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
}
public function testMultipleAppendCallsWithSorting()
{
$finder = $this->buildFinder()
->sortByName()
->append([self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1000_1.php'])
->append([self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1002_0.php'])
;
$this->assertOrderedIterator($this->toAbsolute(['qux_1000_1.php', 'qux_1002_0.php']), $finder->getIterator());
}
public function testCountDirectories()
{
$directory = Finder::create()->directories()->in(self::$tmpDir);