4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Tests\Iterator;
14 use PHPUnit\Framework\TestCase;
16 abstract class IteratorTestCase extends TestCase
18 protected function assertIterator($expected, \Traversable $iterator)
20 // set iterator_to_array $use_key to false to avoid values merge
21 // this made FinderTest::testAppendWithAnArray() fail with GnuFinderAdapter
22 $values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator, false));
24 $expected = array_map(function ($path) { return str_replace('/', DIRECTORY_SEPARATOR, $path); }, $expected);
29 $this->assertEquals($expected, array_values($values));
32 protected function assertOrderedIterator($expected, \Traversable $iterator)
34 $values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
36 $this->assertEquals($expected, array_values($values));
40 * Same as assertOrderedIterator, but checks the order of groups of
43 * @param array $expected - an array of arrays. For any two subarrays
44 * $a and $b such that $a goes before $b in $expected, the method
45 * asserts that any element of $a goes before any element of $b
46 * in the sequence generated by $iterator
47 * @param \Traversable $iterator
49 protected function assertOrderedIteratorForGroups($expected, \Traversable $iterator)
51 $values = array_values(array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator)));
53 foreach ($expected as $subarray) {
55 while (count($values) && count($temp) < count($subarray)) {
56 $temp[] = array_shift($values);
60 $this->assertEquals($subarray, $temp);
65 * Same as IteratorTestCase::assertIterator with foreach usage.
67 * @param array $expected
68 * @param \Traversable $iterator
70 protected function assertIteratorInForeach($expected, \Traversable $iterator)
73 foreach ($iterator as $file) {
74 $this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
75 $values[] = $file->getPathname();
81 $this->assertEquals($expected, array_values($values));
85 * Same as IteratorTestCase::assertOrderedIterator with foreach usage.
87 * @param array $expected
88 * @param \Traversable $iterator
90 protected function assertOrderedIteratorInForeach($expected, \Traversable $iterator)
93 foreach ($iterator as $file) {
94 $this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
95 $values[] = $file->getPathname();
98 $this->assertEquals($expected, array_values($values));