Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FinderTest.php 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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(['foo', 'qux', 'toto']), $finder->in(self::$tmpDir)->getIterator());
  23. $finder = $this->buildFinder();
  24. $finder->directories();
  25. $finder->files();
  26. $finder->directories();
  27. $this->assertIterator($this->toAbsolute(['foo', 'qux', '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(['foo/bar.tmp',
  34. 'test.php',
  35. 'test.py',
  36. 'foo bar',
  37. 'qux/baz_100_1.py',
  38. 'qux/baz_1_2.py',
  39. 'qux_0_1.php',
  40. 'qux_1000_1.php',
  41. 'qux_1002_0.php',
  42. 'qux_10_2.php',
  43. 'qux_12_0.php',
  44. 'qux_2_0.php',
  45. ]), $finder->in(self::$tmpDir)->getIterator());
  46. $finder = $this->buildFinder();
  47. $finder->files();
  48. $finder->directories();
  49. $finder->files();
  50. $this->assertIterator($this->toAbsolute(['foo/bar.tmp',
  51. 'test.php',
  52. 'test.py',
  53. 'foo bar',
  54. 'qux/baz_100_1.py',
  55. 'qux/baz_1_2.py',
  56. 'qux_0_1.php',
  57. 'qux_1000_1.php',
  58. 'qux_1002_0.php',
  59. 'qux_10_2.php',
  60. 'qux_12_0.php',
  61. 'qux_2_0.php',
  62. ]), $finder->in(self::$tmpDir)->getIterator());
  63. }
  64. public function testRemoveTrailingSlash()
  65. {
  66. $finder = $this->buildFinder();
  67. $expected = $this->toAbsolute([
  68. 'foo/bar.tmp',
  69. 'test.php',
  70. 'test.py',
  71. 'foo bar',
  72. 'qux/baz_100_1.py',
  73. 'qux/baz_1_2.py',
  74. 'qux_0_1.php',
  75. 'qux_1000_1.php',
  76. 'qux_1002_0.php',
  77. 'qux_10_2.php',
  78. 'qux_12_0.php',
  79. 'qux_2_0.php',
  80. ]);
  81. $in = self::$tmpDir.'//';
  82. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  83. }
  84. public function testSymlinksNotResolved()
  85. {
  86. if ('\\' === \DIRECTORY_SEPARATOR) {
  87. $this->markTestSkipped('symlinks are not supported on Windows');
  88. }
  89. $finder = $this->buildFinder();
  90. symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
  91. $expected = $this->toAbsolute(['baz/bar.tmp']);
  92. $in = self::$tmpDir.'/baz/';
  93. try {
  94. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  95. unlink($this->toAbsolute('baz'));
  96. } catch (\Exception $e) {
  97. unlink($this->toAbsolute('baz'));
  98. throw $e;
  99. }
  100. }
  101. public function testBackPathNotNormalized()
  102. {
  103. $finder = $this->buildFinder();
  104. $expected = $this->toAbsolute(['foo/../foo/bar.tmp']);
  105. $in = self::$tmpDir.'/foo/../foo/';
  106. $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
  107. }
  108. public function testDepth()
  109. {
  110. $finder = $this->buildFinder();
  111. $this->assertSame($finder, $finder->depth('< 1'));
  112. $this->assertIterator($this->toAbsolute(['foo',
  113. 'test.php',
  114. 'test.py',
  115. 'toto',
  116. 'foo bar',
  117. 'qux',
  118. 'qux_0_1.php',
  119. 'qux_1000_1.php',
  120. 'qux_1002_0.php',
  121. 'qux_10_2.php',
  122. 'qux_12_0.php',
  123. 'qux_2_0.php',
  124. ]), $finder->in(self::$tmpDir)->getIterator());
  125. $finder = $this->buildFinder();
  126. $this->assertSame($finder, $finder->depth('<= 0'));
  127. $this->assertIterator($this->toAbsolute(['foo',
  128. 'test.php',
  129. 'test.py',
  130. 'toto',
  131. 'foo bar',
  132. 'qux',
  133. 'qux_0_1.php',
  134. 'qux_1000_1.php',
  135. 'qux_1002_0.php',
  136. 'qux_10_2.php',
  137. 'qux_12_0.php',
  138. 'qux_2_0.php',
  139. ]), $finder->in(self::$tmpDir)->getIterator());
  140. $finder = $this->buildFinder();
  141. $this->assertSame($finder, $finder->depth('>= 1'));
  142. $this->assertIterator($this->toAbsolute([
  143. 'foo/bar.tmp',
  144. 'qux/baz_100_1.py',
  145. 'qux/baz_1_2.py',
  146. ]), $finder->in(self::$tmpDir)->getIterator());
  147. $finder = $this->buildFinder();
  148. $finder->depth('< 1')->depth('>= 1');
  149. $this->assertIterator([], $finder->in(self::$tmpDir)->getIterator());
  150. }
  151. public function testDepthWithArrayParam()
  152. {
  153. $finder = $this->buildFinder();
  154. $finder->depth(['>= 1', '< 2']);
  155. $this->assertIterator($this->toAbsolute([
  156. 'foo/bar.tmp',
  157. 'qux/baz_100_1.py',
  158. 'qux/baz_1_2.py',
  159. ]), $finder->in(self::$tmpDir)->getIterator());
  160. }
  161. public function testName()
  162. {
  163. $finder = $this->buildFinder();
  164. $this->assertSame($finder, $finder->name('*.php'));
  165. $this->assertIterator($this->toAbsolute([
  166. 'test.php',
  167. 'qux_0_1.php',
  168. 'qux_1000_1.php',
  169. 'qux_1002_0.php',
  170. 'qux_10_2.php',
  171. 'qux_12_0.php',
  172. 'qux_2_0.php',
  173. ]), $finder->in(self::$tmpDir)->getIterator());
  174. $finder = $this->buildFinder();
  175. $finder->name('test.ph*');
  176. $finder->name('test.py');
  177. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  178. $finder = $this->buildFinder();
  179. $finder->name('~^test~i');
  180. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  181. $finder = $this->buildFinder();
  182. $finder->name('~\\.php$~i');
  183. $this->assertIterator($this->toAbsolute([
  184. 'test.php',
  185. 'qux_0_1.php',
  186. 'qux_1000_1.php',
  187. 'qux_1002_0.php',
  188. 'qux_10_2.php',
  189. 'qux_12_0.php',
  190. 'qux_2_0.php',
  191. ]), $finder->in(self::$tmpDir)->getIterator());
  192. $finder = $this->buildFinder();
  193. $finder->name('test.p{hp,y}');
  194. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  195. }
  196. public function testNameWithArrayParam()
  197. {
  198. $finder = $this->buildFinder();
  199. $finder->name(['test.php', 'test.py']);
  200. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  201. }
  202. public function testNotName()
  203. {
  204. $finder = $this->buildFinder();
  205. $this->assertSame($finder, $finder->notName('*.php'));
  206. $this->assertIterator($this->toAbsolute([
  207. 'foo',
  208. 'foo/bar.tmp',
  209. 'test.py',
  210. 'toto',
  211. 'foo bar',
  212. 'qux',
  213. 'qux/baz_100_1.py',
  214. 'qux/baz_1_2.py',
  215. ]), $finder->in(self::$tmpDir)->getIterator());
  216. $finder = $this->buildFinder();
  217. $finder->notName('*.php');
  218. $finder->notName('*.py');
  219. $this->assertIterator($this->toAbsolute([
  220. 'foo',
  221. 'foo/bar.tmp',
  222. 'toto',
  223. 'foo bar',
  224. 'qux',
  225. ]), $finder->in(self::$tmpDir)->getIterator());
  226. $finder = $this->buildFinder();
  227. $finder->name('test.ph*');
  228. $finder->name('test.py');
  229. $finder->notName('*.php');
  230. $finder->notName('*.py');
  231. $this->assertIterator([], $finder->in(self::$tmpDir)->getIterator());
  232. $finder = $this->buildFinder();
  233. $finder->name('test.ph*');
  234. $finder->name('test.py');
  235. $finder->notName('*.p{hp,y}');
  236. $this->assertIterator([], $finder->in(self::$tmpDir)->getIterator());
  237. }
  238. public function testNotNameWithArrayParam()
  239. {
  240. $finder = $this->buildFinder();
  241. $finder->notName(['*.php', '*.py']);
  242. $this->assertIterator($this->toAbsolute([
  243. 'foo',
  244. 'foo/bar.tmp',
  245. 'toto',
  246. 'foo bar',
  247. 'qux',
  248. ]), $finder->in(self::$tmpDir)->getIterator());
  249. }
  250. /**
  251. * @dataProvider getRegexNameTestData
  252. */
  253. public function testRegexName($regex)
  254. {
  255. $finder = $this->buildFinder();
  256. $finder->name($regex);
  257. $this->assertIterator($this->toAbsolute([
  258. 'test.py',
  259. 'test.php',
  260. ]), $finder->in(self::$tmpDir)->getIterator());
  261. }
  262. public function testSize()
  263. {
  264. $finder = $this->buildFinder();
  265. $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
  266. $this->assertIterator($this->toAbsolute(['test.php']), $finder->in(self::$tmpDir)->getIterator());
  267. }
  268. public function testSizeWithArrayParam()
  269. {
  270. $finder = $this->buildFinder();
  271. $this->assertSame($finder, $finder->files()->size(['< 1K', '> 500']));
  272. $this->assertIterator($this->toAbsolute(['test.php']), $finder->in(self::$tmpDir)->getIterator());
  273. }
  274. public function testDate()
  275. {
  276. $finder = $this->buildFinder();
  277. $this->assertSame($finder, $finder->files()->date('until last month'));
  278. $this->assertIterator($this->toAbsolute(['foo/bar.tmp', 'test.php']), $finder->in(self::$tmpDir)->getIterator());
  279. }
  280. public function testDateWithArrayParam()
  281. {
  282. $finder = $this->buildFinder();
  283. $this->assertSame($finder, $finder->files()->date(['>= 2005-10-15', 'until last month']));
  284. $this->assertIterator($this->toAbsolute(['foo/bar.tmp', 'test.php']), $finder->in(self::$tmpDir)->getIterator());
  285. }
  286. public function testExclude()
  287. {
  288. $finder = $this->buildFinder();
  289. $this->assertSame($finder, $finder->exclude('foo'));
  290. $this->assertIterator($this->toAbsolute([
  291. 'test.php',
  292. 'test.py',
  293. 'toto',
  294. 'foo bar',
  295. 'qux',
  296. 'qux/baz_100_1.py',
  297. 'qux/baz_1_2.py',
  298. 'qux_0_1.php',
  299. 'qux_1000_1.php',
  300. 'qux_1002_0.php',
  301. 'qux_10_2.php',
  302. 'qux_12_0.php',
  303. 'qux_2_0.php',
  304. ]), $finder->in(self::$tmpDir)->getIterator());
  305. }
  306. public function testIgnoreVCS()
  307. {
  308. $finder = $this->buildFinder();
  309. $this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
  310. $this->assertIterator($this->toAbsolute([
  311. '.gitignore',
  312. '.git',
  313. 'foo',
  314. 'foo/bar.tmp',
  315. 'test.php',
  316. 'test.py',
  317. 'toto',
  318. 'toto/.git',
  319. '.bar',
  320. '.foo',
  321. '.foo/.bar',
  322. '.foo/bar',
  323. 'foo bar',
  324. 'qux',
  325. 'qux/baz_100_1.py',
  326. 'qux/baz_1_2.py',
  327. 'qux_0_1.php',
  328. 'qux_1000_1.php',
  329. 'qux_1002_0.php',
  330. 'qux_10_2.php',
  331. 'qux_12_0.php',
  332. 'qux_2_0.php',
  333. ]), $finder->in(self::$tmpDir)->getIterator());
  334. $finder = $this->buildFinder();
  335. $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
  336. $this->assertIterator($this->toAbsolute([
  337. '.gitignore',
  338. '.git',
  339. 'foo',
  340. 'foo/bar.tmp',
  341. 'test.php',
  342. 'test.py',
  343. 'toto',
  344. 'toto/.git',
  345. '.bar',
  346. '.foo',
  347. '.foo/.bar',
  348. '.foo/bar',
  349. 'foo bar',
  350. 'qux',
  351. 'qux/baz_100_1.py',
  352. 'qux/baz_1_2.py',
  353. 'qux_0_1.php',
  354. 'qux_1000_1.php',
  355. 'qux_1002_0.php',
  356. 'qux_10_2.php',
  357. 'qux_12_0.php',
  358. 'qux_2_0.php',
  359. ]), $finder->in(self::$tmpDir)->getIterator());
  360. $finder = $this->buildFinder();
  361. $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
  362. $this->assertIterator($this->toAbsolute([
  363. '.gitignore',
  364. 'foo',
  365. 'foo/bar.tmp',
  366. 'test.php',
  367. 'test.py',
  368. 'toto',
  369. '.bar',
  370. '.foo',
  371. '.foo/.bar',
  372. '.foo/bar',
  373. 'foo bar',
  374. 'qux',
  375. 'qux/baz_100_1.py',
  376. 'qux/baz_1_2.py',
  377. 'qux_0_1.php',
  378. 'qux_1000_1.php',
  379. 'qux_1002_0.php',
  380. 'qux_10_2.php',
  381. 'qux_12_0.php',
  382. 'qux_2_0.php',
  383. ]), $finder->in(self::$tmpDir)->getIterator());
  384. }
  385. public function testIgnoreVCSIgnored()
  386. {
  387. $finder = $this->buildFinder();
  388. $this->assertSame(
  389. $finder,
  390. $finder
  391. ->ignoreVCS(true)
  392. ->ignoreDotFiles(true)
  393. ->ignoreVCSIgnored(true)
  394. );
  395. $this->assertIterator($this->toAbsolute([
  396. 'foo',
  397. 'foo/bar.tmp',
  398. 'test.py',
  399. 'toto',
  400. 'foo bar',
  401. 'qux',
  402. 'qux/baz_100_1.py',
  403. 'qux/baz_1_2.py',
  404. ]), $finder->in(self::$tmpDir)->getIterator());
  405. }
  406. public function testIgnoreVCSCanBeDisabledAfterFirstIteration()
  407. {
  408. $finder = $this->buildFinder();
  409. $finder->in(self::$tmpDir);
  410. $finder->ignoreDotFiles(false);
  411. $this->assertIterator($this->toAbsolute([
  412. '.gitignore',
  413. 'foo',
  414. 'foo/bar.tmp',
  415. 'qux',
  416. 'qux/baz_100_1.py',
  417. 'qux/baz_1_2.py',
  418. 'qux_0_1.php',
  419. 'qux_1000_1.php',
  420. 'qux_1002_0.php',
  421. 'qux_10_2.php',
  422. 'qux_12_0.php',
  423. 'qux_2_0.php',
  424. 'test.php',
  425. 'test.py',
  426. 'toto',
  427. '.bar',
  428. '.foo',
  429. '.foo/.bar',
  430. '.foo/bar',
  431. 'foo bar',
  432. ]), $finder->getIterator());
  433. $finder->ignoreVCS(false);
  434. $this->assertIterator($this->toAbsolute([
  435. '.gitignore',
  436. '.git',
  437. 'foo',
  438. 'foo/bar.tmp',
  439. 'qux',
  440. 'qux/baz_100_1.py',
  441. 'qux/baz_1_2.py',
  442. 'qux_0_1.php',
  443. 'qux_1000_1.php',
  444. 'qux_1002_0.php',
  445. 'qux_10_2.php',
  446. 'qux_12_0.php',
  447. 'qux_2_0.php',
  448. 'test.php',
  449. 'test.py',
  450. 'toto',
  451. 'toto/.git',
  452. '.bar',
  453. '.foo',
  454. '.foo/.bar',
  455. '.foo/bar',
  456. 'foo bar',
  457. ]), $finder->getIterator());
  458. }
  459. public function testIgnoreDotFiles()
  460. {
  461. $finder = $this->buildFinder();
  462. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  463. $this->assertIterator($this->toAbsolute([
  464. '.gitignore',
  465. '.git',
  466. '.bar',
  467. '.foo',
  468. '.foo/.bar',
  469. '.foo/bar',
  470. 'foo',
  471. 'foo/bar.tmp',
  472. 'test.php',
  473. 'test.py',
  474. 'toto',
  475. 'toto/.git',
  476. 'foo bar',
  477. 'qux',
  478. 'qux/baz_100_1.py',
  479. 'qux/baz_1_2.py',
  480. 'qux_0_1.php',
  481. 'qux_1000_1.php',
  482. 'qux_1002_0.php',
  483. 'qux_10_2.php',
  484. 'qux_12_0.php',
  485. 'qux_2_0.php',
  486. ]), $finder->in(self::$tmpDir)->getIterator());
  487. $finder = $this->buildFinder();
  488. $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
  489. $this->assertIterator($this->toAbsolute([
  490. '.gitignore',
  491. '.git',
  492. '.bar',
  493. '.foo',
  494. '.foo/.bar',
  495. '.foo/bar',
  496. 'foo',
  497. 'foo/bar.tmp',
  498. 'test.php',
  499. 'test.py',
  500. 'toto',
  501. 'toto/.git',
  502. 'foo bar',
  503. 'qux',
  504. 'qux/baz_100_1.py',
  505. 'qux/baz_1_2.py',
  506. 'qux_0_1.php',
  507. 'qux_1000_1.php',
  508. 'qux_1002_0.php',
  509. 'qux_10_2.php',
  510. 'qux_12_0.php',
  511. 'qux_2_0.php',
  512. ]), $finder->in(self::$tmpDir)->getIterator());
  513. $finder = $this->buildFinder();
  514. $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
  515. $this->assertIterator($this->toAbsolute([
  516. 'foo',
  517. 'foo/bar.tmp',
  518. 'test.php',
  519. 'test.py',
  520. 'toto',
  521. 'foo bar',
  522. 'qux',
  523. 'qux/baz_100_1.py',
  524. 'qux/baz_1_2.py',
  525. 'qux_0_1.php',
  526. 'qux_1000_1.php',
  527. 'qux_1002_0.php',
  528. 'qux_10_2.php',
  529. 'qux_12_0.php',
  530. 'qux_2_0.php',
  531. ]), $finder->in(self::$tmpDir)->getIterator());
  532. }
  533. public function testIgnoreDotFilesCanBeDisabledAfterFirstIteration()
  534. {
  535. $finder = $this->buildFinder();
  536. $finder->in(self::$tmpDir);
  537. $this->assertIterator($this->toAbsolute([
  538. 'foo',
  539. 'foo/bar.tmp',
  540. 'qux',
  541. 'qux/baz_100_1.py',
  542. 'qux/baz_1_2.py',
  543. 'qux_0_1.php',
  544. 'qux_1000_1.php',
  545. 'qux_1002_0.php',
  546. 'qux_10_2.php',
  547. 'qux_12_0.php',
  548. 'qux_2_0.php',
  549. 'test.php',
  550. 'test.py',
  551. 'toto',
  552. 'foo bar',
  553. ]), $finder->getIterator());
  554. $finder->ignoreDotFiles(false);
  555. $this->assertIterator($this->toAbsolute([
  556. '.gitignore',
  557. 'foo',
  558. 'foo/bar.tmp',
  559. 'qux',
  560. 'qux/baz_100_1.py',
  561. 'qux/baz_1_2.py',
  562. 'qux_0_1.php',
  563. 'qux_1000_1.php',
  564. 'qux_1002_0.php',
  565. 'qux_10_2.php',
  566. 'qux_12_0.php',
  567. 'qux_2_0.php',
  568. 'test.php',
  569. 'test.py',
  570. 'toto',
  571. '.bar',
  572. '.foo',
  573. '.foo/.bar',
  574. '.foo/bar',
  575. 'foo bar',
  576. ]), $finder->getIterator());
  577. }
  578. public function testSortByName()
  579. {
  580. $finder = $this->buildFinder();
  581. $this->assertSame($finder, $finder->sortByName());
  582. $this->assertIterator($this->toAbsolute([
  583. 'foo',
  584. 'foo bar',
  585. 'foo/bar.tmp',
  586. 'qux',
  587. 'qux/baz_100_1.py',
  588. 'qux/baz_1_2.py',
  589. 'qux_0_1.php',
  590. 'qux_1000_1.php',
  591. 'qux_1002_0.php',
  592. 'qux_10_2.php',
  593. 'qux_12_0.php',
  594. 'qux_2_0.php',
  595. 'test.php',
  596. 'test.py',
  597. 'toto',
  598. ]), $finder->in(self::$tmpDir)->getIterator());
  599. }
  600. public function testSortByType()
  601. {
  602. $finder = $this->buildFinder();
  603. $this->assertSame($finder, $finder->sortByType());
  604. $this->assertIterator($this->toAbsolute([
  605. 'foo',
  606. 'foo bar',
  607. 'toto',
  608. 'foo/bar.tmp',
  609. 'test.php',
  610. 'test.py',
  611. 'qux',
  612. 'qux/baz_100_1.py',
  613. 'qux/baz_1_2.py',
  614. 'qux_0_1.php',
  615. 'qux_1000_1.php',
  616. 'qux_1002_0.php',
  617. 'qux_10_2.php',
  618. 'qux_12_0.php',
  619. 'qux_2_0.php',
  620. ]), $finder->in(self::$tmpDir)->getIterator());
  621. }
  622. public function testSortByAccessedTime()
  623. {
  624. $finder = $this->buildFinder();
  625. $this->assertSame($finder, $finder->sortByAccessedTime());
  626. $this->assertIterator($this->toAbsolute([
  627. 'foo/bar.tmp',
  628. 'test.php',
  629. 'toto',
  630. 'test.py',
  631. 'foo',
  632. 'foo bar',
  633. 'qux',
  634. 'qux/baz_100_1.py',
  635. 'qux/baz_1_2.py',
  636. 'qux_0_1.php',
  637. 'qux_1000_1.php',
  638. 'qux_1002_0.php',
  639. 'qux_10_2.php',
  640. 'qux_12_0.php',
  641. 'qux_2_0.php',
  642. ]), $finder->in(self::$tmpDir)->getIterator());
  643. }
  644. public function testSortByChangedTime()
  645. {
  646. $finder = $this->buildFinder();
  647. $this->assertSame($finder, $finder->sortByChangedTime());
  648. $this->assertIterator($this->toAbsolute([
  649. 'toto',
  650. 'test.py',
  651. 'test.php',
  652. 'foo/bar.tmp',
  653. 'foo',
  654. 'foo bar',
  655. 'qux',
  656. 'qux/baz_100_1.py',
  657. 'qux/baz_1_2.py',
  658. 'qux_0_1.php',
  659. 'qux_1000_1.php',
  660. 'qux_1002_0.php',
  661. 'qux_10_2.php',
  662. 'qux_12_0.php',
  663. 'qux_2_0.php',
  664. ]), $finder->in(self::$tmpDir)->getIterator());
  665. }
  666. public function testSortByModifiedTime()
  667. {
  668. $finder = $this->buildFinder();
  669. $this->assertSame($finder, $finder->sortByModifiedTime());
  670. $this->assertIterator($this->toAbsolute([
  671. 'foo/bar.tmp',
  672. 'test.php',
  673. 'toto',
  674. 'test.py',
  675. 'foo',
  676. 'foo bar',
  677. 'qux',
  678. 'qux/baz_100_1.py',
  679. 'qux/baz_1_2.py',
  680. 'qux_0_1.php',
  681. 'qux_1000_1.php',
  682. 'qux_1002_0.php',
  683. 'qux_10_2.php',
  684. 'qux_12_0.php',
  685. 'qux_2_0.php',
  686. ]), $finder->in(self::$tmpDir)->getIterator());
  687. }
  688. public function testReverseSorting()
  689. {
  690. $finder = $this->buildFinder();
  691. $this->assertSame($finder, $finder->sortByName());
  692. $this->assertSame($finder, $finder->reverseSorting());
  693. $this->assertOrderedIteratorInForeach($this->toAbsolute([
  694. 'toto',
  695. 'test.py',
  696. 'test.php',
  697. 'qux_2_0.php',
  698. 'qux_12_0.php',
  699. 'qux_10_2.php',
  700. 'qux_1002_0.php',
  701. 'qux_1000_1.php',
  702. 'qux_0_1.php',
  703. 'qux/baz_1_2.py',
  704. 'qux/baz_100_1.py',
  705. 'qux',
  706. 'foo/bar.tmp',
  707. 'foo bar',
  708. 'foo',
  709. ]), $finder->in(self::$tmpDir)->getIterator());
  710. }
  711. public function testSortByNameNatural()
  712. {
  713. $finder = $this->buildFinder();
  714. $this->assertSame($finder, $finder->sortByName(true));
  715. $this->assertIterator($this->toAbsolute([
  716. 'foo',
  717. 'foo bar',
  718. 'foo/bar.tmp',
  719. 'qux',
  720. 'qux/baz_100_1.py',
  721. 'qux/baz_1_2.py',
  722. 'qux_0_1.php',
  723. 'qux_1000_1.php',
  724. 'qux_1002_0.php',
  725. 'qux_10_2.php',
  726. 'qux_12_0.php',
  727. 'qux_2_0.php',
  728. 'test.php',
  729. 'test.py',
  730. 'toto',
  731. ]), $finder->in(self::$tmpDir)->getIterator());
  732. $finder = $this->buildFinder();
  733. $this->assertSame($finder, $finder->sortByName(false));
  734. $this->assertIterator($this->toAbsolute([
  735. 'foo',
  736. 'foo bar',
  737. 'foo/bar.tmp',
  738. 'qux',
  739. 'qux/baz_100_1.py',
  740. 'qux/baz_1_2.py',
  741. 'qux_0_1.php',
  742. 'qux_1000_1.php',
  743. 'qux_1002_0.php',
  744. 'qux_10_2.php',
  745. 'qux_12_0.php',
  746. 'qux_2_0.php',
  747. 'test.php',
  748. 'test.py',
  749. 'toto',
  750. ]), $finder->in(self::$tmpDir)->getIterator());
  751. }
  752. public function testSort()
  753. {
  754. $finder = $this->buildFinder();
  755. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
  756. $this->assertIterator($this->toAbsolute([
  757. 'foo',
  758. 'foo bar',
  759. 'foo/bar.tmp',
  760. 'test.php',
  761. 'test.py',
  762. 'toto',
  763. 'qux',
  764. 'qux/baz_100_1.py',
  765. 'qux/baz_1_2.py',
  766. 'qux_0_1.php',
  767. 'qux_1000_1.php',
  768. 'qux_1002_0.php',
  769. 'qux_10_2.php',
  770. 'qux_12_0.php',
  771. 'qux_2_0.php',
  772. ]), $finder->in(self::$tmpDir)->getIterator());
  773. }
  774. public function testFilter()
  775. {
  776. $finder = $this->buildFinder();
  777. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
  778. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  779. }
  780. public function testFollowLinks()
  781. {
  782. if ('\\' == \DIRECTORY_SEPARATOR) {
  783. $this->markTestSkipped('symlinks are not supported on Windows');
  784. }
  785. $finder = $this->buildFinder();
  786. $this->assertSame($finder, $finder->followLinks());
  787. $this->assertIterator($this->toAbsolute([
  788. 'foo',
  789. 'foo/bar.tmp',
  790. 'test.php',
  791. 'test.py',
  792. 'toto',
  793. 'foo bar',
  794. 'qux',
  795. 'qux/baz_100_1.py',
  796. 'qux/baz_1_2.py',
  797. 'qux_0_1.php',
  798. 'qux_1000_1.php',
  799. 'qux_1002_0.php',
  800. 'qux_10_2.php',
  801. 'qux_12_0.php',
  802. 'qux_2_0.php',
  803. ]), $finder->in(self::$tmpDir)->getIterator());
  804. }
  805. public function testIn()
  806. {
  807. $finder = $this->buildFinder();
  808. $iterator = $finder->files()->name('*.php')->depth('< 1')->in([self::$tmpDir, __DIR__])->getIterator();
  809. $expected = [
  810. self::$tmpDir.\DIRECTORY_SEPARATOR.'test.php',
  811. __DIR__.\DIRECTORY_SEPARATOR.'GitignoreTest.php',
  812. __DIR__.\DIRECTORY_SEPARATOR.'FinderTest.php',
  813. __DIR__.\DIRECTORY_SEPARATOR.'GlobTest.php',
  814. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_0_1.php',
  815. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1000_1.php',
  816. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1002_0.php',
  817. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_10_2.php',
  818. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_12_0.php',
  819. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_2_0.php',
  820. ];
  821. $this->assertIterator($expected, $iterator);
  822. }
  823. /**
  824. * @expectedException \Symfony\Component\Finder\Exception\DirectoryNotFoundException
  825. */
  826. public function testInWithNonExistentDirectory()
  827. {
  828. $finder = new Finder();
  829. $finder->in('foobar');
  830. }
  831. /**
  832. * @expectedException \InvalidArgumentException
  833. */
  834. public function testInWithNonExistentDirectoryLegacyException()
  835. {
  836. $finder = new Finder();
  837. $finder->in('foobar');
  838. }
  839. public function testInWithGlob()
  840. {
  841. $finder = $this->buildFinder();
  842. $finder->in([__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'])->getIterator();
  843. $this->assertIterator($this->toAbsoluteFixtures(['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']), $finder);
  844. }
  845. /**
  846. * @expectedException \InvalidArgumentException
  847. */
  848. public function testInWithNonDirectoryGlob()
  849. {
  850. $finder = new Finder();
  851. $finder->in(__DIR__.'/Fixtures/A/a*');
  852. }
  853. public function testInWithGlobBrace()
  854. {
  855. if (!\defined('GLOB_BRACE')) {
  856. $this->markTestSkipped('Glob brace is not supported on this system.');
  857. }
  858. $finder = $this->buildFinder();
  859. $finder->in([__DIR__.'/Fixtures/{A,copy/A}/B/C'])->getIterator();
  860. $this->assertIterator($this->toAbsoluteFixtures(['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']), $finder);
  861. }
  862. /**
  863. * @expectedException \LogicException
  864. */
  865. public function testGetIteratorWithoutIn()
  866. {
  867. $finder = Finder::create();
  868. $finder->getIterator();
  869. }
  870. public function testGetIterator()
  871. {
  872. $finder = $this->buildFinder();
  873. $dirs = [];
  874. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  875. $dirs[] = (string) $dir;
  876. }
  877. $expected = $this->toAbsolute(['foo', 'qux', 'toto']);
  878. sort($dirs);
  879. sort($expected);
  880. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  881. $finder = $this->buildFinder();
  882. $this->assertEquals(3, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  883. $finder = $this->buildFinder();
  884. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  885. $a = array_values(array_map('strval', $a));
  886. sort($a);
  887. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  888. }
  889. public function testRelativePath()
  890. {
  891. $finder = $this->buildFinder()->in(self::$tmpDir);
  892. $paths = [];
  893. foreach ($finder as $file) {
  894. $paths[] = $file->getRelativePath();
  895. }
  896. $ref = ['', '', '', '', '', '', '', '', '', '', '', 'foo', 'qux', 'qux', ''];
  897. sort($ref);
  898. sort($paths);
  899. $this->assertEquals($ref, $paths);
  900. }
  901. public function testRelativePathname()
  902. {
  903. $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
  904. $paths = [];
  905. foreach ($finder as $file) {
  906. $paths[] = $file->getRelativePathname();
  907. }
  908. $ref = [
  909. 'test.php',
  910. 'toto',
  911. 'test.py',
  912. 'foo',
  913. 'foo'.\DIRECTORY_SEPARATOR.'bar.tmp',
  914. 'foo bar',
  915. 'qux',
  916. 'qux'.\DIRECTORY_SEPARATOR.'baz_100_1.py',
  917. 'qux'.\DIRECTORY_SEPARATOR.'baz_1_2.py',
  918. 'qux_0_1.php',
  919. 'qux_1000_1.php',
  920. 'qux_1002_0.php',
  921. 'qux_10_2.php',
  922. 'qux_12_0.php',
  923. 'qux_2_0.php',
  924. ];
  925. sort($paths);
  926. sort($ref);
  927. $this->assertEquals($ref, $paths);
  928. }
  929. public function testGetFilenameWithoutExtension()
  930. {
  931. $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
  932. $fileNames = [];
  933. foreach ($finder as $file) {
  934. $fileNames[] = $file->getFilenameWithoutExtension();
  935. }
  936. $ref = [
  937. 'test',
  938. 'toto',
  939. 'test',
  940. 'foo',
  941. 'bar',
  942. 'foo bar',
  943. 'qux',
  944. 'baz_100_1',
  945. 'baz_1_2',
  946. 'qux_0_1',
  947. 'qux_1000_1',
  948. 'qux_1002_0',
  949. 'qux_10_2',
  950. 'qux_12_0',
  951. 'qux_2_0',
  952. ];
  953. sort($fileNames);
  954. sort($ref);
  955. $this->assertEquals($ref, $fileNames);
  956. }
  957. public function testAppendWithAFinder()
  958. {
  959. $finder = $this->buildFinder();
  960. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  961. $finder1 = $this->buildFinder();
  962. $finder1->directories()->in(self::$tmpDir);
  963. $finder = $finder->append($finder1);
  964. $this->assertIterator($this->toAbsolute(['foo', 'foo/bar.tmp', 'qux', 'toto']), $finder->getIterator());
  965. }
  966. public function testAppendWithAnArray()
  967. {
  968. $finder = $this->buildFinder();
  969. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  970. $finder->append($this->toAbsolute(['foo', 'toto']));
  971. $this->assertIterator($this->toAbsolute(['foo', 'foo/bar.tmp', 'toto']), $finder->getIterator());
  972. }
  973. public function testAppendReturnsAFinder()
  974. {
  975. $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append([]));
  976. }
  977. public function testAppendDoesNotRequireIn()
  978. {
  979. $finder = $this->buildFinder();
  980. $finder->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  981. $finder1 = Finder::create()->append($finder);
  982. $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
  983. }
  984. public function testCountDirectories()
  985. {
  986. $directory = Finder::create()->directories()->in(self::$tmpDir);
  987. $i = 0;
  988. foreach ($directory as $dir) {
  989. ++$i;
  990. }
  991. $this->assertCount($i, $directory);
  992. }
  993. public function testCountFiles()
  994. {
  995. $files = Finder::create()->files()->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  996. $i = 0;
  997. foreach ($files as $file) {
  998. ++$i;
  999. }
  1000. $this->assertCount($i, $files);
  1001. }
  1002. /**
  1003. * @expectedException \LogicException
  1004. */
  1005. public function testCountWithoutIn()
  1006. {
  1007. $finder = Finder::create()->files();
  1008. \count($finder);
  1009. }
  1010. public function testHasResults()
  1011. {
  1012. $finder = $this->buildFinder();
  1013. $finder->in(__DIR__);
  1014. $this->assertTrue($finder->hasResults());
  1015. }
  1016. public function testNoResults()
  1017. {
  1018. $finder = $this->buildFinder();
  1019. $finder->in(__DIR__)->name('DoesNotExist');
  1020. $this->assertFalse($finder->hasResults());
  1021. }
  1022. /**
  1023. * @dataProvider getContainsTestData
  1024. */
  1025. public function testContains($matchPatterns, $noMatchPatterns, $expected)
  1026. {
  1027. $finder = $this->buildFinder();
  1028. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  1029. ->name('*.txt')->sortByName()
  1030. ->contains($matchPatterns)
  1031. ->notContains($noMatchPatterns);
  1032. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  1033. }
  1034. public function testContainsOnDirectory()
  1035. {
  1036. $finder = $this->buildFinder();
  1037. $finder->in(__DIR__)
  1038. ->directories()
  1039. ->name('Fixtures')
  1040. ->contains('abc');
  1041. $this->assertIterator([], $finder);
  1042. }
  1043. public function testNotContainsOnDirectory()
  1044. {
  1045. $finder = $this->buildFinder();
  1046. $finder->in(__DIR__)
  1047. ->directories()
  1048. ->name('Fixtures')
  1049. ->notContains('abc');
  1050. $this->assertIterator([], $finder);
  1051. }
  1052. /**
  1053. * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
  1054. * with inner FilesystemIterator in an invalid state.
  1055. *
  1056. * @see https://bugs.php.net/68557
  1057. */
  1058. public function testMultipleLocations()
  1059. {
  1060. $locations = [
  1061. self::$tmpDir.'/',
  1062. self::$tmpDir.'/toto/',
  1063. ];
  1064. // it is expected that there are test.py test.php in the tmpDir
  1065. $finder = new Finder();
  1066. $finder->in($locations)
  1067. // the default flag IGNORE_DOT_FILES fixes the problem indirectly
  1068. // so we set it to false for better isolation
  1069. ->ignoreDotFiles(false)
  1070. ->depth('< 1')->name('test.php');
  1071. $this->assertCount(1, $finder);
  1072. }
  1073. /**
  1074. * Searching in multiple locations with sub directories involves
  1075. * AppendIterator which does an unnecessary rewind which leaves
  1076. * FilterIterator with inner FilesystemIterator in an invalid state.
  1077. *
  1078. * @see https://bugs.php.net/68557
  1079. */
  1080. public function testMultipleLocationsWithSubDirectories()
  1081. {
  1082. $locations = [
  1083. __DIR__.'/Fixtures/one',
  1084. self::$tmpDir.\DIRECTORY_SEPARATOR.'toto',
  1085. ];
  1086. $finder = $this->buildFinder();
  1087. $finder->in($locations)->depth('< 10')->name('*.neon');
  1088. $expected = [
  1089. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
  1090. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
  1091. ];
  1092. $this->assertIterator($expected, $finder);
  1093. $this->assertIteratorInForeach($expected, $finder);
  1094. }
  1095. /**
  1096. * Iterator keys must be the file pathname.
  1097. */
  1098. public function testIteratorKeys()
  1099. {
  1100. $finder = $this->buildFinder()->in(self::$tmpDir);
  1101. foreach ($finder as $key => $file) {
  1102. $this->assertEquals($file->getPathname(), $key);
  1103. }
  1104. }
  1105. public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
  1106. {
  1107. $finder = $this->buildFinder();
  1108. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
  1109. ->path('/^dir/');
  1110. $expected = ['r+e.gex[c]a(r)s'.\DIRECTORY_SEPARATOR.'dir', 'r+e.gex[c]a(r)s'.\DIRECTORY_SEPARATOR.'dir'.\DIRECTORY_SEPARATOR.'bar.dat'];
  1111. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  1112. }
  1113. public function getContainsTestData()
  1114. {
  1115. return [
  1116. ['', '', []],
  1117. ['foo', 'bar', []],
  1118. ['', 'foobar', ['dolor.txt', 'ipsum.txt', 'lorem.txt']],
  1119. ['lorem ipsum dolor sit amet', 'foobar', ['lorem.txt']],
  1120. ['sit', 'bar', ['dolor.txt', 'ipsum.txt', 'lorem.txt']],
  1121. ['dolor sit amet', '@^L@m', ['dolor.txt', 'ipsum.txt']],
  1122. ['/^lorem ipsum dolor sit amet$/m', 'foobar', ['lorem.txt']],
  1123. ['lorem', 'foobar', ['lorem.txt']],
  1124. ['', 'lorem', ['dolor.txt', 'ipsum.txt']],
  1125. ['ipsum dolor sit amet', '/^IPSUM/m', ['lorem.txt']],
  1126. [['lorem', 'dolor'], [], ['lorem.txt', 'ipsum.txt', 'dolor.txt']],
  1127. ['', ['lorem', 'ipsum'], ['dolor.txt']],
  1128. ];
  1129. }
  1130. public function getRegexNameTestData()
  1131. {
  1132. return [
  1133. ['~.*t\\.p.+~i'],
  1134. ['~t.*s~i'],
  1135. ];
  1136. }
  1137. /**
  1138. * @dataProvider getTestPathData
  1139. */
  1140. public function testPath($matchPatterns, $noMatchPatterns, array $expected)
  1141. {
  1142. $finder = $this->buildFinder();
  1143. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  1144. ->path($matchPatterns)
  1145. ->notPath($noMatchPatterns);
  1146. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  1147. }
  1148. public function getTestPathData()
  1149. {
  1150. return [
  1151. ['', '', []],
  1152. ['/^A\/B\/C/', '/C$/',
  1153. ['A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat'],
  1154. ],
  1155. ['/^A\/B/', 'foobar',
  1156. [
  1157. 'A'.\DIRECTORY_SEPARATOR.'B',
  1158. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1159. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1160. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1161. ],
  1162. ],
  1163. ['A/B/C', 'foobar',
  1164. [
  1165. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1166. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1167. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1168. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  1169. ],
  1170. ],
  1171. ['A/B', 'foobar',
  1172. [
  1173. //dirs
  1174. 'A'.\DIRECTORY_SEPARATOR.'B',
  1175. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1176. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B',
  1177. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1178. //files
  1179. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1180. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1181. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat.copy',
  1182. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  1183. ],
  1184. ],
  1185. ['/^with space\//', 'foobar',
  1186. [
  1187. 'with space'.\DIRECTORY_SEPARATOR.'foo.txt',
  1188. ],
  1189. ],
  1190. [
  1191. '/^A/',
  1192. ['a.dat', 'abc.dat'],
  1193. [
  1194. 'A',
  1195. 'A'.\DIRECTORY_SEPARATOR.'B',
  1196. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1197. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1198. ],
  1199. ],
  1200. [
  1201. ['/^A/', 'one'],
  1202. 'foobar',
  1203. [
  1204. 'A',
  1205. 'A'.\DIRECTORY_SEPARATOR.'B',
  1206. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1207. 'A'.\DIRECTORY_SEPARATOR.'a.dat',
  1208. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1209. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1210. 'one',
  1211. 'one'.\DIRECTORY_SEPARATOR.'a',
  1212. 'one'.\DIRECTORY_SEPARATOR.'b',
  1213. 'one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
  1214. 'one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
  1215. ],
  1216. ],
  1217. ];
  1218. }
  1219. public function testAccessDeniedException()
  1220. {
  1221. if ('\\' === \DIRECTORY_SEPARATOR) {
  1222. $this->markTestSkipped('chmod is not supported on Windows');
  1223. }
  1224. $finder = $this->buildFinder();
  1225. $finder->files()->in(self::$tmpDir);
  1226. // make 'foo' directory non-readable
  1227. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  1228. chmod($testDir, 0333);
  1229. if (false === $couldRead = is_readable($testDir)) {
  1230. try {
  1231. $this->assertIterator($this->toAbsolute(['foo bar', 'test.php', 'test.py']), $finder->getIterator());
  1232. $this->fail('Finder should throw an exception when opening a non-readable directory.');
  1233. } catch (\Exception $e) {
  1234. $expectedExceptionClass = 'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
  1235. if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
  1236. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  1237. }
  1238. if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
  1239. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  1240. }
  1241. $this->assertInstanceOf($expectedExceptionClass, $e);
  1242. }
  1243. }
  1244. // restore original permissions
  1245. chmod($testDir, 0777);
  1246. clearstatcache(true, $testDir);
  1247. if ($couldRead) {
  1248. $this->markTestSkipped('could read test files while test requires unreadable');
  1249. }
  1250. }
  1251. public function testIgnoredAccessDeniedException()
  1252. {
  1253. if ('\\' === \DIRECTORY_SEPARATOR) {
  1254. $this->markTestSkipped('chmod is not supported on Windows');
  1255. }
  1256. $finder = $this->buildFinder();
  1257. $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
  1258. // make 'foo' directory non-readable
  1259. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  1260. chmod($testDir, 0333);
  1261. if (false === ($couldRead = is_readable($testDir))) {
  1262. $this->assertIterator($this->toAbsolute([
  1263. 'foo bar',
  1264. 'test.php',
  1265. 'test.py',
  1266. 'qux/baz_100_1.py',
  1267. 'qux/baz_1_2.py',
  1268. 'qux_0_1.php',
  1269. 'qux_1000_1.php',
  1270. 'qux_1002_0.php',
  1271. 'qux_10_2.php',
  1272. 'qux_12_0.php',
  1273. 'qux_2_0.php',
  1274. ]
  1275. ), $finder->getIterator());
  1276. }
  1277. // restore original permissions
  1278. chmod($testDir, 0777);
  1279. clearstatcache(true, $testDir);
  1280. if ($couldRead) {
  1281. $this->markTestSkipped('could read test files while test requires unreadable');
  1282. }
  1283. }
  1284. /**
  1285. * @group legacy
  1286. * @expectedDeprecation The "Symfony\Component\Finder\Finder::sortByName()" method will have a new "bool $useNaturalSort = false" argument in version 5.0, not defining it is deprecated since Symfony 4.2.
  1287. */
  1288. public function testInheritedClassCallSortByNameWithNoArguments()
  1289. {
  1290. $finderChild = new ClassThatInheritFinder();
  1291. $finderChild->sortByName();
  1292. }
  1293. protected function buildFinder()
  1294. {
  1295. return Finder::create();
  1296. }
  1297. }
  1298. class ClassThatInheritFinder extends Finder
  1299. {
  1300. public function sortByName()
  1301. {
  1302. parent::sortByName();
  1303. }
  1304. }