FinderTest.php 25 KB

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