TYPO3  7.6
FinderTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Finder\Tests;
13 
15 use Symfony\Component\Finder\Adapter;
16 
18 {
19  public function testCreate()
20  {
21  $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
22  }
23 
27  public function testDirectories($adapter)
28  {
29  $finder = $this->buildFinder($adapter);
30  $this->assertSame($finder, $finder->directories());
31  $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
32 
33  $finder = $this->buildFinder($adapter);
34  $finder->directories();
35  $finder->files();
36  $finder->directories();
37  $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
38  }
39 
43  public function testFiles($adapter)
44  {
45  $finder = $this->buildFinder($adapter);
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());
48 
49  $finder = $this->buildFinder($adapter);
50  $finder->files();
51  $finder->directories();
52  $finder->files();
53  $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
54  }
55 
59  public function testDepth($adapter)
60  {
61  $finder = $this->buildFinder($adapter);
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());
64 
65  $finder = $this->buildFinder($adapter);
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());
68 
69  $finder = $this->buildFinder($adapter);
70  $this->assertSame($finder, $finder->depth('>= 1'));
71  $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
72 
73  $finder = $this->buildFinder($adapter);
74  $finder->depth('< 1')->depth('>= 1');
75  $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
76  }
77 
81  public function testName($adapter)
82  {
83  $finder = $this->buildFinder($adapter);
84  $this->assertSame($finder, $finder->name('*.php'));
85  $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
86 
87  $finder = $this->buildFinder($adapter);
88  $finder->name('test.ph*');
89  $finder->name('test.py');
90  $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
91 
92  $finder = $this->buildFinder($adapter);
93  $finder->name('~^test~i');
94  $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
95 
96  $finder = $this->buildFinder($adapter);
97  $finder->name('~\\.php$~i');
98  $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
99 
100  $finder = $this->buildFinder($adapter);
101  $finder->name('test.p{hp,y}');
102  $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
103  }
104 
108  public function testNotName($adapter)
109  {
110  $finder = $this->buildFinder($adapter);
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());
113 
114  $finder = $this->buildFinder($adapter);
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());
118 
119  $finder = $this->buildFinder($adapter);
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());
125 
126  $finder = $this->buildFinder($adapter);
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());
131  }
132 
136  public function testRegexName($adapter, $regex)
137  {
138  $finder = $this->buildFinder($adapter);
139  $finder->name($regex);
140  $this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
141  }
142 
146  public function testSize($adapter)
147  {
148  $finder = $this->buildFinder($adapter);
149  $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
150  $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
151  }
152 
156  public function testDate($adapter)
157  {
158  $finder = $this->buildFinder($adapter);
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());
161  }
162 
166  public function testExclude($adapter)
167  {
168  $finder = $this->buildFinder($adapter);
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());
171  }
172 
176  public function testIgnoreVCS($adapter)
177  {
178  $finder = $this->buildFinder($adapter);
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());
181 
182  $finder = $this->buildFinder($adapter);
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());
185 
186  $finder = $this->buildFinder($adapter);
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());
189  }
190 
194  public function testIgnoreDotFiles($adapter)
195  {
196  $finder = $this->buildFinder($adapter);
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());
199 
200  $finder = $this->buildFinder($adapter);
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());
203 
204  $finder = $this->buildFinder($adapter);
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());
207  }
208 
212  public function testSortByName($adapter)
213  {
214  $finder = $this->buildFinder($adapter);
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());
217  }
218 
222  public function testSortByType($adapter)
223  {
224  $finder = $this->buildFinder($adapter);
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());
227  }
228 
232  public function testSortByAccessedTime($adapter)
233  {
234  $finder = $this->buildFinder($adapter);
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());
237  }
238 
242  public function testSortByChangedTime($adapter)
243  {
244  $finder = $this->buildFinder($adapter);
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());
247  }
248 
252  public function testSortByModifiedTime($adapter)
253  {
254  $finder = $this->buildFinder($adapter);
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());
257  }
258 
262  public function testSort($adapter)
263  {
264  $finder = $this->buildFinder($adapter);
265  $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
266  $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
267  }
268 
272  public function testFilter($adapter)
273  {
274  $finder = $this->buildFinder($adapter);
275  $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
276  $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
277  }
278 
282  public function testFollowLinks($adapter)
283  {
284  if ('\\' == DIRECTORY_SEPARATOR) {
285  return;
286  }
287 
288  $finder = $this->buildFinder($adapter);
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());
291  }
292 
296  public function testIn($adapter)
297  {
298  $finder = $this->buildFinder($adapter);
299  try {
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');
304  }
305 
306  $finder = $this->buildFinder($adapter);
307  $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
308 
309  $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
310  }
311 
315  public function testInWithGlob($adapter)
316  {
317  $finder = $this->buildFinder($adapter);
318  $finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
319 
320  $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
321  }
322 
327  public function testInWithNonDirectoryGlob($adapter)
328  {
329  $finder = $this->buildFinder($adapter);
330  $finder->in(__DIR__.'/Fixtures/A/a*');
331  }
332 
336  public function testInWithGlobBrace($adapter)
337  {
338  $finder = $this->buildFinder($adapter);
339  $finder->in(array(__DIR__.'/Fixtures/{A,copy/A}/B/C'))->getIterator();
340 
341  $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
342  }
343 
347  public function testGetIterator($adapter)
348  {
349  $finder = $this->buildFinder($adapter);
350  try {
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');
355  }
356 
357  $finder = $this->buildFinder($adapter);
358  $dirs = array();
359  foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
360  $dirs[] = (string) $dir;
361  }
362 
363  $expected = $this->toAbsolute(array('foo', 'toto'));
364 
365  sort($dirs);
366  sort($expected);
367 
368  $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
369 
370  $finder = $this->buildFinder($adapter);
371  $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
372 
373  $finder = $this->buildFinder($adapter);
374  $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
375  $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
376  sort($a);
377  $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
378  }
379 
383  public function testRelativePath($adapter)
384  {
385  $finder = $this->buildFinder($adapter)->in(self::$tmpDir);
386 
387  $paths = array();
388 
389  foreach ($finder as $file) {
390  $paths[] = $file->getRelativePath();
391  }
392 
393  $ref = array('', '', '', '', 'foo', '');
394 
395  sort($ref);
396  sort($paths);
397 
398  $this->assertEquals($ref, $paths);
399  }
400 
404  public function testRelativePathname($adapter)
405  {
406  $finder = $this->buildFinder($adapter)->in(self::$tmpDir)->sortByName();
407 
408  $paths = array();
409 
410  foreach ($finder as $file) {
411  $paths[] = $file->getRelativePathname();
412  }
413 
414  $ref = array('test.php', 'toto', 'test.py', 'foo', 'foo'.DIRECTORY_SEPARATOR.'bar.tmp', 'foo bar');
415 
416  sort($paths);
417  sort($ref);
418 
419  $this->assertEquals($ref, $paths);
420  }
421 
425  public function testAppendWithAFinder($adapter)
426  {
427  $finder = $this->buildFinder($adapter);
428  $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
429 
430  $finder1 = $this->buildFinder($adapter);
431  $finder1->directories()->in(self::$tmpDir);
432 
433  $finder = $finder->append($finder1);
434 
435  $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
436  }
437 
441  public function testAppendWithAnArray($adapter)
442  {
443  $finder = $this->buildFinder($adapter);
444  $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
445 
446  $finder->append($this->toAbsolute(array('foo', 'toto')));
447 
448  $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
449  }
450 
454  public function testAppendReturnsAFinder($adapter)
455  {
456  $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', $this->buildFinder($adapter)->append(array()));
457  }
458 
462  public function testAppendDoesNotRequireIn($adapter)
463  {
464  $finder = $this->buildFinder($adapter);
465  $finder->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
466 
467  $finder1 = Finder::create()->append($finder);
468 
469  $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
470  }
471 
472  public function testCountDirectories()
473  {
474  $directory = Finder::create()->directories()->in(self::$tmpDir);
475  $i = 0;
476 
477  foreach ($directory as $dir) {
478  ++$i;
479  }
480 
481  $this->assertCount($i, $directory);
482  }
483 
484  public function testCountFiles()
485  {
486  $files = Finder::create()->files()->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures');
487  $i = 0;
488 
489  foreach ($files as $file) {
490  ++$i;
491  }
492 
493  $this->assertCount($i, $files);
494  }
495 
499  public function testCountWithoutIn()
500  {
501  $finder = Finder::create()->files();
502  count($finder);
503  }
504 
508  public function testContains($adapter, $matchPatterns, $noMatchPatterns, $expected)
509  {
510  $finder = $this->buildFinder($adapter);
511  $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
512  ->name('*.txt')->sortByName()
513  ->contains($matchPatterns)
514  ->notContains($noMatchPatterns);
515 
516  $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
517  }
518 
522  public function testContainsOnDirectory(Adapter\AdapterInterface $adapter)
523  {
524  $finder = $this->buildFinder($adapter);
525  $finder->in(__DIR__)
526  ->directories()
527  ->name('Fixtures')
528  ->contains('abc');
529  $this->assertIterator(array(), $finder);
530  }
531 
535  public function testNotContainsOnDirectory(Adapter\AdapterInterface $adapter)
536  {
537  $finder = $this->buildFinder($adapter);
538  $finder->in(__DIR__)
539  ->directories()
540  ->name('Fixtures')
541  ->notContains('abc');
542  $this->assertIterator(array(), $finder);
543  }
544 
553  public function testMultipleLocations(Adapter\AdapterInterface $adapter)
554  {
555  $locations = array(
556  self::$tmpDir.'/',
557  self::$tmpDir.'/toto/',
558  );
559 
560  // it is expected that there are test.py test.php in the tmpDir
561  $finder = $this->buildFinder($adapter);
562  $finder->in($locations)->depth('< 1')->name('test.php');
563 
564  $this->assertCount(1, $finder);
565  }
566 
572  public function testIteratorKeys(Adapter\AdapterInterface $adapter)
573  {
574  $finder = $this->buildFinder($adapter)->in(self::$tmpDir);
575  foreach ($finder as $key => $file) {
576  $this->assertEquals($file->getPathname(), $key);
577  }
578  }
579 
584  {
585  $finder = $this->buildFinder($adapter);
586  $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
587  ->path('/^dir/');
588 
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',);
591  $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
592  }
593 
594  public function testAdaptersOrdering()
595  {
596  $finder = Finder::create()
597  ->removeAdapters()
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);
603 
604  $this->assertEquals(
605  array('c', 'e', 'a', 'd', 'b'),
606  array_map(function (Adapter\AdapterInterface $adapter) {
607  return $adapter->getName();
608  }, $finder->getAdapters())
609  );
610  }
611 
612  public function testAdaptersChaining()
613  {
614  $iterator = new \ArrayIterator(array());
615  $filenames = $this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto'));
616  foreach ($filenames as $file) {
617  $iterator->append(new \Symfony\Component\Finder\SplFileInfo($file, null, null));
618  }
619 
620  $finder = Finder::create()
621  ->removeAdapters()
622  ->addAdapter(new FakeAdapter\UnsupportedAdapter(), 3)
623  ->addAdapter(new FakeAdapter\FailingAdapter(), 2)
624  ->addAdapter(new FakeAdapter\DummyAdapter($iterator), 1);
625 
626  $this->assertIterator($filenames, $finder->in(sys_get_temp_dir())->getIterator());
627  }
628 
629  public function getAdaptersTestData()
630  {
631  return array_map(
632  function ($adapter) { return array($adapter); },
633  $this->getValidAdapters()
634  );
635  }
636 
637  public function getContainsTestData()
638  {
639  $tests = array(
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')),
650  );
651 
652  return $this->buildTestData($tests);
653  }
654 
655  public function getRegexNameTestData()
656  {
657  $tests = array(
658  array('~.+\\.p.+~i'),
659  array('~t.*s~i'),
660  );
661 
662  return $this->buildTestData($tests);
663  }
664 
668  public function testPath(Adapter\AdapterInterface $adapter, $matchPatterns, $noMatchPatterns, array $expected)
669  {
670  $finder = $this->buildFinder($adapter);
671  $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
672  ->path($matchPatterns)
673  ->notPath($noMatchPatterns);
674 
675  $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
676  }
677 
678  public function testAdapterSelection()
679  {
680  // test that by default, PhpAdapter is selected
681  $adapters = Finder::create()->getAdapters();
682  $this->assertTrue($adapters[0] instanceof Adapter\PhpAdapter);
683 
684  // test another adapter selection
685  $adapters = Finder::create()->setAdapter('gnu_find')->getAdapters();
686  $this->assertTrue($adapters[0] instanceof Adapter\GnuFindAdapter);
687 
688  // test that useBestAdapter method removes selection
689  $adapters = Finder::create()->useBestAdapter()->getAdapters();
690  $this->assertFalse($adapters[0] instanceof Adapter\PhpAdapter);
691  }
692 
693  public function getTestPathData()
694  {
695  $tests = array(
696  array('', '', array()),
697  array('/^A\/B\/C/', '/C$/',
698  array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat'),
699  ),
700  array('/^A\/B/', 'foobar',
701  array(
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',
706  ),
707  ),
708  array('A/B/C', 'foobar',
709  array(
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',
714  ),
715  ),
716  array('A/B', 'foobar',
717  array(
718  //dirs
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',
723  //files
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',
728  ),
729  ),
730  array('/^with space\//', 'foobar',
731  array(
732  'with space'.DIRECTORY_SEPARATOR.'foo.txt',
733  ),
734  ),
735  );
736 
737  return $this->buildTestData($tests);
738  }
739 
743  public function testAccessDeniedException(Adapter\AdapterInterface $adapter)
744  {
745  if ('\\' === DIRECTORY_SEPARATOR) {
746  $this->markTestSkipped('chmod is not supported on Windows');
747  }
748 
749  $finder = $this->buildFinder($adapter);
750  $finder->files()->in(self::$tmpDir);
751 
752  // make 'foo' directory non-readable
753  $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.'foo';
754  chmod($testDir, 0333);
755 
756  if (false === $couldRead = is_readable($testDir)) {
757  try {
758  $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
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()));
764  }
765 
766  $this->assertInstanceOf($expectedExceptionClass, $e);
767  }
768  }
769 
770  // restore original permissions
771  chmod($testDir, 0777);
772  clearstatcache($testDir);
773 
774  if ($couldRead) {
775  $this->markTestSkipped('could read test files while test requires unreadable');
776  }
777  }
778 
782  public function testIgnoredAccessDeniedException(Adapter\AdapterInterface $adapter)
783  {
784  if ('\\' === DIRECTORY_SEPARATOR) {
785  $this->markTestSkipped('chmod is not supported on Windows');
786  }
787 
788  $finder = $this->buildFinder($adapter);
789  $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
790 
791  // make 'foo' directory non-readable
792  $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.'foo';
793  chmod($testDir, 0333);
794 
795  if (false === ($couldRead = is_readable($testDir))) {
796  $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
797  }
798 
799  // restore original permissions
800  chmod($testDir, 0777);
801  clearstatcache($testDir);
802 
803  if ($couldRead) {
804  $this->markTestSkipped('could read test files while test requires unreadable');
805  }
806  }
807 
808  private function buildTestData(array $tests)
809  {
810  $data = array();
811  foreach ($this->getValidAdapters() as $adapter) {
812  foreach ($tests as $test) {
813  $data[] = array_merge(array($adapter), $test);
814  }
815  }
816 
817  return $data;
818  }
819 
820  private function buildFinder(Adapter\AdapterInterface $adapter)
821  {
822  return Finder::create()
823  ->removeAdapters()
824  ->addAdapter($adapter);
825  }
826 
827  private function getValidAdapters()
828  {
829  return array_filter(
830  array(
831  new Adapter\BsdFindAdapter(),
832  new Adapter\GnuFindAdapter(),
833  new Adapter\PhpAdapter(),
834  ),
835  function (Adapter\AdapterInterface $adapter) {
836  return $adapter->isSupported();
837  }
838  );
839  }
840 
849  {
850  $locations = array(
851  __DIR__.'/Fixtures/one',
852  self::$tmpDir.DIRECTORY_SEPARATOR.'toto',
853  );
854 
855  $finder = new Finder();
856  $finder->in($locations)->depth('< 10')->name('*.neon');
857 
858  $expected = array(
859  __DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
860  __DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
861  );
862 
863  $this->assertIterator($expected, $finder);
864  $this->assertIteratorInForeach($expected, $finder);
865  }
866 }