-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Finder] Fix iteration fails with non-rewindable streams #8120
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -459,4 +459,21 @@ public function testMultipleLocations() | |
|
||
$this->assertEquals(1, count($finder)); | ||
} | ||
|
||
public function testNonSeekableStream() | ||
{ | ||
try { | ||
$i = Finder::create()->in('ftp://ftp.mozilla.org/')->depth(0)->getIterator(); | ||
} catch (\UnexpectedValueException $e) { | ||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', 'ftp')); | ||
} | ||
|
||
$contains = array( | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README', | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html', | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the iterator really return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
); | ||
|
||
$this->assertIteratorInForeach($contains, $i); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,41 @@ protected function assertOrderedIterator($expected, \Traversable $iterator) | |
|
||
$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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, I do not really reflects on it name. Have you a better naming? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When reading the method names in the test before seeing their definition, I thought it was asserting that the iterator contains these elements, not that it was exactly these elements, because of the |
||
{ | ||
$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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Finder\Tests\Iterator; | ||
|
||
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator; | ||
|
||
class RecursiveDirectoryIteratorTest extends IteratorTestCase | ||
{ | ||
/** | ||
* @dataProvider getPaths | ||
* | ||
* @param string $path | ||
* @param Boolean $seekable | ||
* @param Boolean $supports | ||
* @param string $message | ||
*/ | ||
public function testRewind($path, $seekable, $contains, $message = null) | ||
{ | ||
try { | ||
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS); | ||
} catch (\UnexpectedValueException $e) { | ||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path)); | ||
} | ||
|
||
$i->rewind(); | ||
|
||
$this->assertTrue(true, $message); | ||
} | ||
|
||
/** | ||
* @dataProvider getPaths | ||
* | ||
* @param string $path | ||
* @param Boolean $seekable | ||
* @param Boolean $supports | ||
* @param string $message | ||
*/ | ||
public function testSeek($path, $seekable, $contains, $message = null) | ||
{ | ||
try { | ||
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS); | ||
} catch (\UnexpectedValueException $e) { | ||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path)); | ||
} | ||
|
||
$actual = array(); | ||
|
||
$i->seek(0); | ||
$actual[] = $i->getPathname(); | ||
|
||
$i->seek(1); | ||
$actual[] = $i->getPathname(); | ||
|
||
$i->seek(2); | ||
$actual[] = $i->getPathname(); | ||
|
||
$this->assertEquals($contains, $actual); | ||
} | ||
|
||
public function getPaths() | ||
{ | ||
$data = array(); | ||
|
||
// ftp | ||
$contains = array( | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README', | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html', | ||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub', | ||
); | ||
$data[] = array('ftp://ftp.mozilla.org/', false, $contains); | ||
|
||
return $data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A data provider with a single dataset is useless. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed however it will be more easy to add others dataset. Moreover the data provider is used by two test method. So it avoids duplicate code. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RecursiveDirectoryIterator
is a\FilesystemIterator
so your fix does not have the same behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a real "fix" it CS fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stloyd your suggestion is not a CS fix as the suggested code does not have the same behavior than the current code of the PR. Combining both conditions together changes what goes through the
elseif