FinderTest.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. }