Merge branch '2.3' into 2.7

* 2.3:
  [Finder] Fix recursive filter iterator
  Improve the structure of the Finder testsuite

Conflicts:
	src/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.php
	src/Symfony/Component/Finder/Tests/FinderTest.php
	src/Symfony/Component/Finder/Tests/Iterator/DateRangeFilterIteratorTest.php
This commit is contained in:
Nicolas Grekas 2015-09-18 09:19:55 +02:00
commit 666f523984
13 changed files with 258 additions and 305 deletions

View File

@ -16,8 +16,9 @@ namespace Symfony\Component\Finder\Iterator;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExcludeDirectoryFilterIterator extends FilterIterator
class ExcludeDirectoryFilterIterator extends FilterIterator implements \RecursiveIterator
{
private $isRecursive;
private $patterns = array();
/**
@ -28,6 +29,7 @@ class ExcludeDirectoryFilterIterator extends FilterIterator
*/
public function __construct(\Iterator $iterator, array $directories)
{
$this->isRecursive = $iterator instanceof \RecursiveIterator;
foreach ($directories as $directory) {
$this->patterns[] = '#(^|/)'.preg_quote($directory, '#').'(/|$)#';
}
@ -52,4 +54,17 @@ class ExcludeDirectoryFilterIterator extends FilterIterator
return true;
}
public function hasChildren()
{
return $this->isRecursive && $this->getInnerIterator()->hasChildren();
}
public function getChildren()
{
$children = new self($this->getInnerIterator()->getChildren(), array());
$children->patterns = $this->patterns;
return $children;
}
}

View File

@ -18,18 +18,8 @@ namespace Symfony\Component\Finder\Iterator;
*
* @author Alex Bogomazov
*/
abstract class FilterIterator extends \FilterIterator implements \RecursiveIterator
abstract class FilterIterator extends \FilterIterator
{
public function hasChildren()
{
return $this->getInnerIterator() instanceof \RecursiveIterator && $this->getInnerIterator()->hasChildren();
}
public function getChildren()
{
return $this->getInnerIterator()->getChildren();
}
/**
* This is a workaround for the problem with \FilterIterator leaving inner \FilesystemIterator in wrong state after
* rewind in some cases.

View File

@ -0,0 +1,28 @@
<?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;
use Symfony\Component\Finder\Adapter\BsdFindAdapter;
class BsdFinderTest extends FinderTest
{
protected function getAdapter()
{
$adapter = new BsdFindAdapter();
if (!$adapter->isSupported()) {
$this->markTestSkipped(get_class($adapter).' is not supported.');
}
return $adapter;
}
}

View File

@ -11,8 +11,10 @@
namespace Symfony\Component\Finder\Tests;
use Symfony\Component\Finder\Adapter\AdapterInterface;
use Symfony\Component\Finder\Adapter\GnuFindAdapter;
use Symfony\Component\Finder\Adapter\PhpAdapter;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Adapter;
class FinderTest extends Iterator\RealIteratorTestCase
{
@ -21,109 +23,94 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testDirectories($adapter)
public function testDirectories()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->directories());
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->directories();
$finder->files();
$finder->directories();
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testFiles($adapter)
public function testFiles()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->files());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->files();
$finder->directories();
$finder->files();
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testDepth($adapter)
public function testDepth()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('< 1'));
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('<= 0'));
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('>= 1'));
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->depth('< 1')->depth('>= 1');
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testName($adapter)
public function testName()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->name('*.php'));
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('test.ph*');
$finder->name('test.py');
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('~^test~i');
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('~\\.php$~i');
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('test.p{hp,y}');
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testNotName($adapter)
public function testNotName()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->notName('*.php'));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->notName('*.php');
$finder->notName('*.py');
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('test.ph*');
$finder->name('test.py');
$finder->notName('*.php');
$finder->notName('*.py');
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name('test.ph*');
$finder->name('test.py');
$finder->notName('*.p{hp,y}');
@ -133,228 +120,186 @@ class FinderTest extends Iterator\RealIteratorTestCase
/**
* @dataProvider getRegexNameTestData
*/
public function testRegexName($adapter, $regex)
public function testRegexName($regex)
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->name($regex);
$this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSize($adapter)
public function testSize()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testDate($adapter)
public function testDate()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->files()->date('until last month'));
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testExclude($adapter)
public function testExclude()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->exclude('foo'));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testIgnoreVCS($adapter)
public function testIgnoreVCS()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testIgnoreDotFiles($adapter)
public function testIgnoreDotFiles()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSortByName($adapter)
public function testSortByName()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByName());
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSortByType($adapter)
public function testSortByType()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByType());
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSortByAccessedTime($adapter)
public function testSortByAccessedTime()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByAccessedTime());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSortByChangedTime($adapter)
public function testSortByChangedTime()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByChangedTime());
$this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSortByModifiedTime($adapter)
public function testSortByModifiedTime()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByModifiedTime());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testSort($adapter)
public function testSort()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testFilter($adapter)
public function testFilter()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testFollowLinks($adapter)
public function testFollowLinks()
{
if ('\\' == DIRECTORY_SEPARATOR) {
return;
$this->markTestSkipped('symlinks are not supported on Windows');
}
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->followLinks());
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testIn($adapter)
public function testIn()
{
$finder = $this->buildFinder($adapter);
try {
$finder->in('foobar');
$this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
}
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
$expected = array(
self::$tmpDir.DIRECTORY_SEPARATOR.'test.php',
__DIR__.DIRECTORY_SEPARATOR.'BsdFinderTest.php',
__DIR__.DIRECTORY_SEPARATOR.'FinderTest.php',
__DIR__.DIRECTORY_SEPARATOR.'GnuFinderTest.php',
__DIR__.DIRECTORY_SEPARATOR.'GlobTest.php',
);
$this->assertIterator($expected, $iterator);
}
/**
* @dataProvider getAdaptersTestData
* @expectedException \InvalidArgumentException
*/
public function testInWithGlob($adapter)
public function testInWithNonExistentDirectory()
{
$finder = $this->buildFinder($adapter);
$finder = new Finder();
$finder->in('foobar');
}
public function testInWithGlob()
{
$finder = $this->buildFinder();
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
}
/**
* @dataProvider getAdaptersTestData
* @expectedException \InvalidArgumentException
*/
public function testInWithNonDirectoryGlob($adapter)
public function testInWithNonDirectoryGlob()
{
$finder = $this->buildFinder($adapter);
$finder = new Finder();
$finder->in(__DIR__.'/Fixtures/A/a*');
}
/**
* @dataProvider getAdaptersTestData
*/
public function testInWithGlobBrace($adapter)
public function testInWithGlobBrace()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(array(__DIR__.'/Fixtures/{A,copy/A}/B/C'))->getIterator();
$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
}
/**
* @dataProvider getAdaptersTestData
* @expectedException \LogicException
*/
public function testGetIterator($adapter)
public function testGetIteratorWithoutIn()
{
$finder = $this->buildFinder($adapter);
try {
$finder->getIterator();
$this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
} catch (\Exception $e) {
$this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
}
$finder = Finder::create();
$finder->getIterator();
}
$finder = $this->buildFinder($adapter);
public function testGetIterator()
{
$finder = $this->buildFinder();
$dirs = array();
foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
$dirs[] = (string) $dir;
@ -367,22 +312,19 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$a = iterator_to_array($finder->directories()->in(self::$tmpDir));
$a = array_values(array_map(function ($a) { return (string) $a; }, $a));
sort($a);
$this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
}
/**
* @dataProvider getAdaptersTestData
*/
public function testRelativePath($adapter)
public function testRelativePath()
{
$finder = $this->buildFinder($adapter)->in(self::$tmpDir);
$finder = $this->buildFinder()->in(self::$tmpDir);
$paths = array();
@ -398,12 +340,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals($ref, $paths);
}
/**
* @dataProvider getAdaptersTestData
*/
public function testRelativePathname($adapter)
public function testRelativePathname()
{
$finder = $this->buildFinder($adapter)->in(self::$tmpDir)->sortByName();
$finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
$paths = array();
@ -419,15 +358,12 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals($ref, $paths);
}
/**
* @dataProvider getAdaptersTestData
*/
public function testAppendWithAFinder($adapter)
public function testAppendWithAFinder()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
$finder1 = $this->buildFinder($adapter);
$finder1 = $this->buildFinder();
$finder1->directories()->in(self::$tmpDir);
$finder = $finder->append($finder1);
@ -435,12 +371,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testAppendWithAnArray($adapter)
public function testAppendWithAnArray()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
$finder->append($this->toAbsolute(array('foo', 'toto')));
@ -448,20 +381,14 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
}
/**
* @dataProvider getAdaptersTestData
*/
public function testAppendReturnsAFinder($adapter)
public function testAppendReturnsAFinder()
{
$this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', $this->buildFinder($adapter)->append(array()));
$this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append(array()));
}
/**
* @dataProvider getAdaptersTestData
*/
public function testAppendDoesNotRequireIn($adapter)
public function testAppendDoesNotRequireIn()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
$finder1 = Finder::create()->append($finder);
@ -505,9 +432,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
/**
* @dataProvider getContainsTestData
*/
public function testContains($adapter, $matchPatterns, $noMatchPatterns, $expected)
public function testContains($matchPatterns, $noMatchPatterns, $expected)
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
->name('*.txt')->sortByName()
->contains($matchPatterns)
@ -516,12 +443,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
}
/**
* @dataProvider getAdaptersTestData
*/
public function testContainsOnDirectory(Adapter\AdapterInterface $adapter)
public function testContainsOnDirectory()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(__DIR__)
->directories()
->name('Fixtures')
@ -529,12 +453,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator(array(), $finder);
}
/**
* @dataProvider getAdaptersTestData
*/
public function testNotContainsOnDirectory(Adapter\AdapterInterface $adapter)
public function testNotContainsOnDirectory()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(__DIR__)
->directories()
->name('Fixtures')
@ -547,10 +468,8 @@ class FinderTest extends Iterator\RealIteratorTestCase
* with inner FilesystemIterator in an invalid state.
*
* @see https://bugs.php.net/bug.php?id=49104
*
* @dataProvider getAdaptersTestData
*/
public function testMultipleLocations(Adapter\AdapterInterface $adapter)
public function testMultipleLocations()
{
$locations = array(
self::$tmpDir.'/',
@ -558,36 +477,57 @@ class FinderTest extends Iterator\RealIteratorTestCase
);
// it is expected that there are test.py test.php in the tmpDir
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in($locations)->depth('< 1')->name('test.php');
$this->assertCount(1, $finder);
}
/**
* Iterator keys must be the file pathname.
* Searching in multiple locations with sub directories involves
* AppendIterator which does an unnecessary rewind which leaves
* FilterIterator with inner FilesystemIterator in an invalid state.
*
* @dataProvider getAdaptersTestData
* @see https://bugs.php.net/bug.php?id=49104
*/
public function testIteratorKeys(Adapter\AdapterInterface $adapter)
public function testMultipleLocationsWithSubDirectories()
{
$finder = $this->buildFinder($adapter)->in(self::$tmpDir);
$locations = array(
__DIR__.'/Fixtures/one',
self::$tmpDir.DIRECTORY_SEPARATOR.'toto',
);
$finder = $this->buildFinder();
$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);
}
/**
* Iterator keys must be the file pathname.
*/
public function testIteratorKeys()
{
$finder = $this->buildFinder()->in(self::$tmpDir);
foreach ($finder as $key => $file) {
$this->assertEquals($file->getPathname(), $key);
}
}
/**
* @dataProvider getAdaptersTestData
*/
public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag(Adapter\AdapterInterface $adapter)
public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
->path('/^dir/');
$expected = array('r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.'dir',
'r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'bar.dat',);
'r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'bar.dat',);
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
}
@ -603,7 +543,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals(
array('c', 'e', 'a', 'd', 'b'),
array_map(function (Adapter\AdapterInterface $adapter) {
array_map(function (AdapterInterface $adapter) {
return $adapter->getName();
}, $finder->getAdapters())
);
@ -626,17 +566,9 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertIterator($filenames, $finder->in(sys_get_temp_dir())->getIterator());
}
public function getAdaptersTestData()
{
return array_map(
function ($adapter) { return array($adapter); },
$this->getValidAdapters()
);
}
public function getContainsTestData()
{
$tests = array(
return array(
array('', '', array()),
array('foo', 'bar', array()),
array('', 'foobar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
@ -648,26 +580,22 @@ class FinderTest extends Iterator\RealIteratorTestCase
array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
);
return $this->buildTestData($tests);
}
public function getRegexNameTestData()
{
$tests = array(
return array(
array('~.+\\.p.+~i'),
array('~t.*s~i'),
);
return $this->buildTestData($tests);
}
/**
* @dataProvider getTestPathData
*/
public function testPath(Adapter\AdapterInterface $adapter, $matchPatterns, $noMatchPatterns, array $expected)
public function testPath($matchPatterns, $noMatchPatterns, array $expected)
{
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
->path($matchPatterns)
->notPath($noMatchPatterns);
@ -679,20 +607,20 @@ class FinderTest extends Iterator\RealIteratorTestCase
{
// test that by default, PhpAdapter is selected
$adapters = Finder::create()->getAdapters();
$this->assertTrue($adapters[0] instanceof Adapter\PhpAdapter);
$this->assertTrue($adapters[0] instanceof PhpAdapter);
// test another adapter selection
$adapters = Finder::create()->setAdapter('gnu_find')->getAdapters();
$this->assertTrue($adapters[0] instanceof Adapter\GnuFindAdapter);
$this->assertTrue($adapters[0] instanceof GnuFindAdapter);
// test that useBestAdapter method removes selection
$adapters = Finder::create()->useBestAdapter()->getAdapters();
$this->assertFalse($adapters[0] instanceof Adapter\PhpAdapter);
$this->assertFalse($adapters[0] instanceof PhpAdapter);
}
public function getTestPathData()
{
$tests = array(
return array(
array('', '', array()),
array('/^A\/B\/C/', '/C$/',
array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat'),
@ -733,20 +661,15 @@ class FinderTest extends Iterator\RealIteratorTestCase
),
),
);
return $this->buildTestData($tests);
}
/**
* @dataProvider getAdaptersTestData
*/
public function testAccessDeniedException(Adapter\AdapterInterface $adapter)
public function testAccessDeniedException()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('chmod is not supported on Windows');
}
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->files()->in(self::$tmpDir);
// make 'foo' directory non-readable
@ -776,16 +699,13 @@ class FinderTest extends Iterator\RealIteratorTestCase
}
}
/**
* @dataProvider getAdaptersTestData
*/
public function testIgnoredAccessDeniedException(Adapter\AdapterInterface $adapter)
public function testIgnoredAccessDeniedException()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('chmod is not supported on Windows');
}
$finder = $this->buildFinder($adapter);
$finder = $this->buildFinder();
$finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
// make 'foo' directory non-readable
@ -805,62 +725,18 @@ class FinderTest extends Iterator\RealIteratorTestCase
}
}
private function buildTestData(array $tests)
/**
* @return AdapterInterface
*/
protected function getAdapter()
{
$data = array();
foreach ($this->getValidAdapters() as $adapter) {
foreach ($tests as $test) {
$data[] = array_merge(array($adapter), $test);
}
}
return $data;
return new PhpAdapter();
}
private function buildFinder(Adapter\AdapterInterface $adapter)
private function buildFinder()
{
return Finder::create()
->removeAdapters()
->addAdapter($adapter);
}
private function getValidAdapters()
{
return array_filter(
array(
new Adapter\BsdFindAdapter(),
new Adapter\GnuFindAdapter(),
new Adapter\PhpAdapter(),
),
function (Adapter\AdapterInterface $adapter) {
return $adapter->isSupported();
}
);
}
/**
* Searching in multiple locations with sub directories involves
* AppendIterator which does an unnecessary rewind which leaves
* FilterIterator with inner FilesystemIterator in an invalid state.
*
* @see https://bugs.php.net/bug.php?id=49104
*/
public function testMultipleLocationsWithSubDirectories()
{
$locations = array(
__DIR__.'/Fixtures/one',
self::$tmpDir.DIRECTORY_SEPARATOR.'toto',
);
$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);
->addAdapter($this->getAdapter());
}
}

View File

@ -0,0 +1,28 @@
<?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;
use Symfony\Component\Finder\Adapter\GnuFindAdapter;
class GnuFinderTest extends FinderTest
{
protected function getAdapter()
{
$adapter = new GnuFindAdapter();
if (!$adapter->isSupported()) {
$this->markTestSkipped(get_class($adapter).' is not supported.');
}
return $adapter;
}
}

View File

@ -39,6 +39,7 @@ class DateRangeFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp',
'test.php',
'toto',
'toto/.git',
'.bar',
'.foo',
'.foo/.bar',
@ -51,6 +52,7 @@ class DateRangeFilterIteratorTest extends RealIteratorTestCase
'test.py',
'foo',
'toto',
'toto/.git',
'.bar',
'.foo',
'.foo/.bar',

View File

@ -50,6 +50,7 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp',
'test.php',
'toto',
'toto/.git',
'.foo',
'.foo/.bar',
'.bar',
@ -58,12 +59,14 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
);
$graterThanOrEqualTo1 = array(
'toto/.git',
'foo/bar.tmp',
'.foo/.bar',
'.foo/bar',
);
$equalTo1 = array(
'toto/.git',
'foo/bar.tmp',
'.foo/.bar',
'.foo/bar',

View File

@ -39,6 +39,7 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
'test.py',
'test.php',
'toto',
'toto/.git',
'foo bar',
);
@ -53,6 +54,7 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp',
'test.php',
'toto',
'toto/.git',
'foo bar',
);

View File

@ -43,6 +43,7 @@ class FileTypeFilterIteratorTest extends RealIteratorTestCase
'.git',
'foo',
'toto',
'toto/.git',
'.foo',
);

View File

@ -16,7 +16,7 @@ abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
protected function assertIterator($expected, \Traversable $iterator)
{
// set iterator_to_array $use_key to false to avoid values merge
// this made FinderTest::testAppendWithAnArray() failed with GnuFinderAdapter
// this made FinderTest::testAppendWithAnArray() fail with GnuFinderAdapter
$values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator, false));
$expected = array_map(function ($path) { return str_replace('/', DIRECTORY_SEPARATOR, $path); }, $expected);

View File

@ -31,6 +31,7 @@ abstract class RealIteratorTestCase extends IteratorTestCase
'foo/bar.tmp',
'test.php',
'toto/',
'toto/.git/',
'foo bar',
);

View File

@ -36,6 +36,7 @@ class SizeRangeFilterIteratorTest extends RealIteratorTestCase
'foo',
'test.php',
'toto',
'toto/.git',
);
return array(

View File

@ -85,6 +85,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'test.php',
'test.py',
'toto',
'toto/.git',
);
$sortByType = array(
@ -92,6 +93,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'.git',
'foo',
'toto',
'toto/.git',
'.bar',
'.foo/.bar',
'.foo/bar',
@ -113,6 +115,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'test.php',
'test.py',
'toto',
'toto/.git',
);
$sortByAccessedTime = array(
@ -127,6 +130,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'test.py',
'foo',
'toto',
'toto/.git',
'foo bar',
),
// This file was accessed after sleeping for 1 sec
@ -143,6 +147,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'foo',
'foo/bar.tmp',
'toto',
'toto/.git',
'foo bar',
),
array('test.php'),
@ -159,6 +164,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'foo',
'foo/bar.tmp',
'toto',
'toto/.git',
'foo bar',
),
array('test.php'),