FinderTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. '.git',
  312. 'foo',
  313. 'foo/bar.tmp',
  314. 'test.php',
  315. 'test.py',
  316. 'toto',
  317. 'toto/.git',
  318. '.bar',
  319. '.foo',
  320. '.foo/.bar',
  321. '.foo/bar',
  322. 'foo bar',
  323. 'qux',
  324. 'qux/baz_100_1.py',
  325. 'qux/baz_1_2.py',
  326. 'qux_0_1.php',
  327. 'qux_1000_1.php',
  328. 'qux_1002_0.php',
  329. 'qux_10_2.php',
  330. 'qux_12_0.php',
  331. 'qux_2_0.php',
  332. ]), $finder->in(self::$tmpDir)->getIterator());
  333. $finder = $this->buildFinder();
  334. $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
  335. $this->assertIterator($this->toAbsolute([
  336. '.git',
  337. 'foo',
  338. 'foo/bar.tmp',
  339. 'test.php',
  340. 'test.py',
  341. 'toto',
  342. 'toto/.git',
  343. '.bar',
  344. '.foo',
  345. '.foo/.bar',
  346. '.foo/bar',
  347. 'foo bar',
  348. 'qux',
  349. 'qux/baz_100_1.py',
  350. 'qux/baz_1_2.py',
  351. 'qux_0_1.php',
  352. 'qux_1000_1.php',
  353. 'qux_1002_0.php',
  354. 'qux_10_2.php',
  355. 'qux_12_0.php',
  356. 'qux_2_0.php',
  357. ]), $finder->in(self::$tmpDir)->getIterator());
  358. $finder = $this->buildFinder();
  359. $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
  360. $this->assertIterator($this->toAbsolute([
  361. 'foo',
  362. 'foo/bar.tmp',
  363. 'test.php',
  364. 'test.py',
  365. 'toto',
  366. '.bar',
  367. '.foo',
  368. '.foo/.bar',
  369. '.foo/bar',
  370. 'foo bar',
  371. 'qux',
  372. 'qux/baz_100_1.py',
  373. 'qux/baz_1_2.py',
  374. 'qux_0_1.php',
  375. 'qux_1000_1.php',
  376. 'qux_1002_0.php',
  377. 'qux_10_2.php',
  378. 'qux_12_0.php',
  379. 'qux_2_0.php',
  380. ]), $finder->in(self::$tmpDir)->getIterator());
  381. }
  382. public function testIgnoreDotFiles()
  383. {
  384. $finder = $this->buildFinder();
  385. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  386. $this->assertIterator($this->toAbsolute([
  387. '.git',
  388. '.bar',
  389. '.foo',
  390. '.foo/.bar',
  391. '.foo/bar',
  392. 'foo',
  393. 'foo/bar.tmp',
  394. 'test.php',
  395. 'test.py',
  396. 'toto',
  397. 'toto/.git',
  398. 'foo bar',
  399. 'qux',
  400. 'qux/baz_100_1.py',
  401. 'qux/baz_1_2.py',
  402. 'qux_0_1.php',
  403. 'qux_1000_1.php',
  404. 'qux_1002_0.php',
  405. 'qux_10_2.php',
  406. 'qux_12_0.php',
  407. 'qux_2_0.php',
  408. ]), $finder->in(self::$tmpDir)->getIterator());
  409. $finder = $this->buildFinder();
  410. $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
  411. $this->assertIterator($this->toAbsolute([
  412. '.git',
  413. '.bar',
  414. '.foo',
  415. '.foo/.bar',
  416. '.foo/bar',
  417. 'foo',
  418. 'foo/bar.tmp',
  419. 'test.php',
  420. 'test.py',
  421. 'toto',
  422. 'toto/.git',
  423. 'foo bar',
  424. 'qux',
  425. 'qux/baz_100_1.py',
  426. 'qux/baz_1_2.py',
  427. 'qux_0_1.php',
  428. 'qux_1000_1.php',
  429. 'qux_1002_0.php',
  430. 'qux_10_2.php',
  431. 'qux_12_0.php',
  432. 'qux_2_0.php',
  433. ]), $finder->in(self::$tmpDir)->getIterator());
  434. $finder = $this->buildFinder();
  435. $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
  436. $this->assertIterator($this->toAbsolute([
  437. 'foo',
  438. 'foo/bar.tmp',
  439. 'test.php',
  440. 'test.py',
  441. 'toto',
  442. 'foo bar',
  443. 'qux',
  444. 'qux/baz_100_1.py',
  445. 'qux/baz_1_2.py',
  446. 'qux_0_1.php',
  447. 'qux_1000_1.php',
  448. 'qux_1002_0.php',
  449. 'qux_10_2.php',
  450. 'qux_12_0.php',
  451. 'qux_2_0.php',
  452. ]), $finder->in(self::$tmpDir)->getIterator());
  453. }
  454. public function testSortByName()
  455. {
  456. $finder = $this->buildFinder();
  457. $this->assertSame($finder, $finder->sortByName());
  458. $this->assertIterator($this->toAbsolute([
  459. 'foo',
  460. 'foo bar',
  461. 'foo/bar.tmp',
  462. 'qux',
  463. 'qux/baz_100_1.py',
  464. 'qux/baz_1_2.py',
  465. 'qux_0_1.php',
  466. 'qux_1000_1.php',
  467. 'qux_1002_0.php',
  468. 'qux_10_2.php',
  469. 'qux_12_0.php',
  470. 'qux_2_0.php',
  471. 'test.php',
  472. 'test.py',
  473. 'toto',
  474. ]), $finder->in(self::$tmpDir)->getIterator());
  475. }
  476. public function testSortByType()
  477. {
  478. $finder = $this->buildFinder();
  479. $this->assertSame($finder, $finder->sortByType());
  480. $this->assertIterator($this->toAbsolute([
  481. 'foo',
  482. 'foo bar',
  483. 'toto',
  484. 'foo/bar.tmp',
  485. 'test.php',
  486. 'test.py',
  487. 'qux',
  488. 'qux/baz_100_1.py',
  489. 'qux/baz_1_2.py',
  490. 'qux_0_1.php',
  491. 'qux_1000_1.php',
  492. 'qux_1002_0.php',
  493. 'qux_10_2.php',
  494. 'qux_12_0.php',
  495. 'qux_2_0.php',
  496. ]), $finder->in(self::$tmpDir)->getIterator());
  497. }
  498. public function testSortByAccessedTime()
  499. {
  500. $finder = $this->buildFinder();
  501. $this->assertSame($finder, $finder->sortByAccessedTime());
  502. $this->assertIterator($this->toAbsolute([
  503. 'foo/bar.tmp',
  504. 'test.php',
  505. 'toto',
  506. 'test.py',
  507. 'foo',
  508. 'foo bar',
  509. 'qux',
  510. 'qux/baz_100_1.py',
  511. 'qux/baz_1_2.py',
  512. 'qux_0_1.php',
  513. 'qux_1000_1.php',
  514. 'qux_1002_0.php',
  515. 'qux_10_2.php',
  516. 'qux_12_0.php',
  517. 'qux_2_0.php',
  518. ]), $finder->in(self::$tmpDir)->getIterator());
  519. }
  520. public function testSortByChangedTime()
  521. {
  522. $finder = $this->buildFinder();
  523. $this->assertSame($finder, $finder->sortByChangedTime());
  524. $this->assertIterator($this->toAbsolute([
  525. 'toto',
  526. 'test.py',
  527. 'test.php',
  528. 'foo/bar.tmp',
  529. 'foo',
  530. 'foo bar',
  531. 'qux',
  532. 'qux/baz_100_1.py',
  533. 'qux/baz_1_2.py',
  534. 'qux_0_1.php',
  535. 'qux_1000_1.php',
  536. 'qux_1002_0.php',
  537. 'qux_10_2.php',
  538. 'qux_12_0.php',
  539. 'qux_2_0.php',
  540. ]), $finder->in(self::$tmpDir)->getIterator());
  541. }
  542. public function testSortByModifiedTime()
  543. {
  544. $finder = $this->buildFinder();
  545. $this->assertSame($finder, $finder->sortByModifiedTime());
  546. $this->assertIterator($this->toAbsolute([
  547. 'foo/bar.tmp',
  548. 'test.php',
  549. 'toto',
  550. 'test.py',
  551. 'foo',
  552. 'foo bar',
  553. 'qux',
  554. 'qux/baz_100_1.py',
  555. 'qux/baz_1_2.py',
  556. 'qux_0_1.php',
  557. 'qux_1000_1.php',
  558. 'qux_1002_0.php',
  559. 'qux_10_2.php',
  560. 'qux_12_0.php',
  561. 'qux_2_0.php',
  562. ]), $finder->in(self::$tmpDir)->getIterator());
  563. }
  564. public function testReverseSorting()
  565. {
  566. $finder = $this->buildFinder();
  567. $this->assertSame($finder, $finder->sortByName());
  568. $this->assertSame($finder, $finder->reverseSorting());
  569. $this->assertOrderedIteratorInForeach($this->toAbsolute([
  570. 'toto',
  571. 'test.py',
  572. 'test.php',
  573. 'qux_2_0.php',
  574. 'qux_12_0.php',
  575. 'qux_10_2.php',
  576. 'qux_1002_0.php',
  577. 'qux_1000_1.php',
  578. 'qux_0_1.php',
  579. 'qux/baz_1_2.py',
  580. 'qux/baz_100_1.py',
  581. 'qux',
  582. 'foo/bar.tmp',
  583. 'foo bar',
  584. 'foo',
  585. ]), $finder->in(self::$tmpDir)->getIterator());
  586. }
  587. public function testSortByNameNatural()
  588. {
  589. $finder = $this->buildFinder();
  590. $this->assertSame($finder, $finder->sortByName(true));
  591. $this->assertIterator($this->toAbsolute([
  592. 'foo',
  593. 'foo bar',
  594. 'foo/bar.tmp',
  595. 'qux',
  596. 'qux/baz_100_1.py',
  597. 'qux/baz_1_2.py',
  598. 'qux_0_1.php',
  599. 'qux_1000_1.php',
  600. 'qux_1002_0.php',
  601. 'qux_10_2.php',
  602. 'qux_12_0.php',
  603. 'qux_2_0.php',
  604. 'test.php',
  605. 'test.py',
  606. 'toto',
  607. ]), $finder->in(self::$tmpDir)->getIterator());
  608. $finder = $this->buildFinder();
  609. $this->assertSame($finder, $finder->sortByName(false));
  610. $this->assertIterator($this->toAbsolute([
  611. 'foo',
  612. 'foo bar',
  613. 'foo/bar.tmp',
  614. 'qux',
  615. 'qux/baz_100_1.py',
  616. 'qux/baz_1_2.py',
  617. 'qux_0_1.php',
  618. 'qux_1000_1.php',
  619. 'qux_1002_0.php',
  620. 'qux_10_2.php',
  621. 'qux_12_0.php',
  622. 'qux_2_0.php',
  623. 'test.php',
  624. 'test.py',
  625. 'toto',
  626. ]), $finder->in(self::$tmpDir)->getIterator());
  627. }
  628. public function testSort()
  629. {
  630. $finder = $this->buildFinder();
  631. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
  632. $this->assertIterator($this->toAbsolute([
  633. 'foo',
  634. 'foo bar',
  635. 'foo/bar.tmp',
  636. 'test.php',
  637. 'test.py',
  638. 'toto',
  639. 'qux',
  640. 'qux/baz_100_1.py',
  641. 'qux/baz_1_2.py',
  642. 'qux_0_1.php',
  643. 'qux_1000_1.php',
  644. 'qux_1002_0.php',
  645. 'qux_10_2.php',
  646. 'qux_12_0.php',
  647. 'qux_2_0.php',
  648. ]), $finder->in(self::$tmpDir)->getIterator());
  649. }
  650. public function testFilter()
  651. {
  652. $finder = $this->buildFinder();
  653. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
  654. $this->assertIterator($this->toAbsolute(['test.php', 'test.py']), $finder->in(self::$tmpDir)->getIterator());
  655. }
  656. public function testFollowLinks()
  657. {
  658. if ('\\' == \DIRECTORY_SEPARATOR) {
  659. $this->markTestSkipped('symlinks are not supported on Windows');
  660. }
  661. $finder = $this->buildFinder();
  662. $this->assertSame($finder, $finder->followLinks());
  663. $this->assertIterator($this->toAbsolute([
  664. 'foo',
  665. 'foo/bar.tmp',
  666. 'test.php',
  667. 'test.py',
  668. 'toto',
  669. 'foo bar',
  670. 'qux',
  671. 'qux/baz_100_1.py',
  672. 'qux/baz_1_2.py',
  673. 'qux_0_1.php',
  674. 'qux_1000_1.php',
  675. 'qux_1002_0.php',
  676. 'qux_10_2.php',
  677. 'qux_12_0.php',
  678. 'qux_2_0.php',
  679. ]), $finder->in(self::$tmpDir)->getIterator());
  680. }
  681. public function testIn()
  682. {
  683. $finder = $this->buildFinder();
  684. $iterator = $finder->files()->name('*.php')->depth('< 1')->in([self::$tmpDir, __DIR__])->getIterator();
  685. $expected = [
  686. self::$tmpDir.\DIRECTORY_SEPARATOR.'test.php',
  687. __DIR__.\DIRECTORY_SEPARATOR.'FinderTest.php',
  688. __DIR__.\DIRECTORY_SEPARATOR.'GlobTest.php',
  689. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_0_1.php',
  690. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1000_1.php',
  691. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_1002_0.php',
  692. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_10_2.php',
  693. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_12_0.php',
  694. self::$tmpDir.\DIRECTORY_SEPARATOR.'qux_2_0.php',
  695. ];
  696. $this->assertIterator($expected, $iterator);
  697. }
  698. /**
  699. * @expectedException \InvalidArgumentException
  700. */
  701. public function testInWithNonExistentDirectory()
  702. {
  703. $finder = new Finder();
  704. $finder->in('foobar');
  705. }
  706. public function testInWithGlob()
  707. {
  708. $finder = $this->buildFinder();
  709. $finder->in([__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'])->getIterator();
  710. $this->assertIterator($this->toAbsoluteFixtures(['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']), $finder);
  711. }
  712. /**
  713. * @expectedException \InvalidArgumentException
  714. */
  715. public function testInWithNonDirectoryGlob()
  716. {
  717. $finder = new Finder();
  718. $finder->in(__DIR__.'/Fixtures/A/a*');
  719. }
  720. public function testInWithGlobBrace()
  721. {
  722. $finder = $this->buildFinder();
  723. $finder->in([__DIR__.'/Fixtures/{A,copy/A}/B/C'])->getIterator();
  724. $this->assertIterator($this->toAbsoluteFixtures(['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']), $finder);
  725. }
  726. /**
  727. * @expectedException \LogicException
  728. */
  729. public function testGetIteratorWithoutIn()
  730. {
  731. $finder = Finder::create();
  732. $finder->getIterator();
  733. }
  734. public function testGetIterator()
  735. {
  736. $finder = $this->buildFinder();
  737. $dirs = [];
  738. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  739. $dirs[] = (string) $dir;
  740. }
  741. $expected = $this->toAbsolute(['foo', 'qux', 'toto']);
  742. sort($dirs);
  743. sort($expected);
  744. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  745. $finder = $this->buildFinder();
  746. $this->assertEquals(3, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  747. $finder = $this->buildFinder();
  748. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  749. $a = array_values(array_map('strval', $a));
  750. sort($a);
  751. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  752. }
  753. public function testRelativePath()
  754. {
  755. $finder = $this->buildFinder()->in(self::$tmpDir);
  756. $paths = [];
  757. foreach ($finder as $file) {
  758. $paths[] = $file->getRelativePath();
  759. }
  760. $ref = ['', '', '', '', '', '', '', '', '', '', '', 'foo', 'qux', 'qux', ''];
  761. sort($ref);
  762. sort($paths);
  763. $this->assertEquals($ref, $paths);
  764. }
  765. public function testRelativePathname()
  766. {
  767. $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
  768. $paths = [];
  769. foreach ($finder as $file) {
  770. $paths[] = $file->getRelativePathname();
  771. }
  772. $ref = [
  773. 'test.php',
  774. 'toto',
  775. 'test.py',
  776. 'foo',
  777. 'foo'.\DIRECTORY_SEPARATOR.'bar.tmp',
  778. 'foo bar',
  779. 'qux',
  780. 'qux'.\DIRECTORY_SEPARATOR.'baz_100_1.py',
  781. 'qux'.\DIRECTORY_SEPARATOR.'baz_1_2.py',
  782. 'qux_0_1.php',
  783. 'qux_1000_1.php',
  784. 'qux_1002_0.php',
  785. 'qux_10_2.php',
  786. 'qux_12_0.php',
  787. 'qux_2_0.php',
  788. ];
  789. sort($paths);
  790. sort($ref);
  791. $this->assertEquals($ref, $paths);
  792. }
  793. public function testAppendWithAFinder()
  794. {
  795. $finder = $this->buildFinder();
  796. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  797. $finder1 = $this->buildFinder();
  798. $finder1->directories()->in(self::$tmpDir);
  799. $finder = $finder->append($finder1);
  800. $this->assertIterator($this->toAbsolute(['foo', 'foo/bar.tmp', 'qux', 'toto']), $finder->getIterator());
  801. }
  802. public function testAppendWithAnArray()
  803. {
  804. $finder = $this->buildFinder();
  805. $finder->files()->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  806. $finder->append($this->toAbsolute(['foo', 'toto']));
  807. $this->assertIterator($this->toAbsolute(['foo', 'foo/bar.tmp', 'toto']), $finder->getIterator());
  808. }
  809. public function testAppendReturnsAFinder()
  810. {
  811. $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append([]));
  812. }
  813. public function testAppendDoesNotRequireIn()
  814. {
  815. $finder = $this->buildFinder();
  816. $finder->in(self::$tmpDir.\DIRECTORY_SEPARATOR.'foo');
  817. $finder1 = Finder::create()->append($finder);
  818. $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
  819. }
  820. public function testCountDirectories()
  821. {
  822. $directory = Finder::create()->directories()->in(self::$tmpDir);
  823. $i = 0;
  824. foreach ($directory as $dir) {
  825. ++$i;
  826. }
  827. $this->assertCount($i, $directory);
  828. }
  829. public function testCountFiles()
  830. {
  831. $files = Finder::create()->files()->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  832. $i = 0;
  833. foreach ($files as $file) {
  834. ++$i;
  835. }
  836. $this->assertCount($i, $files);
  837. }
  838. /**
  839. * @expectedException \LogicException
  840. */
  841. public function testCountWithoutIn()
  842. {
  843. $finder = Finder::create()->files();
  844. \count($finder);
  845. }
  846. public function testHasResults()
  847. {
  848. $finder = $this->buildFinder();
  849. $finder->in(__DIR__);
  850. $this->assertTrue($finder->hasResults());
  851. }
  852. public function testNoResults()
  853. {
  854. $finder = $this->buildFinder();
  855. $finder->in(__DIR__)->name('DoesNotExist');
  856. $this->assertFalse($finder->hasResults());
  857. }
  858. /**
  859. * @dataProvider getContainsTestData
  860. */
  861. public function testContains($matchPatterns, $noMatchPatterns, $expected)
  862. {
  863. $finder = $this->buildFinder();
  864. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  865. ->name('*.txt')->sortByName()
  866. ->contains($matchPatterns)
  867. ->notContains($noMatchPatterns);
  868. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  869. }
  870. public function testContainsOnDirectory()
  871. {
  872. $finder = $this->buildFinder();
  873. $finder->in(__DIR__)
  874. ->directories()
  875. ->name('Fixtures')
  876. ->contains('abc');
  877. $this->assertIterator([], $finder);
  878. }
  879. public function testNotContainsOnDirectory()
  880. {
  881. $finder = $this->buildFinder();
  882. $finder->in(__DIR__)
  883. ->directories()
  884. ->name('Fixtures')
  885. ->notContains('abc');
  886. $this->assertIterator([], $finder);
  887. }
  888. /**
  889. * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
  890. * with inner FilesystemIterator in an invalid state.
  891. *
  892. * @see https://bugs.php.net/68557
  893. */
  894. public function testMultipleLocations()
  895. {
  896. $locations = [
  897. self::$tmpDir.'/',
  898. self::$tmpDir.'/toto/',
  899. ];
  900. // it is expected that there are test.py test.php in the tmpDir
  901. $finder = new Finder();
  902. $finder->in($locations)
  903. // the default flag IGNORE_DOT_FILES fixes the problem indirectly
  904. // so we set it to false for better isolation
  905. ->ignoreDotFiles(false)
  906. ->depth('< 1')->name('test.php');
  907. $this->assertCount(1, $finder);
  908. }
  909. /**
  910. * Searching in multiple locations with sub directories involves
  911. * AppendIterator which does an unnecessary rewind which leaves
  912. * FilterIterator with inner FilesystemIterator in an invalid state.
  913. *
  914. * @see https://bugs.php.net/68557
  915. */
  916. public function testMultipleLocationsWithSubDirectories()
  917. {
  918. $locations = [
  919. __DIR__.'/Fixtures/one',
  920. self::$tmpDir.\DIRECTORY_SEPARATOR.'toto',
  921. ];
  922. $finder = $this->buildFinder();
  923. $finder->in($locations)->depth('< 10')->name('*.neon');
  924. $expected = [
  925. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
  926. __DIR__.'/Fixtures/one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
  927. ];
  928. $this->assertIterator($expected, $finder);
  929. $this->assertIteratorInForeach($expected, $finder);
  930. }
  931. /**
  932. * Iterator keys must be the file pathname.
  933. */
  934. public function testIteratorKeys()
  935. {
  936. $finder = $this->buildFinder()->in(self::$tmpDir);
  937. foreach ($finder as $key => $file) {
  938. $this->assertEquals($file->getPathname(), $key);
  939. }
  940. }
  941. public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
  942. {
  943. $finder = $this->buildFinder();
  944. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
  945. ->path('/^dir/');
  946. $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'];
  947. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  948. }
  949. public function getContainsTestData()
  950. {
  951. return [
  952. ['', '', []],
  953. ['foo', 'bar', []],
  954. ['', 'foobar', ['dolor.txt', 'ipsum.txt', 'lorem.txt']],
  955. ['lorem ipsum dolor sit amet', 'foobar', ['lorem.txt']],
  956. ['sit', 'bar', ['dolor.txt', 'ipsum.txt', 'lorem.txt']],
  957. ['dolor sit amet', '@^L@m', ['dolor.txt', 'ipsum.txt']],
  958. ['/^lorem ipsum dolor sit amet$/m', 'foobar', ['lorem.txt']],
  959. ['lorem', 'foobar', ['lorem.txt']],
  960. ['', 'lorem', ['dolor.txt', 'ipsum.txt']],
  961. ['ipsum dolor sit amet', '/^IPSUM/m', ['lorem.txt']],
  962. [['lorem', 'dolor'], [], ['lorem.txt', 'ipsum.txt', 'dolor.txt']],
  963. ['', ['lorem', 'ipsum'], ['dolor.txt']],
  964. ];
  965. }
  966. public function getRegexNameTestData()
  967. {
  968. return [
  969. ['~.*t\\.p.+~i'],
  970. ['~t.*s~i'],
  971. ];
  972. }
  973. /**
  974. * @dataProvider getTestPathData
  975. */
  976. public function testPath($matchPatterns, $noMatchPatterns, array $expected)
  977. {
  978. $finder = $this->buildFinder();
  979. $finder->in(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')
  980. ->path($matchPatterns)
  981. ->notPath($noMatchPatterns);
  982. $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
  983. }
  984. public function getTestPathData()
  985. {
  986. return [
  987. ['', '', []],
  988. ['/^A\/B\/C/', '/C$/',
  989. ['A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat'],
  990. ],
  991. ['/^A\/B/', 'foobar',
  992. [
  993. 'A'.\DIRECTORY_SEPARATOR.'B',
  994. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  995. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  996. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  997. ],
  998. ],
  999. ['A/B/C', 'foobar',
  1000. [
  1001. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1002. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1003. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1004. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  1005. ],
  1006. ],
  1007. ['A/B', 'foobar',
  1008. [
  1009. //dirs
  1010. 'A'.\DIRECTORY_SEPARATOR.'B',
  1011. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1012. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B',
  1013. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1014. //files
  1015. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1016. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1017. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat.copy',
  1018. 'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy',
  1019. ],
  1020. ],
  1021. ['/^with space\//', 'foobar',
  1022. [
  1023. 'with space'.\DIRECTORY_SEPARATOR.'foo.txt',
  1024. ],
  1025. ],
  1026. [
  1027. '/^A/',
  1028. ['a.dat', 'abc.dat'],
  1029. [
  1030. 'A',
  1031. 'A'.\DIRECTORY_SEPARATOR.'B',
  1032. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1033. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1034. ],
  1035. ],
  1036. [
  1037. ['/^A/', 'one'],
  1038. 'foobar',
  1039. [
  1040. 'A',
  1041. 'A'.\DIRECTORY_SEPARATOR.'B',
  1042. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C',
  1043. 'A'.\DIRECTORY_SEPARATOR.'a.dat',
  1044. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat',
  1045. 'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat',
  1046. 'one',
  1047. 'one'.\DIRECTORY_SEPARATOR.'a',
  1048. 'one'.\DIRECTORY_SEPARATOR.'b',
  1049. 'one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'c.neon',
  1050. 'one'.\DIRECTORY_SEPARATOR.'b'.\DIRECTORY_SEPARATOR.'d.neon',
  1051. ],
  1052. ],
  1053. ];
  1054. }
  1055. public function testAccessDeniedException()
  1056. {
  1057. if ('\\' === \DIRECTORY_SEPARATOR) {
  1058. $this->markTestSkipped('chmod is not supported on Windows');
  1059. }
  1060. $finder = $this->buildFinder();
  1061. $finder->files()->in(self::$tmpDir);
  1062. // make 'foo' directory non-readable
  1063. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  1064. chmod($testDir, 0333);
  1065. if (false === $couldRead = is_readable($testDir)) {
  1066. try {
  1067. $this->assertIterator($this->toAbsolute(['foo bar', 'test.php', 'test.py']), $finder->getIterator());
  1068. $this->fail('Finder should throw an exception when opening a non-readable directory.');
  1069. } catch (\Exception $e) {
  1070. $expectedExceptionClass = 'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
  1071. if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
  1072. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  1073. }
  1074. if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
  1075. $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
  1076. }
  1077. $this->assertInstanceOf($expectedExceptionClass, $e);
  1078. }
  1079. }
  1080. // restore original permissions
  1081. chmod($testDir, 0777);
  1082. clearstatcache(true, $testDir);
  1083. if ($couldRead) {
  1084. $this->markTestSkipped('could read test files while test requires unreadable');
  1085. }
  1086. }
  1087. public function testIgnoredAccessDeniedException()
  1088. {
  1089. if ('\\' === \DIRECTORY_SEPARATOR) {
  1090. $this->markTestSkipped('chmod is not supported on Windows');
  1091. }
  1092. $finder = $this->buildFinder();
  1093. $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
  1094. // make 'foo' directory non-readable
  1095. $testDir = self::$tmpDir.\DIRECTORY_SEPARATOR.'foo';
  1096. chmod($testDir, 0333);
  1097. if (false === ($couldRead = is_readable($testDir))) {
  1098. $this->assertIterator($this->toAbsolute([
  1099. 'foo bar',
  1100. 'test.php',
  1101. 'test.py',
  1102. 'qux/baz_100_1.py',
  1103. 'qux/baz_1_2.py',
  1104. 'qux_0_1.php',
  1105. 'qux_1000_1.php',
  1106. 'qux_1002_0.php',
  1107. 'qux_10_2.php',
  1108. 'qux_12_0.php',
  1109. 'qux_2_0.php',
  1110. ]
  1111. ), $finder->getIterator());
  1112. }
  1113. // restore original permissions
  1114. chmod($testDir, 0777);
  1115. clearstatcache(true, $testDir);
  1116. if ($couldRead) {
  1117. $this->markTestSkipped('could read test files while test requires unreadable');
  1118. }
  1119. }
  1120. /**
  1121. * @group legacy
  1122. * @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.
  1123. */
  1124. public function testInheritedClassCallSortByNameWithNoArguments()
  1125. {
  1126. $finderChild = new ClassThatInheritFinder();
  1127. $finderChild->sortByName();
  1128. }
  1129. protected function buildFinder()
  1130. {
  1131. return Finder::create();
  1132. }
  1133. }
  1134. class ClassThatInheritFinder extends Finder
  1135. {
  1136. public function sortByName()
  1137. {
  1138. parent::sortByName();
  1139. }
  1140. }