Drupal investigation

FinderTest.php 28KB

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