FinderTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Tests;
  11. use Symfony\Component\Finder\Adapter\AdapterInterface;
  12. use Symfony\Component\Finder\Adapter\PhpAdapter;
  13. use Symfony\Component\Finder\Finder;
  14. class FinderTest extends Iterator\RealIteratorTestCase
  15. {
  16. public function testCreate()
  17. {
  18. $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
  19. }
  20. public function testDirectories()
  21. {
  22. $finder = $this->buildFinder();
  23. $this->assertSame($finder, $finder->directories());
  24. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  25. $finder = $this->buildFinder();
  26. $finder->directories();
  27. $finder->files();
  28. $finder->directories();
  29. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  30. }
  31. public function testFiles()
  32. {
  33. $finder = $this->buildFinder();
  34. $this->assertSame($finder, $finder->files());
  35. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  36. $finder = $this->buildFinder();
  37. $finder->files();
  38. $finder->directories();
  39. $finder->files();
  40. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  41. }
  42. public function testRemoveTrailingSlash()
  43. {
  44. $finder = $this->buildFinder();
  45. $expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
  46. $in = self::$tmpDir.'//';
  47. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  48. }
  49. public function testSymlinksNotResolved()
  50. {
  51. if ('\\' === \DIRECTORY_SEPARATOR) {
  52. $this->markTestSkipped('symlinks are not supported on Windows');
  53. }
  54. $finder = $this->buildFinder();
  55. symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
  56. $expected = $this->toAbsolute(array('baz/bar.tmp'));
  57. $in = self::$tmpDir.'/baz/';
  58. try {
  59. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  60. unlink($this->toAbsolute('baz'));
  61. } catch (\Exception $e) {
  62. unlink($this->toAbsolute('baz'));
  63. throw $e;
  64. }
  65. }
  66. public function testBackPathNotNormalized()
  67. {
  68. $finder = $this->buildFinder();
  69. $expected = $this->toAbsolute(array('foo/../foo/bar.tmp'));
  70. $in = self::$tmpDir.'/foo/../foo/';
  71. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  72. }
  73. public function testDepth()
  74. {
  75. $finder = $this->buildFinder();
  76. $this->assertSame($finder, $finder->depth('< 1'));
  77. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  78. $finder = $this->buildFinder();
  79. $this->assertSame($finder, $finder->depth('<= 0'));
  80. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  81. $finder = $this->buildFinder();
  82. $this->assertSame($finder, $finder->depth('>= 1'));
  83. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
  84. $finder = $this->buildFinder();
  85. $finder->depth('< 1')->depth('>= 1');
  86. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  87. }
  88. public function testName()
  89. {
  90. $finder = $this->buildFinder();
  91. $this->assertSame($finder, $finder->name('*.php'));
  92. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  93. $finder = $this->buildFinder();
  94. $finder->name('test.ph*');
  95. $finder->name('test.py');
  96. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  97. $finder = $this->buildFinder();
  98. $finder->name('~^test~i');
  99. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  100. $finder = $this->buildFinder();
  101. $finder->name('~\\.php$~i');
  102. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  103. $finder = $this->buildFinder();
  104. $finder->name('test.p{hp,y}');
  105. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  106. }
  107. public function testNotName()
  108. {
  109. $finder = $this->buildFinder();
  110. $this->assertSame($finder, $finder->notName('*.php'));
  111. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  112. $finder = $this->buildFinder();
  113. $finder->notName('*.php');
  114. $finder->notName('*.py');
  115. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  116. $finder = $this->buildFinder();
  117. $finder->name('test.ph*');
  118. $finder->name('test.py');
  119. $finder->notName('*.php');
  120. $finder->notName('*.py');
  121. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  122. $finder = $this->buildFinder();
  123. $finder->name('test.ph*');
  124. $finder->name('test.py');
  125. $finder->notName('*.p{hp,y}');
  126. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  127. }
  128. /**
  129. * @dataProvider getRegexNameTestData
  130. */
  131. public function testRegexName($regex)
  132. {
  133. $finder = $this->buildFinder();
  134. $finder->name($regex);
  135. $this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
  136. }
  137. public function testSize()
  138. {
  139. $finder = $this->buildFinder();
  140. $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
  141. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  142. }
  143. public function testDate()
  144. {
  145. $finder = $this->buildFinder();
  146. $this->assertSame($finder, $finder->files()->date('until last month'));
  147. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
  148. }
  149. public function testExclude()
  150. {
  151. $finder = $this->buildFinder();
  152. $this->assertSame($finder, $finder->exclude('foo'));
  153. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  154. }
  155. public function testIgnoreVCS()
  156. {
  157. $finder = $this->buildFinder();
  158. $this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
  159. $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());
  160. $finder = $this->buildFinder();
  161. $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
  162. $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());
  163. $finder = $this->buildFinder();
  164. $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
  165. $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());
  166. }
  167. public function testIgnoreDotFiles()
  168. {
  169. $finder = $this->buildFinder();
  170. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  171. $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());
  172. $finder = $this->buildFinder();
  173. $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
  174. $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());
  175. $finder = $this->buildFinder();
  176. $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
  177. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  178. }
  179. public function testSortByName()
  180. {
  181. $finder = $this->buildFinder();
  182. $this->assertSame($finder, $finder->sortByName());
  183. $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  184. }
  185. public function testSortByType()
  186. {
  187. $finder = $this->buildFinder();
  188. $this->assertSame($finder, $finder->sortByType());
  189. $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  190. }
  191. public function testSortByAccessedTime()
  192. {
  193. $finder = $this->buildFinder();
  194. $this->assertSame($finder, $finder->sortByAccessedTime());
  195. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  196. }
  197. public function testSortByChangedTime()
  198. {
  199. $finder = $this->buildFinder();
  200. $this->assertSame($finder, $finder->sortByChangedTime());
  201. $this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  202. }
  203. public function testSortByModifiedTime()
  204. {
  205. $finder = $this->buildFinder();
  206. $this->assertSame($finder, $finder->sortByModifiedTime());
  207. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  208. }
  209. public function testSort()
  210. {
  211. $finder = $this->buildFinder();
  212. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
  213. $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  214. }
  215. public function testFilter()
  216. {
  217. $finder = $this->buildFinder();
  218. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
  219. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  220. }
  221. public function testFollowLinks()
  222. {
  223. if ('\\' == \DIRECTORY_SEPARATOR) {
  224. $this->markTestSkipped('symlinks are not supported on Windows');
  225. }
  226. $finder = $this->buildFinder();
  227. $this->assertSame($finder, $finder->followLinks());
  228. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
  229. }
  230. public function testIn()
  231. {
  232. $finder = $this->buildFinder();
  233. $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
  234. $expected = array(
  235. self::$tmpDir.\DIRECTORY_SEPARATOR.'test.php',
  236. __DIR__.\DIRECTORY_SEPARATOR.'BsdFinderTest.php',
  237. __DIR__.\DIRECTORY_SEPARATOR.'FinderTest.php',
  238. __DIR__.\DIRECTORY_SEPARATOR.'GnuFinderTest.php',
  239. __DIR__.\DIRECTORY_SEPARATOR.'PhpFinderTest.php',
  240. __DIR__.\DIRECTORY_SEPARATOR.'GlobTest.php',
  241. );
  242. $this->assertIterator($expected, $iterator);
  243. }
  244. /**
  245. * @expectedException \InvalidArgumentException
  246. */
  247. public function testInWithNonExistentDirectory()
  248. {
  249. $finder = new Finder();
  250. $finder->in('foobar');
  251. }
  252. public function testInWithGlob()
  253. {
  254. $finder = $this->buildFinder();
  255. $finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();
  256. $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
  257. }
  258. /**
  259. * @expectedException \InvalidArgumentException
  260. */
  261. public function testInWithNonDirectoryGlob()
  262. {
  263. $finder = new Finder();
  264. $finder->in(__DIR__.'/Fixtures/A/a*');
  265. }
  266. public function testInWithGlobBrace()
  267. {
  268. $finder = $this->buildFinder();
  269. $finder->in(array(__DIR__.'/Fixtures/{A,copy/A}/B/C'))->getIterator();
  270. $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
  271. }
  272. /**
  273. * @expectedException \LogicException
  274. */
  275. public function testGetIteratorWithoutIn()
  276. {
  277. $finder = Finder::create();
  278. $finder->getIterator();
  279. }
  280. public function testGetIterator()
  281. {
  282. $finder = $this->buildFinder();
  283. $dirs = array();
  284. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  285. $dirs[] = (string) $dir;
  286. }
  287. $expected = $this->toAbsolute(array('foo', 'toto'));
  288. sort($dirs);
  289. sort($expected);
  290. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  291. $finder = $this->buildFinder();
  292. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  293. $finder = $this->buildFinder();
  294. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  295. $a = array_values(array_map('strval', $a));
  296. sort($a);
  297. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  298. }
  299. public function testRelativePath()
  300. {
  301. $finder = $this->buildFinder()->in(self::$tmpDir);
  302. $paths = array();
  303. foreach ($finder as $file) {
  304. $paths[] = $file->getRelativePath();
  305. }
  306. $ref = array('', '', '', '', 'foo', '');
  307. sort($ref);
  308. sort($paths);
  309. $this->assertEquals($ref, $paths);
  310. }
  311. public function testRelativePathname()
  312. {
  313. $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
  314. $paths = array();
  315. foreach ($finder as $file) {
  316. $paths[] = $file->getRelativePathname();
  317. }
  318. $ref = array('test.php', 'toto', 'test.py', 'foo', 'foo'.\DIRECTORY_SEPARATOR.'bar.tmp', 'foo bar');
  319. sort($paths);
  320. sort($ref);
  321. $this->assertEquals($ref, $paths);
  322. }
  323. public function testAppendWithAFinder()
  324. {
  325. $finder = $this->buildFinder();
  326. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  327. $finder1 = $this->buildFinder();
  328. $finder1->directories()->in(self::$tmpDir);
  329. $finder = $finder->append($finder1);
  330. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  331. }
  332. public function testAppendWithAnArray()
  333. {
  334. $finder = $this->buildFinder();
  335. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  336. $finder->append($this->toAbsolute(array('foo', 'toto')));
  337. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  338. }
  339. public function testAppendReturnsAFinder()
  340. {
  341. $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append(array()));
  342. }
  343. public function testAppendDoesNotRequireIn()
  344. {
  345. $finder = $this->buildFinder();
  346. $finder->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  347. $finder1 = Finder::create()->append($finder);
  348. $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
  349. }
  350. public function testCountDirectories()
  351. {
  352. $directory = Finder::create()->directories()->in(self::$tmpDir);
  353. $i = 0;
  354. foreach ($directory as $dir) {
  355. ++$i;
  356. }
  357. $this->assertCount($i, $directory);
  358. }
  359. public function testCountFiles()
  360. {
  361. $files = Finder::create()->files()->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  362. $i = 0;
  363. foreach ($files as $file) {
  364. ++$i;
  365. }
  366. $this->assertCount($i, $files);
  367. }
  368. /**
  369. * @expectedException \LogicException
  370. */
  371. public function testCountWithoutIn()
  372. {
  373. $finder = Finder::create()->files();
  374. \count($finder);
  375. }
  376. /**
  377. * @dataProvider getContainsTestData
  378. */
  379. public function testContains($matchPatterns, $noMatchPatterns, $expected)
  380. {
  381. $finder = $this->buildFinder();
  382. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  383. ->name('*.txt')->sortByName()
  384. ->contains($matchPatterns)
  385. ->notContains($noMatchPatterns);
  386. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  387. }
  388. public function testContainsOnDirectory()
  389. {
  390. $finder = $this->buildFinder();
  391. $finder->in(__DIR__)
  392. ->directories()
  393. ->name('Fixtures')
  394. ->contains('abc');
  395. $this->assertIterator(array(), $finder);
  396. }
  397. public function testNotContainsOnDirectory()
  398. {
  399. $finder = $this->buildFinder();
  400. $finder->in(__DIR__)
  401. ->directories()
  402. ->name('Fixtures')
  403. ->notContains('abc');
  404. $this->assertIterator(array(), $finder);
  405. }
  406. /**
  407. * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
  408. * with inner FilesystemIterator in an invalid state.
  409. *
  410. * @see https://bugs.php.net/68557
  411. */
  412. public function testMultipleLocations()
  413. {
  414. $locations = array(
  415. self::$tmpDir.'/',
  416. self::$tmpDir.'/toto/',
  417. );
  418. // it is expected that there are test.py test.php in the tmpDir
  419. $finder = new Finder();
  420. $finder->in($locations)
  421. // the default flag IGNORE_DOT_FILES fixes the problem indirectly
  422. // so we set it to false for better isolation
  423. ->ignoreDotFiles(false)
  424. ->depth('< 1')->name('test.php');
  425. $this->assertCount(1, $finder);
  426. }
  427. /**
  428. * Searching in multiple locations with sub directories involves
  429. * AppendIterator which does an unnecessary rewind which leaves
  430. * FilterIterator with inner FilesystemIterator in an invalid state.
  431. *
  432. * @see https://bugs.php.net/68557
  433. */
  434. public function testMultipleLocationsWithSubDirectories()
  435. {
  436. $locations = array(
  437. __DIR__.'/Fixtures/one',
  438. self::$tmpDir.\DIRECTORY_SEPARATOR.'toto',
  439. );
  440. $finder = $this->buildFinder();
  441. $finder->in($locations)->depth('< 10')->name('*.neon');
  442. $expected = array(
  443. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
  444. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
  445. );
  446. $this->assertIterator($expected, $finder);
  447. $this->assertIteratorInForeach($expected, $finder);
  448. }
  449. /**
  450. * Iterator keys must be the file pathname.
  451. */
  452. public function testIteratorKeys()
  453. {
  454. $finder = $this->buildFinder()->in(self::$tmpDir);
  455. foreach ($finder as $key => $file) {
  456. $this->assertEquals($file->getPathname(), $key);
  457. }
  458. }
  459. public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
  460. {
  461. $finder = $this->buildFinder();
  462. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
  463. ->path('/^dir/');
  464. $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');
  465. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  466. }
  467. /**
  468. * @group legacy
  469. */
  470. public function testAdaptersOrdering()
  471. {
  472. $finder = Finder::create()
  473. ->removeAdapters()
  474. ->addAdapter(new FakeAdapter\NamedAdapter('a'), 0)
  475. ->addAdapter(new FakeAdapter\NamedAdapter('b'), -50)
  476. ->addAdapter(new FakeAdapter\NamedAdapter('c'), 50)
  477. ->addAdapter(new FakeAdapter\NamedAdapter('d'), -25)
  478. ->addAdapter(new FakeAdapter\NamedAdapter('e'), 25);
  479. $this->assertEquals(
  480. array('c', 'e', 'a', 'd', 'b'),
  481. array_map(function (AdapterInterface $adapter) {
  482. return $adapter->getName();
  483. }, $finder->getAdapters())
  484. );
  485. }
  486. /**
  487. * @group legacy
  488. */
  489. public function testAdaptersChaining()
  490. {
  491. $iterator = new \ArrayIterator(array());
  492. $filenames = $this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto'));
  493. foreach ($filenames as $file) {
  494. $iterator->append(new \Symfony\Component\Finder\SplFileInfo($file, null, null));
  495. }
  496. $finder = Finder::create()
  497. ->removeAdapters()
  498. ->addAdapter(new FakeAdapter\UnsupportedAdapter(), 3)
  499. ->addAdapter(new FakeAdapter\FailingAdapter(), 2)
  500. ->addAdapter(new FakeAdapter\DummyAdapter($iterator), 1);
  501. $this->assertIterator($filenames, $finder->in(sys_get_temp_dir())->getIterator());
  502. }
  503. public function getContainsTestData()
  504. {
  505. return array(
  506. array('', '', array()),
  507. array('foo', 'bar', array()),
  508. array('', 'foobar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
  509. array('lorem ipsum dolor sit amet', 'foobar', array('lorem.txt')),
  510. array('sit', 'bar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
  511. array('dolor sit amet', '@^L@m', array('dolor.txt', 'ipsum.txt')),
  512. array('/^lorem ipsum dolor sit amet$/m', 'foobar', array('lorem.txt')),
  513. array('lorem', 'foobar', array('lorem.txt')),
  514. array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
  515. array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
  516. );
  517. }
  518. public function getRegexNameTestData()
  519. {
  520. return array(
  521. array('~.+\\.p.+~i'),
  522. array('~t.*s~i'),
  523. );
  524. }
  525. /**
  526. * @dataProvider getTestPathData
  527. */
  528. public function testPath($matchPatterns, $noMatchPatterns, array $expected)
  529. {
  530. $finder = $this->buildFinder();
  531. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  532. ->path($matchPatterns)
  533. ->notPath($noMatchPatterns);
  534. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  535. }
  536. /**
  537. * @group legacy
  538. */
  539. public function testAdapterSelection()
  540. {
  541. // test that by default, PhpAdapter is selected
  542. $adapters = Finder::create()->getAdapters();
  543. $this->assertInstanceOf('Symfony\Component\Finder\Adapter\PhpAdapter', $adapters[0]);
  544. // test another adapter selection
  545. $adapters = Finder::create()->setAdapter('gnu_find')->getAdapters();
  546. $this->assertInstanceOf('Symfony\Component\Finder\Adapter\GnuFindAdapter', $adapters[0]);
  547. // test that useBestAdapter method removes selection
  548. $adapters = Finder::create()->useBestAdapter()->getAdapters();
  549. $this->assertNotInstanceOf('Symfony\Component\Finder\Adapter\PhpAdapter', $adapters[0]);
  550. }
  551. public function getTestPathData()
  552. {
  553. return array(
  554. array('', '', array()),
  555. array('/^A\/B\/C/', '/C$/',
  556. array('A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat'),
  557. ),
  558. array('/^A\/B/', 'foobar',
  559. array(
  560. 'A'.\DIRECTORY_SEPARATOR.'B',
  561. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  562. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  563. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  564. ),
  565. ),
  566. array('A/B/C', 'foobar',
  567. array(
  568. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  569. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  570. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  571. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  572. ),
  573. ),
  574. array('A/B', 'foobar',
  575. array(
  576. //dirs
  577. 'A'.\DIRECTORY_SEPARATOR.'B',
  578. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  579. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B',
  580. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  581. //files
  582. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  583. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  584. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat.copy',
  585. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  586. ),
  587. ),
  588. array('/^with space\//', 'foobar',
  589. array(
  590. 'with space'.\DIRECTORY_SEPARATOR.'foo.txt',
  591. ),
  592. ),
  593. );
  594. }
  595. public function testAccessDeniedException()
  596. {
  597. if ('\\' === \DIRECTORY_SEPARATOR) {
  598. $this->markTestSkipped('chmod is not supported on Windows');
  599. }
  600. $finder = $this->buildFinder();
  601. $finder->files()->in(self::$tmpDir);
  602. // make 'foo' directory non-readable
  603. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  604. chmod($testDir, 0333);
  605. if (false === $couldRead = is_readable($testDir)) {
  606. try {
  607. $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
  608. $this->fail('Finder should throw an exception when opening a non-readable directory.');
  609. } catch (\Exception $e) {
  610. $expectedExceptionClass = 'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
  611. if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
  612. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  613. }
  614. if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
  615. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  616. }
  617. $this->assertInstanceOf($expectedExceptionClass, $e);
  618. }
  619. }
  620. // restore original permissions
  621. chmod($testDir, 0777);
  622. clearstatcache($testDir);
  623. if ($couldRead) {
  624. $this->markTestSkipped('could read test files while test requires unreadable');
  625. }
  626. }
  627. public function testIgnoredAccessDeniedException()
  628. {
  629. if ('\\' === \DIRECTORY_SEPARATOR) {
  630. $this->markTestSkipped('chmod is not supported on Windows');
  631. }
  632. $finder = $this->buildFinder();
  633. $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
  634. // make 'foo' directory non-readable
  635. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  636. chmod($testDir, 0333);
  637. if (false === ($couldRead = is_readable($testDir))) {
  638. $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
  639. }
  640. // restore original permissions
  641. chmod($testDir, 0777);
  642. clearstatcache($testDir);
  643. if ($couldRead) {
  644. $this->markTestSkipped('could read test files while test requires unreadable');
  645. }
  646. }
  647. protected function buildFinder()
  648. {
  649. return Finder::create();
  650. }
  651. }