FinderTest.php 30 KB

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