2010-04-21 08:31:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-21 08:31:18 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-21 08:31:18 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Finder\Tests;
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2010-04-21 08:31:18 +01:00
|
|
|
|
|
|
|
class FinderTest extends Iterator\RealIteratorTestCase
|
|
|
|
{
|
2012-07-09 13:50:58 +01:00
|
|
|
protected static $tmpDir;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-07-09 13:50:58 +01:00
|
|
|
public static function setUpBeforeClass()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
2010-11-17 22:42:21 +00:00
|
|
|
self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 13:17:22 +01:00
|
|
|
public function testCreate()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testDirectories()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->directories());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->directories();
|
|
|
|
$finder->files();
|
|
|
|
$finder->directories();
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
public function testFiles()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->files());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->files();
|
|
|
|
$finder->directories();
|
|
|
|
$finder->files();
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-21 12:22:34 +01:00
|
|
|
public function testDepth()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2010-05-21 12:22:34 +01:00
|
|
|
$this->assertSame($finder, $finder->depth('< 1'));
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
2010-05-21 12:22:34 +01:00
|
|
|
$this->assertSame($finder, $finder->depth('<= 0'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->depth('>= 1'));
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
2010-05-21 12:22:34 +01:00
|
|
|
$finder->depth('< 1')->depth('>= 1');
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testName()
|
2010-05-04 10:34:29 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->name('*.php'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->name('test.ph*');
|
|
|
|
$finder->name('test.py');
|
|
|
|
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
2010-05-04 10:34:29 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
public function testNotName()
|
2010-05-04 10:34:29 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->notName('*.php'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->notName('*.php');
|
|
|
|
$finder->notName('*.py');
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->name('test.ph*');
|
|
|
|
$finder->name('test.py');
|
|
|
|
$finder->notName('*.php');
|
|
|
|
$finder->notName('*.py');
|
|
|
|
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
2010-05-04 10:34:29 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testSize()
|
2010-05-04 10:34:29 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
2010-05-04 10:34:29 +01:00
|
|
|
}
|
|
|
|
|
2010-05-21 11:50:13 +01:00
|
|
|
public function testDate()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->files()->date('until last month'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testExclude()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->exclude('foo'));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
2010-05-04 10:34:29 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testIgnoreVCS()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2012-05-06 14:25:22 +01:00
|
|
|
$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')), $finder->in(self::$tmpDir)->getIterator());
|
2010-05-04 10:34:29 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
2012-05-06 14:25:22 +01:00
|
|
|
$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')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$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')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-05-04 10:34:29 +01:00
|
|
|
|
2011-05-23 09:45:59 +01:00
|
|
|
public function testIgnoreDotFiles()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
|
2012-05-06 14:25:22 +01:00
|
|
|
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
2012-05-18 18:41:48 +01:00
|
|
|
|
2012-05-06 14:25:22 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
|
|
|
|
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
2011-05-23 09:45:59 +01:00
|
|
|
|
|
|
|
$finder = new Finder();
|
2012-05-06 14:25:22 +01:00
|
|
|
$this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
|
2011-05-23 09:45:59 +01:00
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
2012-05-06 14:25:22 +01:00
|
|
|
|
2011-05-23 09:45:59 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testSortByName()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sortByName());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testSortByType()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sortByType());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
2012-03-31 19:44:50 +01:00
|
|
|
public function testSortByAccessedTime()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sortByAccessedTime());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortByChangedTime()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sortByChangedTime());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortByModifiedTime()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sortByModifiedTime());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testSort()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilter()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
|
|
|
|
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFollowLinks()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('\\' == DIRECTORY_SEPARATOR) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertSame($finder, $finder->followLinks());
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIn()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder->in('foobar');
|
|
|
|
$this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
2010-05-21 12:22:34 +01:00
|
|
|
$iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'bootstrap.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIterator()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$finder->getIterator();
|
|
|
|
$this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$dirs = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$dirs[] = (string) $dir;
|
|
|
|
}
|
|
|
|
|
2010-08-12 16:25:53 +01:00
|
|
|
$expected = $this->toAbsolute(array('foo', 'toto'));
|
|
|
|
|
|
|
|
sort($dirs);
|
|
|
|
sort($expected);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$a = iterator_to_array($finder->directories()->in(self::$tmpDir));
|
2010-08-12 16:25:53 +01:00
|
|
|
$a = array_values(array_map(function ($a) { return (string) $a; }, $a));
|
|
|
|
sort($a);
|
|
|
|
$this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-02-15 23:18:23 +00:00
|
|
|
public function testRelativePath()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
|
|
|
|
$finder->in(self::$tmpDir);
|
|
|
|
|
|
|
|
$paths = array();
|
|
|
|
|
2012-05-21 21:27:15 +01:00
|
|
|
foreach ($finder as $file) {
|
2011-02-15 23:18:23 +00:00
|
|
|
$paths[] = $file->getRelativePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ref = array("", "", "", "", "foo");
|
|
|
|
|
|
|
|
sort($ref);
|
|
|
|
sort($paths);
|
|
|
|
|
|
|
|
$this->assertEquals($paths, $ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRelativePathname()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
|
|
|
|
$finder->in(self::$tmpDir)->sortByName();
|
|
|
|
|
|
|
|
$paths = array();
|
|
|
|
|
2012-05-21 21:27:15 +01:00
|
|
|
foreach ($finder as $file) {
|
2011-02-15 23:18:23 +00:00
|
|
|
$paths[] = $file->getRelativePathname();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
|
|
|
|
|
|
|
|
sort($paths);
|
|
|
|
sort($ref);
|
|
|
|
|
|
|
|
$this->assertEquals($paths, $ref);
|
|
|
|
}
|
|
|
|
|
2011-03-25 14:50:47 +00:00
|
|
|
public function testAppendWithAFinder()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2011-03-29 00:31:22 +01:00
|
|
|
$finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
|
2011-03-25 14:50:47 +00:00
|
|
|
|
|
|
|
$finder1 = new Finder();
|
|
|
|
$finder1->directories()->in(self::$tmpDir);
|
|
|
|
|
|
|
|
$finder->append($finder1);
|
|
|
|
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendWithAnArray()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
2011-03-29 00:31:22 +01:00
|
|
|
$finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
|
2011-03-25 14:50:47 +00:00
|
|
|
|
|
|
|
$finder->append($this->toAbsolute(array('foo', 'toto')));
|
|
|
|
|
|
|
|
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
public function testCountDirectories()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$directory = $finder->directories()->in(self::$tmpDir);
|
|
|
|
$i = 0;
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
foreach ($directory as $dir) {
|
|
|
|
$i++;
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
$this->assertCount($i, $directory);
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
public function testCountFiles()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$files = $finder->files()->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures');
|
|
|
|
$i = 0;
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
$i++;
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
$this->assertCount($i, $files);
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-04-22 15:13:31 +01:00
|
|
|
public function testCountWithoutIn()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->files();
|
|
|
|
|
|
|
|
try {
|
|
|
|
count($finder);
|
|
|
|
$this->fail('Countable makes use of the getIterator command');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertInstanceOf('LogicException', $e, '->getIterator() throws \LogicException when no logic has been entered');
|
|
|
|
}
|
|
|
|
}
|
2011-03-25 14:50:47 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function toAbsolute($files)
|
|
|
|
{
|
|
|
|
$f = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($files as $file) {
|
2010-11-17 22:42:21 +00:00
|
|
|
$f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $f;
|
|
|
|
}
|
2012-04-19 16:34:50 +01:00
|
|
|
|
|
|
|
protected function toAbsoluteFixtures($files)
|
|
|
|
{
|
|
|
|
$f = array();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$f[] = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-19 17:19:17 +01:00
|
|
|
* @dataProvider getContainsTestData
|
2012-04-19 16:34:50 +01:00
|
|
|
*/
|
2012-04-19 17:19:17 +01:00
|
|
|
public function testContains($matchPatterns, $noMatchPatterns, $expected)
|
2012-04-19 16:34:50 +01:00
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
|
|
|
|
->name('*.txt')->sortByName()
|
2012-04-19 17:19:17 +01:00
|
|
|
->contains($matchPatterns)
|
|
|
|
->notContains($noMatchPatterns);
|
2012-04-19 16:34:50 +01:00
|
|
|
|
|
|
|
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
|
|
|
|
}
|
|
|
|
|
2012-04-19 17:19:17 +01:00
|
|
|
public function getContainsTestData()
|
2012-04-19 16:34:50 +01:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('', '', array()),
|
|
|
|
array('foo', 'bar', array()),
|
|
|
|
array('', 'foobar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
|
|
|
|
array('lorem ipsum dolor sit amet', 'foobar', array('lorem.txt')),
|
|
|
|
array('sit', 'bar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
|
|
|
|
array('dolor sit amet', '@^L@m', array('dolor.txt', 'ipsum.txt')),
|
|
|
|
array('/^lorem ipsum dolor sit amet$/m', 'foobar', array('lorem.txt')),
|
|
|
|
array('lorem', 'foobar', array('lorem.txt')),
|
|
|
|
|
|
|
|
array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
|
|
|
|
array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-21 15:08:44 +01:00
|
|
|
public function testContainsOnDirectory()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->in(__DIR__)
|
|
|
|
->directories()
|
|
|
|
->name('Fixtures')
|
|
|
|
->contains('abc');
|
|
|
|
$this->assertIterator(array(), $finder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotContainsOnDirectory()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->in(__DIR__)
|
|
|
|
->directories()
|
|
|
|
->name('Fixtures')
|
|
|
|
->notContains('abc');
|
|
|
|
$this->assertIterator(array(), $finder);
|
|
|
|
}
|
|
|
|
|
2012-07-20 11:02:47 +01:00
|
|
|
/**
|
|
|
|
* Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
|
|
|
|
* with inner FilesystemIterator in an ivalid state.
|
2012-07-27 08:28:24 +01:00
|
|
|
*
|
|
|
|
* @see https://bugs.php.net/bug.php?id=49104
|
2012-07-20 11:02:47 +01:00
|
|
|
*/
|
2012-07-27 08:28:24 +01:00
|
|
|
public function testMultipleLocations()
|
|
|
|
{
|
|
|
|
$locations = array(
|
|
|
|
self::$tmpDir.'/',
|
|
|
|
self::$tmpDir.'/toto/',
|
2012-07-20 11:02:47 +01:00
|
|
|
);
|
2012-07-27 08:28:24 +01:00
|
|
|
|
2012-07-20 11:02:47 +01:00
|
|
|
// it is expected that there are test.py test.php in the tmpDir
|
2012-07-27 08:28:24 +01:00
|
|
|
$finder = new Finder();
|
2012-07-20 11:02:47 +01:00
|
|
|
$finder->in($locations)->depth('< 1')->name('test.php');
|
|
|
|
|
2012-07-27 08:28:24 +01:00
|
|
|
$this->assertEquals(1, count($finder));
|
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|