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;
14 use Symfony\Component\Finder\Finder;
16 class FinderTest extends Iterator\RealIteratorTestCase
18 public function testCreate()
20 $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
23 public function testDirectories()
25 $finder = $this->buildFinder();
26 $this->assertSame($finder, $finder->directories());
27 $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
29 $finder = $this->buildFinder();
30 $finder->directories();
32 $finder->directories();
33 $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
36 public function testFiles()
38 $finder = $this->buildFinder();
39 $this->assertSame($finder, $finder->files());
40 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
42 $finder = $this->buildFinder();
44 $finder->directories();
46 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
49 public function testRemoveTrailingSlash()
51 $finder = $this->buildFinder();
53 $expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
54 $in = self::$tmpDir.'//';
56 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
59 public function testSymlinksNotResolved()
61 if ('\\' === \DIRECTORY_SEPARATOR) {
62 $this->markTestSkipped('symlinks are not supported on Windows');
65 $finder = $this->buildFinder();
67 symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
68 $expected = $this->toAbsolute(array('baz/bar.tmp'));
69 $in = self::$tmpDir.'/baz/';
71 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
72 unlink($this->toAbsolute('baz'));
73 } catch (\Exception $e) {
74 unlink($this->toAbsolute('baz'));
79 public function testBackPathNotNormalized()
81 $finder = $this->buildFinder();
83 $expected = $this->toAbsolute(array('foo/../foo/bar.tmp'));
84 $in = self::$tmpDir.'/foo/../foo/';
85 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
88 public function testDepth()
90 $finder = $this->buildFinder();
91 $this->assertSame($finder, $finder->depth('< 1'));
92 $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
94 $finder = $this->buildFinder();
95 $this->assertSame($finder, $finder->depth('<= 0'));
96 $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
98 $finder = $this->buildFinder();
99 $this->assertSame($finder, $finder->depth('>= 1'));
100 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
102 $finder = $this->buildFinder();
103 $finder->depth('< 1')->depth('>= 1');
104 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
107 public function testName()
109 $finder = $this->buildFinder();
110 $this->assertSame($finder, $finder->name('*.php'));
111 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
113 $finder = $this->buildFinder();
114 $finder->name('test.ph*');
115 $finder->name('test.py');
116 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
118 $finder = $this->buildFinder();
119 $finder->name('~^test~i');
120 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
122 $finder = $this->buildFinder();
123 $finder->name('~\\.php$~i');
124 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
126 $finder = $this->buildFinder();
127 $finder->name('test.p{hp,y}');
128 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
131 public function testNotName()
133 $finder = $this->buildFinder();
134 $this->assertSame($finder, $finder->notName('*.php'));
135 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
137 $finder = $this->buildFinder();
138 $finder->notName('*.php');
139 $finder->notName('*.py');
140 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
142 $finder = $this->buildFinder();
143 $finder->name('test.ph*');
144 $finder->name('test.py');
145 $finder->notName('*.php');
146 $finder->notName('*.py');
147 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
149 $finder = $this->buildFinder();
150 $finder->name('test.ph*');
151 $finder->name('test.py');
152 $finder->notName('*.p{hp,y}');
153 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
157 * @dataProvider getRegexNameTestData
159 public function testRegexName($regex)
161 $finder = $this->buildFinder();
162 $finder->name($regex);
163 $this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
166 public function testSize()
168 $finder = $this->buildFinder();
169 $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
170 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
173 public function testDate()
175 $finder = $this->buildFinder();
176 $this->assertSame($finder, $finder->files()->date('until last month'));
177 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
180 public function testExclude()
182 $finder = $this->buildFinder();
183 $this->assertSame($finder, $finder->exclude('foo'));
184 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
187 public function testIgnoreVCS()
189 $finder = $this->buildFinder();
190 $this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
191 $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());
193 $finder = $this->buildFinder();
194 $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
195 $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());
197 $finder = $this->buildFinder();
198 $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
199 $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());
202 public function testIgnoreDotFiles()
204 $finder = $this->buildFinder();
205 $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
206 $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());
208 $finder = $this->buildFinder();
209 $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
210 $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());
212 $finder = $this->buildFinder();
213 $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
214 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
217 public function testSortByName()
219 $finder = $this->buildFinder();
220 $this->assertSame($finder, $finder->sortByName());
221 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
224 public function testSortByType()
226 $finder = $this->buildFinder();
227 $this->assertSame($finder, $finder->sortByType());
228 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
231 public function testSortByAccessedTime()
233 $finder = $this->buildFinder();
234 $this->assertSame($finder, $finder->sortByAccessedTime());
235 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
238 public function testSortByChangedTime()
240 $finder = $this->buildFinder();
241 $this->assertSame($finder, $finder->sortByChangedTime());
242 $this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
245 public function testSortByModifiedTime()
247 $finder = $this->buildFinder();
248 $this->assertSame($finder, $finder->sortByModifiedTime());
249 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
252 public function testSort()
254 $finder = $this->buildFinder();
255 $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
256 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
259 public function testFilter()
261 $finder = $this->buildFinder();
262 $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
263 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
266 public function testFollowLinks()
268 if ('\\' == \DIRECTORY_SEPARATOR) {
269 $this->markTestSkipped('symlinks are not supported on Windows');
272 $finder = $this->buildFinder();
273 $this->assertSame($finder, $finder->followLinks());
274 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
277 public function testIn()
279 $finder = $this->buildFinder();
280 $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
283 self::$tmpDir.\DIRECTORY_SEPARATOR.'test.php',
284 __DIR__.\DIRECTORY_SEPARATOR.'FinderTest.php',
285 __DIR__.\DIRECTORY_SEPARATOR.'GlobTest.php',
288 $this->assertIterator($expected, $iterator);
292 * @expectedException \InvalidArgumentException
294 public function testInWithNonExistentDirectory()
296 $finder = new Finder();
297 $finder->in('foobar');
300 public function testInWithGlob()
302 $finder = $this->buildFinder();
303 $finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();
305 $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
309 * @expectedException \InvalidArgumentException
311 public function testInWithNonDirectoryGlob()
313 $finder = new Finder();
314 $finder->in(__DIR__.'/Fixtures/A/a*');
317 public function testInWithGlobBrace()
319 $finder = $this->buildFinder();
320 $finder->in(array(__DIR__.'/Fixtures/{A,copy/A}/B/C'))->getIterator();
322 $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
326 * @expectedException \LogicException
328 public function testGetIteratorWithoutIn()
330 $finder = Finder::create();
331 $finder->getIterator();
334 public function testGetIterator()
336 $finder = $this->buildFinder();
338 foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
339 $dirs[] = (string) $dir;
342 $expected = $this->toAbsolute(array('foo', 'toto'));
347 $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
349 $finder = $this->buildFinder();
350 $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
352 $finder = $this->buildFinder();
353 $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
354 $a = array_values(array_map('strval', $a));
356 $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
359 public function testRelativePath()
361 $finder = $this->buildFinder()->in(self::$tmpDir);
365 foreach ($finder as $file) {
366 $paths[] = $file->getRelativePath();
369 $ref = array('', '', '', '', 'foo', '');
374 $this->assertEquals($ref, $paths);
377 public function testRelativePathname()
379 $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
383 foreach ($finder as $file) {
384 $paths[] = $file->getRelativePathname();
387 $ref = array('test.php', 'toto', 'test.py', 'foo', 'foo'.\DIRECTORY_SEPARATOR.'bar.tmp', 'foo bar');
392 $this->assertEquals($ref, $paths);
395 public function testAppendWithAFinder()
397 $finder = $this->buildFinder();
398 $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
400 $finder1 = $this->buildFinder();
401 $finder1->directories()->in(self::$tmpDir);
403 $finder = $finder->append($finder1);
405 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
408 public function testAppendWithAnArray()
410 $finder = $this->buildFinder();
411 $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
413 $finder->append($this->toAbsolute(array('foo', 'toto')));
415 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
418 public function testAppendReturnsAFinder()
420 $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append(array()));
423 public function testAppendDoesNotRequireIn()
425 $finder = $this->buildFinder();
426 $finder->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
428 $finder1 = Finder::create()->append($finder);
430 $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
433 public function testCountDirectories()
435 $directory = Finder::create()->directories()->in(self::$tmpDir);
438 foreach ($directory as $dir) {
442 $this->assertCount($i, $directory);
445 public function testCountFiles()
447 $files = Finder::create()->files()->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
450 foreach ($files as $file) {
454 $this->assertCount($i, $files);
458 * @expectedException \LogicException
460 public function testCountWithoutIn()
462 $finder = Finder::create()->files();
466 public function testHasResults()
468 $finder = $this->buildFinder();
469 $finder->in(__DIR__);
470 $this->assertTrue($finder->hasResults());
473 public function testNoResults()
475 $finder = $this->buildFinder();
476 $finder->in(__DIR__)->name('DoesNotExist');
477 $this->assertFalse($finder->hasResults());
481 * @dataProvider getContainsTestData
483 public function testContains($matchPatterns, $noMatchPatterns, $expected)
485 $finder = $this->buildFinder();
486 $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
487 ->name('*.txt')->sortByName()
488 ->contains($matchPatterns)
489 ->notContains($noMatchPatterns);
491 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
494 public function testContainsOnDirectory()
496 $finder = $this->buildFinder();
501 $this->assertIterator(array(), $finder);
504 public function testNotContainsOnDirectory()
506 $finder = $this->buildFinder();
510 ->notContains('abc');
511 $this->assertIterator(array(), $finder);
515 * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
516 * with inner FilesystemIterator in an invalid state.
518 * @see https://bugs.php.net/68557
520 public function testMultipleLocations()
524 self::$tmpDir.'/toto/',
527 // it is expected that there are test.py test.php in the tmpDir
528 $finder = new Finder();
529 $finder->in($locations)
530 // the default flag IGNORE_DOT_FILES fixes the problem indirectly
531 // so we set it to false for better isolation
532 ->ignoreDotFiles(false)
533 ->depth('< 1')->name('test.php');
535 $this->assertCount(1, $finder);
539 * Searching in multiple locations with sub directories involves
540 * AppendIterator which does an unnecessary rewind which leaves
541 * FilterIterator with inner FilesystemIterator in an invalid state.
543 * @see https://bugs.php.net/68557
545 public function testMultipleLocationsWithSubDirectories()
548 __DIR__.'/Fixtures/one',
549 self::$tmpDir.\DIRECTORY_SEPARATOR.'toto',
552 $finder = $this->buildFinder();
553 $finder->in($locations)->depth('< 10')->name('*.neon');
556 __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
557 __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
560 $this->assertIterator($expected, $finder);
561 $this->assertIteratorInForeach($expected, $finder);
565 * Iterator keys must be the file pathname.
567 public function testIteratorKeys()
569 $finder = $this->buildFinder()->in(self::$tmpDir);
570 foreach ($finder as $key => $file) {
571 $this->assertEquals($file->getPathname(), $key);
575 public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
577 $finder = $this->buildFinder();
578 $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
581 $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');
582 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
585 public function getContainsTestData()
588 array('', '', array()),
589 array('foo', 'bar', array()),
590 array('', 'foobar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
591 array('lorem ipsum dolor sit amet', 'foobar', array('lorem.txt')),
592 array('sit', 'bar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
593 array('dolor sit amet', '@^L@m', array('dolor.txt', 'ipsum.txt')),
594 array('/^lorem ipsum dolor sit amet$/m', 'foobar', array('lorem.txt')),
595 array('lorem', 'foobar', array('lorem.txt')),
596 array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
597 array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
601 public function getRegexNameTestData()
604 array('~.+\\.p.+~i'),
610 * @dataProvider getTestPathData
612 public function testPath($matchPatterns, $noMatchPatterns, array $expected)
614 $finder = $this->buildFinder();
615 $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
616 ->path($matchPatterns)
617 ->notPath($noMatchPatterns);
619 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
622 public function getTestPathData()
625 array('', '', array()),
626 array('/^A\/B\/C/', '/C$/',
627 array('A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat'),
629 array('/^A\/B/', 'foobar',
631 'A'.\DIRECTORY_SEPARATOR.'B',
632 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
633 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
634 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
637 array('A/B/C', 'foobar',
639 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
640 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
641 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
642 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
645 array('A/B', 'foobar',
648 'A'.\DIRECTORY_SEPARATOR.'B',
649 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
650 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B',
651 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
653 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
654 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
655 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat.copy',
656 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
659 array('/^with space\//', 'foobar',
661 'with space'.\DIRECTORY_SEPARATOR.'foo.txt',
667 public function testAccessDeniedException()
669 if ('\\' === \DIRECTORY_SEPARATOR) {
670 $this->markTestSkipped('chmod is not supported on Windows');
673 $finder = $this->buildFinder();
674 $finder->files()->in(self::$tmpDir);
676 // make 'foo' directory non-readable
677 $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
678 chmod($testDir, 0333);
680 if (false === $couldRead = is_readable($testDir)) {
682 $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
683 $this->fail('Finder should throw an exception when opening a non-readable directory.');
684 } catch (\Exception $e) {
685 $expectedExceptionClass = 'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
686 if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
687 $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
690 if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
691 $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
694 $this->assertInstanceOf($expectedExceptionClass, $e);
698 // restore original permissions
699 chmod($testDir, 0777);
700 clearstatcache($testDir);
703 $this->markTestSkipped('could read test files while test requires unreadable');
707 public function testIgnoredAccessDeniedException()
709 if ('\\' === \DIRECTORY_SEPARATOR) {
710 $this->markTestSkipped('chmod is not supported on Windows');
713 $finder = $this->buildFinder();
714 $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
716 // make 'foo' directory non-readable
717 $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
718 chmod($testDir, 0333);
720 if (false === ($couldRead = is_readable($testDir))) {
721 $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
724 // restore original permissions
725 chmod($testDir, 0777);
726 clearstatcache($testDir);
729 $this->markTestSkipped('could read test files while test requires unreadable');
733 protected function buildFinder()
735 return Finder::create();