FinderTest.php 27KB

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