12 namespace Symfony\Component\Finder\Tests;
15 use Symfony\Component\Finder\Adapter;
21 $this->assertInstanceOf(
'Symfony\Component\Finder\Finder',
Finder::create());
30 $this->assertSame($finder, $finder->directories());
34 $finder->directories();
36 $finder->directories();
46 $this->assertSame($finder, $finder->files());
47 $this->
assertIterator($this->
toAbsolute(array(
'foo/bar.tmp',
'test.php',
'test.py',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
51 $finder->directories();
53 $this->
assertIterator($this->
toAbsolute(array(
'foo/bar.tmp',
'test.php',
'test.py',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
62 $this->assertSame($finder, $finder->depth(
'< 1'));
63 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'test.php',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
66 $this->assertSame($finder, $finder->depth(
'<= 0'));
67 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'test.php',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
70 $this->assertSame($finder, $finder->depth(
'>= 1'));
74 $finder->depth(
'< 1')->depth(
'>= 1');
75 $this->
assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
84 $this->assertSame($finder, $finder->name(
'*.php'));
88 $finder->name(
'test.ph*');
89 $finder->name(
'test.py');
93 $finder->name(
'~^test~i');
97 $finder->name(
'~\\.php$~i');
101 $finder->name(
'test.p{hp,y}');
111 $this->assertSame($finder, $finder->notName(
'*.php'));
112 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
115 $finder->notName(
'*.php');
116 $finder->notName(
'*.py');
117 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo/bar.tmp',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
120 $finder->name(
'test.ph*');
121 $finder->name(
'test.py');
122 $finder->notName(
'*.php');
123 $finder->notName(
'*.py');
124 $this->
assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
127 $finder->name(
'test.ph*');
128 $finder->name(
'test.py');
129 $finder->notName(
'*.p{hp,y}');
130 $this->
assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
139 $finder->name($regex);
149 $this->assertSame($finder, $finder->files()->size(
'< 1K')->size(
'> 500'));
159 $this->assertSame($finder, $finder->files()->date(
'until last month'));
160 $this->
assertIterator($this->
toAbsolute(array(
'foo/bar.tmp',
'test.php')), $finder->in(self::$tmpDir)->getIterator());
169 $this->assertSame($finder, $finder->exclude(
'foo'));
170 $this->
assertIterator($this->
toAbsolute(array(
'test.php',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
179 $this->assertSame($finder, $finder->ignoreVCS(
false)->ignoreDotFiles(
false));
180 $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());
183 $finder->ignoreVCS(
false)->ignoreVCS(
false)->ignoreDotFiles(
false);
184 $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());
187 $this->assertSame($finder, $finder->ignoreVCS(
true)->ignoreDotFiles(
false));
188 $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());
197 $this->assertSame($finder, $finder->ignoreDotFiles(
false)->ignoreVCS(
false));
198 $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());
201 $finder->ignoreDotFiles(
false)->ignoreDotFiles(
false)->ignoreVCS(
false);
202 $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());
205 $this->assertSame($finder, $finder->ignoreDotFiles(
true)->ignoreVCS(
false));
206 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
215 $this->assertSame($finder, $finder->sortByName());
216 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'test.php',
'test.py',
'toto')), $finder->in(self::$tmpDir)->getIterator());
225 $this->assertSame($finder, $finder->sortByType());
226 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo bar',
'toto',
'foo/bar.tmp',
'test.php',
'test.py')), $finder->in(self::$tmpDir)->getIterator());
235 $this->assertSame($finder, $finder->sortByAccessedTime());
236 $this->
assertIterator($this->
toAbsolute(array(
'foo/bar.tmp',
'test.php',
'toto',
'test.py',
'foo',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
245 $this->assertSame($finder, $finder->sortByChangedTime());
246 $this->
assertIterator($this->
toAbsolute(array(
'toto',
'test.py',
'test.php',
'foo/bar.tmp',
'foo',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
255 $this->assertSame($finder, $finder->sortByModifiedTime());
256 $this->
assertIterator($this->
toAbsolute(array(
'foo/bar.tmp',
'test.php',
'toto',
'test.py',
'foo',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
266 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'test.php',
'test.py',
'toto')), $finder->in(self::$tmpDir)->getIterator());
275 $this->assertSame($finder, $finder->filter(
function (\
SplFileInfo $f) {
return false !== strpos($f,
'test'); }));
284 if (
'\\' == DIRECTORY_SEPARATOR) {
289 $this->assertSame($finder, $finder->followLinks());
290 $this->
assertIterator($this->
toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'foo bar')), $finder->in(self::$tmpDir)->getIterator());
300 $finder->in(
'foobar');
301 $this->fail(
'->in() throws a \InvalidArgumentException if the directory does not exist');
302 }
catch (\Exception $e) {
303 $this->assertInstanceOf(
'InvalidArgumentException', $e,
'->in() throws a \InvalidArgumentException if the directory does not exist');
307 $iterator = $finder->files()->name(
'*.php')->depth(
'< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
309 $this->
assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.
'test.php', __DIR__.DIRECTORY_SEPARATOR.
'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.
'GlobTest.php'), $iterator);
318 $finder->in(array(__DIR__.
'/Fixtures/*/B/C', __DIR__.
'/Fixtures/*/*/B/C'))->getIterator();
330 $finder->in(__DIR__.
'/Fixtures/A/a*');
339 $finder->in(array(__DIR__.
'/Fixtures/{A,copy/A}/B/C'))->getIterator();
351 $finder->getIterator();
352 $this->fail(
'->getIterator() throws a \LogicException if the in() method has not been called');
353 }
catch (\Exception $e) {
354 $this->assertInstanceOf(
'LogicException', $e,
'->getIterator() throws a \LogicException if the in() method has not been called');
359 foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
360 $dirs[] = (string) $dir;
363 $expected = $this->
toAbsolute(array(
'foo',
'toto'));
368 $this->assertEquals($expected, $dirs,
'implements the \IteratorAggregate interface');
371 $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)),
'implements the \IteratorAggregate interface');
374 $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
375 $a = array_values(array_map(
function ($a) {
return (
string) $a; }, $a));
377 $this->assertEquals($expected, $a,
'implements the \IteratorAggregate interface');
385 $finder = $this->
buildFinder($adapter)->in(self::$tmpDir);
389 foreach ($finder as $file) {
390 $paths[] = $file->getRelativePath();
393 $ref = array(
'',
'',
'',
'',
'foo',
'');
398 $this->assertEquals($ref, $paths);
406 $finder = $this->
buildFinder($adapter)->in(self::$tmpDir)->sortByName();
410 foreach ($finder as $file) {
411 $paths[] = $file->getRelativePathname();
414 $ref = array(
'test.php',
'toto',
'test.py',
'foo',
'foo'.DIRECTORY_SEPARATOR.
'bar.tmp',
'foo bar');
419 $this->assertEquals($ref, $paths);
428 $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.
'foo');
431 $finder1->directories()->in(self::$tmpDir);
433 $finder = $finder->append($finder1);
444 $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.
'foo');
446 $finder->append($this->
toAbsolute(array(
'foo',
'toto')));
456 $this->assertInstanceOf(
'Symfony\\Component\\Finder\\Finder', $this->
buildFinder($adapter)->append(array()));
465 $finder->in(self::$tmpDir.DIRECTORY_SEPARATOR.
'foo');
469 $this->
assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
477 foreach ($directory as $dir) {
481 $this->assertCount($i, $directory);
489 foreach (
$files as $file) {
493 $this->assertCount($i,
$files);
508 public function testContains($adapter, $matchPatterns, $noMatchPatterns, $expected)
511 $finder->in(__DIR__.DIRECTORY_SEPARATOR.
'Fixtures')
512 ->name(
'*.txt')->sortByName()
513 ->contains($matchPatterns)
514 ->notContains($noMatchPatterns);
541 ->notContains(
'abc');
557 self::$tmpDir.
'/toto/',
562 $finder->in($locations)->depth(
'< 1')->name(
'test.php');
564 $this->assertCount(1, $finder);
574 $finder = $this->
buildFinder($adapter)->in(self::$tmpDir);
575 foreach ($finder as $key => $file) {
576 $this->assertEquals($file->getPathname(), $key);
586 $finder->in(__DIR__.DIRECTORY_SEPARATOR.
'Fixtures'.DIRECTORY_SEPARATOR.
'r+e.gex[c]a(r)s')
589 $expected = array(
'r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.
'dir',
590 'r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.
'dir'.DIRECTORY_SEPARATOR.
'bar.dat',);
598 ->addAdapter(
new FakeAdapter\NamedAdapter(
'a'), 0)
599 ->addAdapter(
new FakeAdapter\NamedAdapter(
'b'), -50)
600 ->addAdapter(
new FakeAdapter\NamedAdapter(
'c'), 50)
601 ->addAdapter(
new FakeAdapter\NamedAdapter(
'd'), -25)
602 ->addAdapter(
new FakeAdapter\NamedAdapter(
'e'), 25);
605 array(
'c',
'e',
'a',
'd',
'b'),
607 return $adapter->getName();
608 }, $finder->getAdapters())
614 $iterator = new \ArrayIterator(array());
615 $filenames = $this->
toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto'));
616 foreach ($filenames as $file) {
622 ->addAdapter(
new FakeAdapter\UnsupportedAdapter(), 3)
623 ->addAdapter(
new FakeAdapter\FailingAdapter(), 2)
624 ->addAdapter(
new FakeAdapter\DummyAdapter($iterator), 1);
626 $this->
assertIterator($filenames, $finder->in(sys_get_temp_dir())->getIterator());
632 function ($adapter) {
return array($adapter); },
640 array(
'',
'', array()),
641 array(
'foo',
'bar', array()),
642 array(
'',
'foobar', array(
'dolor.txt',
'ipsum.txt',
'lorem.txt')),
643 array(
'lorem ipsum dolor sit amet',
'foobar', array(
'lorem.txt')),
644 array(
'sit',
'bar', array(
'dolor.txt',
'ipsum.txt',
'lorem.txt')),
645 array(
'dolor sit amet',
'@^L@m', array(
'dolor.txt',
'ipsum.txt')),
646 array(
'/^lorem ipsum dolor sit amet$/m',
'foobar', array(
'lorem.txt')),
647 array(
'lorem',
'foobar', array(
'lorem.txt')),
648 array(
'',
'lorem', array(
'dolor.txt',
'ipsum.txt')),
649 array(
'ipsum dolor sit amet',
'/^IPSUM/m', array(
'lorem.txt')),
658 array(
'~.+\\.p.+~i'),
671 $finder->in(__DIR__.DIRECTORY_SEPARATOR.
'Fixtures')
672 ->path($matchPatterns)
673 ->notPath($noMatchPatterns);
682 $this->assertTrue($adapters[0] instanceof Adapter\
PhpAdapter);
685 $adapters =
Finder::create()->setAdapter(
'gnu_find')->getAdapters();
686 $this->assertTrue($adapters[0] instanceof Adapter\
GnuFindAdapter);
690 $this->assertFalse($adapters[0] instanceof Adapter\PhpAdapter);
696 array(
'',
'', array()),
697 array(
'/^A\/B\/C/',
'/C$/',
698 array(
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat'),
700 array(
'/^A\/B/',
'foobar',
702 'A'.DIRECTORY_SEPARATOR.
'B',
703 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C',
704 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'ab.dat',
705 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat',
708 array(
'A/B/C',
'foobar',
710 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C',
711 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat',
712 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C',
713 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat.copy',
716 array(
'A/B',
'foobar',
719 'A'.DIRECTORY_SEPARATOR.
'B',
720 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C',
721 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B',
722 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C',
724 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'ab.dat',
725 'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat',
726 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'ab.dat.copy',
727 'copy'.DIRECTORY_SEPARATOR.
'A'.DIRECTORY_SEPARATOR.
'B'.DIRECTORY_SEPARATOR.
'C'.DIRECTORY_SEPARATOR.
'abc.dat.copy',
730 array(
'/^with space\//',
'foobar',
732 'with space'.DIRECTORY_SEPARATOR.
'foo.txt',
745 if (
'\\' === DIRECTORY_SEPARATOR) {
746 $this->markTestSkipped(
'chmod is not supported on Windows');
750 $finder->files()->in(self::$tmpDir);
753 $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.
'foo';
754 chmod($testDir, 0333);
756 if (
false === $couldRead = is_readable($testDir)) {
759 $this->fail(
'Finder should throw an exception when opening a non-readable directory.');
760 }
catch (\Exception $e) {
761 $expectedExceptionClass =
'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
762 if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
763 $this->fail(sprintf(
"Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass,
'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
766 $this->assertInstanceOf($expectedExceptionClass, $e);
771 chmod($testDir, 0777);
772 clearstatcache($testDir);
775 $this->markTestSkipped(
'could read test files while test requires unreadable');
784 if (
'\\' === DIRECTORY_SEPARATOR) {
785 $this->markTestSkipped(
'chmod is not supported on Windows');
789 $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
792 $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.
'foo';
793 chmod($testDir, 0333);
795 if (
false === ($couldRead = is_readable($testDir))) {
800 chmod($testDir, 0777);
801 clearstatcache($testDir);
804 $this->markTestSkipped(
'could read test files while test requires unreadable');
812 foreach ($tests as $test) {
813 $data[] = array_merge(array($adapter), $test);
824 ->addAdapter($adapter);
836 return $adapter->isSupported();
851 __DIR__.
'/Fixtures/one',
852 self::$tmpDir.DIRECTORY_SEPARATOR.
'toto',
856 $finder->in($locations)->depth(
'< 10')->name(
'*.neon');
859 __DIR__.
'/Fixtures/one'.DIRECTORY_SEPARATOR.
'b'.DIRECTORY_SEPARATOR.
'c.neon',
860 __DIR__.
'/Fixtures/one'.DIRECTORY_SEPARATOR.
'b'.DIRECTORY_SEPARATOR.
'd.neon',