PathTest.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. <?php
  2. namespace Path\Tests\unit;
  3. use Path\BuiltinProxy;
  4. use Path\Exception\FileExistsException;
  5. use Path\Exception\FileNotFoundException;
  6. use Path\Exception\IOException;
  7. use Path\Path;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. class TestablePath extends Path {
  11. public function setBuiltin(BuiltinProxy $builtinProxy): void
  12. {
  13. $this->builtin = $builtinProxy;
  14. }
  15. public function cast(string|Path $path): Path
  16. {
  17. return parent::cast($path);
  18. }
  19. }
  20. class PathTest extends TestCase
  21. {
  22. private BuiltinProxy | MockObject $builtin;
  23. public function setUp(): void
  24. {
  25. $this->builtin = $this->getMockBuilder(BuiltinProxy::class)->getMock();
  26. }
  27. public function getMock(string $path, string $methodName): TestablePath | MockObject
  28. {
  29. $mock = $this
  30. ->getMockBuilder(TestablePath::class)
  31. ->setConstructorArgs([$path])
  32. ->setMethodsExcept(['__toString', 'setBuiltin', $methodName])
  33. ->getMock();
  34. $mock->setBuiltin($this->builtin);
  35. return $mock;
  36. }
  37. /**
  38. * Test 'join' method.
  39. */
  40. public function testJoin(): void
  41. {
  42. // One part
  43. $this->assertEquals(
  44. '/home/user',
  45. Path::join('/home', 'user')
  46. );
  47. // Multiple parts
  48. $this->assertEquals(
  49. '/home/user/documents',
  50. Path::join('/home', 'user', 'documents')
  51. );
  52. // Absolute path passed in $parts
  53. $this->assertEquals(
  54. '/user/documents',
  55. Path::join('home', '/user', 'documents')
  56. );
  57. }
  58. public function testToString(): void
  59. {
  60. $path = new Path('/foo/bar');
  61. $this->assertEquals('/foo/bar', $path->__toString());
  62. }
  63. public function testPath()
  64. {
  65. $path = $this->getMock('bar', 'path');
  66. $this->assertEquals(
  67. 'bar',
  68. $path->path()
  69. );
  70. }
  71. public function testEq(): void
  72. {
  73. $path = $this->getMock('bar', 'eq');
  74. $path
  75. ->method('path')
  76. ->willReturn('bar');
  77. $path2 = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  78. $path2
  79. ->method('path')
  80. ->willReturn('bar');
  81. $path3 = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  82. $path3
  83. ->method('path')
  84. ->willReturn('/foo/bar');
  85. $path->method('cast')->willReturnMap(
  86. [
  87. [$path2, $path2],
  88. [$path3, $path3],
  89. ['bar', $path2],
  90. ['/foo/bar', $path3],
  91. ]
  92. );
  93. $this->assertTrue($path->eq($path2));
  94. $this->assertFalse($path->eq($path3));
  95. $this->assertTrue($path->eq('bar'));
  96. $this->assertFalse($path->eq('/foo/bar'));
  97. }
  98. /**
  99. * @throws IOException
  100. */
  101. public function testAbsPath(): void {
  102. $path = $this->getMock('bar', 'absPath');
  103. $this->builtin
  104. ->expects(self::once())
  105. ->method('realpath')
  106. ->with('bar')
  107. ->willReturn('/foo/bar');
  108. $newPath = $this->getMockBuilder(TestablePath::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $newPath->method('path')->willReturn('/foo/bar');
  112. $path->method('cast')->with('/foo/bar')->willReturn($newPath);
  113. $this->assertEquals(
  114. '/foo/bar',
  115. $path->absPath()->path()
  116. );
  117. }
  118. /**
  119. * @throws IOException
  120. */
  121. public function testRealPath()
  122. {
  123. $path = $this->getMock('bar', 'realpath');
  124. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  125. $newPath
  126. ->method('eq')
  127. ->with('/foo/bar')
  128. ->willReturn(True);
  129. $path
  130. ->expects(self::once())
  131. ->method('absPath')
  132. ->willReturn($newPath);
  133. $this->assertTrue(
  134. $path->realpath()->eq('/foo/bar')
  135. );
  136. }
  137. public function testAccessFileExist(): void
  138. {
  139. $path = $this->getMock('bar', 'access');
  140. $this->builtin
  141. ->expects(self::once())
  142. ->method('file_exists')
  143. ->with('bar')
  144. ->willReturn(true);
  145. $this->builtin
  146. ->expects(self::never())
  147. ->method('is_readable')
  148. ->with('bar')
  149. ->willReturn(true);
  150. $this->builtin
  151. ->expects(self::never())
  152. ->method('is_writable')
  153. ->with('bar')
  154. ->willReturn(true);
  155. $this->builtin
  156. ->expects(self::never())
  157. ->method('is_executable')
  158. ->with('bar')
  159. ->willReturn(true);
  160. $this->assertTrue(
  161. $path->access(Path::F_OK)
  162. );
  163. }
  164. public function testAccessIsReadable(): void
  165. {
  166. $path = $this->getMock('bar', 'access');
  167. $this->builtin
  168. ->expects(self::never())
  169. ->method('file_exists')
  170. ->with('bar')
  171. ->willReturn(true);
  172. $this->builtin
  173. ->expects(self::once())
  174. ->method('is_readable')
  175. ->with('bar')
  176. ->willReturn(true);
  177. $this->builtin
  178. ->expects(self::never())
  179. ->method('is_writable')
  180. ->with('bar')
  181. ->willReturn(true);
  182. $this->builtin
  183. ->expects(self::never())
  184. ->method('is_executable')
  185. ->with('bar')
  186. ->willReturn(true);
  187. $this->assertTrue(
  188. $path->access(Path::R_OK)
  189. );
  190. }
  191. public function testAccessIsWritable(): void
  192. {
  193. $path = $this->getMock('bar', 'access');
  194. $this->builtin
  195. ->expects(self::never())
  196. ->method('file_exists')
  197. ->with('bar')
  198. ->willReturn(true);
  199. $this->builtin
  200. ->expects(self::never())
  201. ->method('is_readable')
  202. ->with('bar')
  203. ->willReturn(true);
  204. $this->builtin
  205. ->expects(self::once())
  206. ->method('is_writable')
  207. ->with('bar')
  208. ->willReturn(true);
  209. $this->builtin
  210. ->expects(self::never())
  211. ->method('is_executable')
  212. ->with('bar')
  213. ->willReturn(true);
  214. $this->assertTrue(
  215. $path->access(Path::W_OK)
  216. );
  217. }
  218. public function testAccessIsExecutable(): void
  219. {
  220. $path = $this->getMock('bar', 'access');
  221. $this->builtin
  222. ->expects(self::never())
  223. ->method('file_exists')
  224. ->with('bar')
  225. ->willReturn(true);
  226. $this->builtin
  227. ->expects(self::never())
  228. ->method('is_readable')
  229. ->with('bar')
  230. ->willReturn(true);
  231. $this->builtin
  232. ->expects(self::never())
  233. ->method('is_writable')
  234. ->with('bar')
  235. ->willReturn(true);
  236. $this->builtin
  237. ->expects(self::once())
  238. ->method('is_executable')
  239. ->with('bar')
  240. ->willReturn(true);
  241. $this->assertTrue(
  242. $path->access(Path::X_OK)
  243. );
  244. }
  245. public function testAccessInvalidMode(): void
  246. {
  247. $path = $this->getMock('bar', 'access');
  248. $this->builtin
  249. ->expects(self::never())
  250. ->method('file_exists')
  251. ->with('bar')
  252. ->willReturn(true);
  253. $this->builtin
  254. ->expects(self::never())
  255. ->method('is_readable')
  256. ->with('bar')
  257. ->willReturn(true);
  258. $this->builtin
  259. ->expects(self::never())
  260. ->method('is_writable')
  261. ->with('bar')
  262. ->willReturn(true);
  263. $this->builtin
  264. ->expects(self::never())
  265. ->method('is_executable')
  266. ->with('bar')
  267. ->willReturn(true);
  268. $this->expectException(\RuntimeException::class);
  269. $path->access(-1);
  270. }
  271. public function testATime(): void
  272. {
  273. $path = $this->getMock('bar', 'atime');
  274. $this->builtin
  275. ->expects(self::once())
  276. ->method('fileatime')
  277. ->with('bar')
  278. ->willReturn(1000);
  279. $date = '2000-01-01';
  280. $this->builtin
  281. ->expects(self::once())
  282. ->method('date')
  283. ->with('Y-m-d H:i:s', 1000)
  284. ->willReturn($date);
  285. $this->assertEquals(
  286. $date,
  287. $path->atime()
  288. );
  289. }
  290. public function testATimeError(): void
  291. {
  292. $path = $this->getMock('bar', 'atime');
  293. $this->builtin
  294. ->expects(self::once())
  295. ->method('fileatime')
  296. ->with('bar')
  297. ->willReturn(false);
  298. $this->assertEquals(
  299. null,
  300. $path->atime()
  301. );
  302. }
  303. public function testCTime(): void
  304. {
  305. $path = $this->getMock('bar', 'ctime');
  306. $this->builtin
  307. ->expects(self::once())
  308. ->method('filectime')
  309. ->with('bar')
  310. ->willReturn(1000);
  311. $date = '2000-01-01';
  312. $this->builtin
  313. ->expects(self::once())
  314. ->method('date')
  315. ->with('Y-m-d H:i:s', 1000)
  316. ->willReturn($date);
  317. $this->assertEquals(
  318. $date,
  319. $path->ctime()
  320. );
  321. }
  322. public function testCTimeError(): void
  323. {
  324. $path = $this->getMock('bar', 'ctime');
  325. $this->builtin
  326. ->expects(self::once())
  327. ->method('filectime')
  328. ->with('bar')
  329. ->willReturn(false);
  330. $this->assertEquals(
  331. null,
  332. $path->ctime()
  333. );
  334. }
  335. public function testMTime(): void
  336. {
  337. $path = $this->getMock('bar', 'mtime');
  338. $this->builtin
  339. ->expects(self::once())
  340. ->method('filemtime')
  341. ->with('bar')
  342. ->willReturn(1000);
  343. $date = '2000-01-01';
  344. $this->builtin
  345. ->expects(self::once())
  346. ->method('date')
  347. ->with('Y-m-d H:i:s', 1000)
  348. ->willReturn($date);
  349. $this->assertEquals(
  350. $date,
  351. $path->mtime()
  352. );
  353. }
  354. public function testMTimeError(): void
  355. {
  356. $path = $this->getMock('bar', 'mtime');
  357. $this->builtin
  358. ->expects(self::once())
  359. ->method('filemtime')
  360. ->with('bar')
  361. ->willReturn(false);
  362. $this->assertEquals(
  363. null,
  364. $path->mtime()
  365. );
  366. }
  367. public function testIsFile(): void
  368. {
  369. $path = $this->getMock('bar', 'isFile');
  370. $this->builtin
  371. ->expects(self::once())
  372. ->method('is_file')
  373. ->with('bar')
  374. ->willReturn(true);
  375. $this->assertTrue(
  376. $path->isFile()
  377. );
  378. }
  379. public function testIsDir(): void
  380. {
  381. $path = $this->getMock('bar', 'isDir');
  382. $this->builtin
  383. ->expects(self::once())
  384. ->method('is_dir')
  385. ->with('bar')
  386. ->willReturn(true);
  387. $this->assertTrue(
  388. $path->isDir()
  389. );
  390. }
  391. public function testExt(): void
  392. {
  393. $path = $this->getMock('bar.ext', 'ext');
  394. $this->builtin
  395. ->expects(self::once())
  396. ->method('pathinfo')
  397. ->with('bar.ext', PATHINFO_EXTENSION)
  398. ->willReturn('ext');
  399. $this->assertEquals(
  400. 'ext',
  401. $path->ext()
  402. );
  403. }
  404. public function testBaseName(): void
  405. {
  406. $path = $this->getMock('bar.ext', 'basename');
  407. $this->builtin
  408. ->expects(self::once())
  409. ->method('pathinfo')
  410. ->with('bar.ext', PATHINFO_BASENAME)
  411. ->willReturn('bar.ext');
  412. $this->assertEquals(
  413. 'bar.ext',
  414. $path->basename()
  415. );
  416. }
  417. public function testCD(): void
  418. {
  419. $path = $this->getMock('bar', 'cd');
  420. $this->builtin
  421. ->expects(self::once())
  422. ->method('chdir')
  423. ->with('foo')
  424. ->willReturn(true);
  425. $this->assertTrue(
  426. $path->cd('foo')
  427. );
  428. }
  429. public function testChDir(): void
  430. {
  431. $path = $this->getMock('bar', 'chdir');
  432. $path
  433. ->expects(self::once())
  434. ->method('cd')
  435. ->with('foo')
  436. ->willReturn(true);
  437. $this->assertTrue(
  438. $path->cd('foo')
  439. );
  440. }
  441. public function testName(): void
  442. {
  443. $path = $this->getMock('bar.ext', 'name');
  444. $this->builtin
  445. ->expects(self::once())
  446. ->method('pathinfo')
  447. ->with('bar.ext', PATHINFO_FILENAME)
  448. ->willReturn('bar');
  449. $this->assertEquals(
  450. 'bar',
  451. $path->name()
  452. );
  453. }
  454. /**
  455. * @throws IOException
  456. * @throws FileExistsException
  457. */
  458. public function testMkDir(): void
  459. {
  460. $path = $this->getMock('foo', 'mkdir');
  461. $path->method('isDir')->willReturn(False);
  462. $path->method('isFile')->willReturn(False);
  463. $this->builtin
  464. ->expects(self::once())
  465. ->method('mkdir')
  466. ->with('foo', 0777, false)
  467. ->willReturn(true);
  468. $path->mkdir();
  469. }
  470. /**
  471. * @throws IOException
  472. */
  473. public function testMkDirAlreadyExist(): void
  474. {
  475. $path = $this->getMock('foo', 'mkdir');
  476. $path->method('isDir')->willReturn(True);
  477. $this->expectException(FileExistsException::class);
  478. $path->mkdir();
  479. }
  480. /**
  481. * @throws IOException
  482. * @throws FileExistsException
  483. */
  484. public function testMkDirAlreadyExistButRecursive(): void
  485. {
  486. $path = $this->getMock('foo', 'mkdir');
  487. $path->method('isDir')->willReturn(True);
  488. $this->builtin
  489. ->expects(self::never())
  490. ->method('mkdir');
  491. $path->mkdir(0777, true);
  492. }
  493. /**
  494. * @throws IOException
  495. * @throws FileExistsException
  496. */
  497. public function testMkDirFileExists(): void
  498. {
  499. $path = $this->getMock('foo', 'mkdir');
  500. $path->method('isDir')->willReturn(False);
  501. $path->method('isFile')->willReturn(True);
  502. $this->expectException(FileExistsException::class);
  503. $path->mkdir();
  504. }
  505. /**
  506. * @throws IOException
  507. * @throws FileExistsException
  508. */
  509. public function testMkDirFileExistsButRecursive(): void
  510. {
  511. $path = $this->getMock('foo', 'mkdir');
  512. $path->method('isDir')->willReturn(False);
  513. $path->method('isFile')->willReturn(True);
  514. $this->expectException(FileExistsException::class);
  515. $path->mkdir(0777, true);
  516. }
  517. /**
  518. * @throws IOException
  519. * @throws FileExistsException
  520. */
  521. public function testMkDirIoError(): void
  522. {
  523. $path = $this->getMock('foo', 'mkdir');
  524. $path->method('isDir')->willReturn(False);
  525. $path->method('isFile')->willReturn(False);
  526. $this->builtin
  527. ->expects(self::once())
  528. ->method('mkdir')
  529. ->with('foo', 0777, false)
  530. ->willReturn(false);
  531. $this->expectException(IOException::class);
  532. $path->mkdir();
  533. }
  534. /**
  535. * @throws IOException
  536. * @throws FileNotFoundException
  537. */
  538. public function testDeleteIsFile(): void
  539. {
  540. $path = $this->getMock('foo', 'delete');
  541. $path->method('isFile')->willReturn(True);
  542. $path->method('isDir')->willReturn(False);
  543. $this->builtin
  544. ->expects(self::once())
  545. ->method('unlink')
  546. ->with('foo')
  547. ->willReturn(true);
  548. $path->delete();
  549. }
  550. /**
  551. * @throws IOException
  552. * @throws FileNotFoundException
  553. */
  554. public function testDeleteIsFileWithError(): void
  555. {
  556. $path = $this->getMock('foo', 'delete');
  557. $path->method('isFile')->willReturn(True);
  558. $path->method('isDir')->willReturn(False);
  559. $this->builtin
  560. ->expects(self::once())
  561. ->method('unlink')
  562. ->with('foo')
  563. ->willReturn(false);
  564. $this->expectException(IOException::class);
  565. $path->delete();
  566. }
  567. /**
  568. * @throws IOException
  569. * @throws FileNotFoundException
  570. */
  571. public function testDeleteIsDir(): void
  572. {
  573. $path = $this->getMock('foo', 'delete');
  574. $path->method('isFile')->willReturn(False);
  575. $path->method('isDir')->willReturn(True);
  576. $this->builtin
  577. ->expects(self::once())
  578. ->method('rmdir')
  579. ->with('foo')
  580. ->willReturn(true);
  581. $path->delete();
  582. }
  583. /**
  584. * @throws IOException
  585. * @throws FileNotFoundException
  586. */
  587. public function testDeleteIsDirWithError(): void
  588. {
  589. $path = $this->getMock('foo', 'delete');
  590. $path->method('isFile')->willReturn(False);
  591. $path->method('isDir')->willReturn(True);
  592. $this->builtin
  593. ->expects(self::once())
  594. ->method('rmdir')
  595. ->with('foo')
  596. ->willReturn(false);
  597. $this->expectException(IOException::class);
  598. $path->delete();
  599. }
  600. /**
  601. * @throws IOException
  602. * @throws FileNotFoundException
  603. */
  604. public function testDeleteNonExistentFile(): void
  605. {
  606. $path = $this->getMock('foo', 'delete');
  607. $path->method('isFile')->willReturn(False);
  608. $path->method('isDir')->willReturn(False);
  609. $this->builtin
  610. ->expects(self::never())
  611. ->method('unlink');
  612. $this->builtin
  613. ->expects(self::never())
  614. ->method('rmdir');
  615. $this->expectException(FileNotFoundException::class);
  616. $path->delete();
  617. }
  618. /**
  619. * @throws IOException
  620. * @throws FileNotFoundException
  621. * @throws FileExistsException
  622. */
  623. public function testCopy()
  624. {
  625. $path = $this->getMock('foo.ext', 'copy');
  626. $path->method('isFile')->willReturn(True);
  627. $destination = "/bar/foo2.ext";
  628. $this->builtin
  629. ->expects(self::once())
  630. ->method('is_dir')
  631. ->with($destination)
  632. ->willReturn(False);
  633. $this->builtin
  634. ->expects(self::once())
  635. ->method('file_exists')
  636. ->with($destination)
  637. ->willReturn(False);
  638. $this->builtin
  639. ->expects(self::once())
  640. ->method('copy')
  641. ->with('foo.ext', $destination)
  642. ->willReturn(True);
  643. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  644. $newPath->method('eq')->with($destination)->willReturn(True);
  645. $path->method('cast')->with($destination)->willReturn($newPath);
  646. $result = $path->copy($destination);
  647. $this->assertTrue(
  648. $result->eq($destination)
  649. );
  650. }
  651. /**
  652. * @throws IOException
  653. * @throws FileNotFoundException
  654. * @throws FileExistsException
  655. */
  656. public function testCopyFileDoesNotExist()
  657. {
  658. $path = $this->getMock('foo.ext', 'copy');
  659. $path->method('isFile')->willReturn(False);
  660. $destination = "/bar/foo2.ext";
  661. $this->builtin
  662. ->expects(self::never())
  663. ->method('is_dir');
  664. $this->builtin
  665. ->expects(self::never())
  666. ->method('file_exists');
  667. $this->builtin
  668. ->expects(self::never())
  669. ->method('copy');
  670. $this->expectException(FileNotFoundException::class);
  671. $path->copy($destination);
  672. }
  673. /**
  674. * @throws IOException
  675. * @throws FileNotFoundException
  676. * @throws FileExistsException
  677. */
  678. public function testCopyDestIsDir()
  679. {
  680. $path = $this->getMock('foo.ext', 'copy');
  681. $path->method('isFile')->willReturn(True);
  682. $path->method('basename')->willReturn('foo.ext');
  683. $destination = "/bar";
  684. $this->builtin
  685. ->expects(self::once())
  686. ->method('is_dir')
  687. ->with($destination)
  688. ->willReturn(True);
  689. $newDest = $destination . "/foo.ext";
  690. $this->builtin
  691. ->expects(self::once())
  692. ->method('file_exists')
  693. ->with($newDest)
  694. ->willReturn(False);
  695. $this->builtin
  696. ->expects(self::once())
  697. ->method('copy')
  698. ->with('foo.ext', $newDest)
  699. ->willReturn(True);
  700. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  701. $newPath->method('eq')->with($newDest)->willReturn(True);
  702. $path->method('cast')->with($newDest)->willReturn($newPath);
  703. $result = $path->copy($destination);
  704. $this->assertTrue(
  705. $result->eq($newDest)
  706. );
  707. }
  708. /**
  709. * @throws IOException
  710. * @throws FileNotFoundException
  711. * @throws FileExistsException
  712. */
  713. public function testCopyFileAlreadyExists()
  714. {
  715. $path = $this->getMock('foo.ext', 'copy');
  716. $path->method('isFile')->willReturn(True);
  717. $destination = "/bar/foo2.ext";
  718. $this->builtin
  719. ->expects(self::once())
  720. ->method('is_dir')
  721. ->with($destination)
  722. ->willReturn(False);
  723. $this->builtin
  724. ->expects(self::once())
  725. ->method('file_exists')
  726. ->with($destination)
  727. ->willReturn(True);
  728. $this->builtin
  729. ->expects(self::never())
  730. ->method('copy');
  731. $this->expectException(FileExistsException::class);
  732. $path->copy($destination);
  733. }
  734. /**
  735. * @throws IOException
  736. * @throws FileNotFoundException
  737. * @throws FileExistsException
  738. */
  739. public function testCopyFileDestIsDirButFileAlreadyExists()
  740. {
  741. $path = $this->getMock('foo.ext', 'copy');
  742. $path->method('isFile')->willReturn(True);
  743. $path->method('basename')->willReturn('foo.ext');
  744. $destination = "/bar";
  745. $this->builtin
  746. ->expects(self::once())
  747. ->method('is_dir')
  748. ->with($destination)
  749. ->willReturn(True);
  750. $newDest = $destination . "/foo.ext";
  751. $this->builtin
  752. ->expects(self::once())
  753. ->method('file_exists')
  754. ->with($newDest)
  755. ->willReturn(True);
  756. $this->builtin
  757. ->expects(self::never())
  758. ->method('copy');
  759. $this->expectException(FileExistsException::class);
  760. $path->copy($destination);
  761. }
  762. /**
  763. * @throws IOException
  764. * @throws FileNotFoundException
  765. * @throws FileExistsException
  766. */
  767. public function testCopyFileWithError()
  768. {
  769. $path = $this->getMock('foo.ext', 'copy');
  770. $path->method('isFile')->willReturn(True);
  771. $destination = "/bar/foo2.ext";
  772. $this->builtin
  773. ->expects(self::once())
  774. ->method('is_dir')
  775. ->with($destination)
  776. ->willReturn(False);
  777. $this->builtin
  778. ->expects(self::once())
  779. ->method('file_exists')
  780. ->with($destination)
  781. ->willReturn(False);
  782. $this->builtin
  783. ->expects(self::once())
  784. ->method('copy')
  785. ->with('foo.ext', $destination)
  786. ->willReturn(False);
  787. $this->expectException(IOException::class);
  788. $path->copy($destination);
  789. }
  790. // public function testCopyTree(): void {
  791. // TODO: implement
  792. // }
  793. /**
  794. * @throws IOException
  795. * @throws FileExistsException
  796. */
  797. public function testMove()
  798. {
  799. $path = $this->getMock('foo.ext', 'move');
  800. $destination = "/bar/foo2.ext";
  801. $this->builtin
  802. ->expects(self::once())
  803. ->method('is_dir')
  804. ->with($destination)
  805. ->willReturn(False);
  806. $this->builtin
  807. ->expects(self::once())
  808. ->method('file_exists')
  809. ->with($destination)
  810. ->willReturn(False);
  811. $this->builtin
  812. ->expects(self::once())
  813. ->method('rename')
  814. ->with('foo.ext', $destination)
  815. ->willReturn(True);
  816. $destinationPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  817. $destinationPath->method('eq')->with($destination)->willReturn(True);
  818. $path->method('cast')->with($destination)->willReturn($destinationPath);
  819. $result = $path->move($destination);
  820. $this->assertTrue(
  821. $result->eq($destination)
  822. );
  823. }
  824. /**
  825. * @throws IOException
  826. * @throws FileExistsException
  827. */
  828. public function testMoveWithPath()
  829. {
  830. $path = $this->getMock('foo.ext', 'move');
  831. $destination = new Path("/bar/foo2.ext");
  832. $this->builtin
  833. ->expects(self::once())
  834. ->method('is_dir')
  835. ->with($destination)
  836. ->willReturn(False);
  837. $this->builtin
  838. ->expects(self::once())
  839. ->method('file_exists')
  840. ->with($destination)
  841. ->willReturn(False);
  842. $this->builtin
  843. ->expects(self::once())
  844. ->method('rename')
  845. ->with('foo.ext', $destination)
  846. ->willReturn(True);
  847. $destinationPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  848. $destinationPath->method('eq')->with($destination)->willReturn(True);
  849. $path->method('cast')->with($destination)->willReturn($destinationPath);
  850. $result = $path->move($destination);
  851. $this->assertTrue(
  852. $result->eq($destination)
  853. );
  854. }
  855. /**
  856. * @throws IOException
  857. * @throws FileExistsException
  858. */
  859. public function testMoveDestIsDir()
  860. {
  861. $path = $this->getMock('foo.ext', 'move');
  862. $path->method('basename')->willReturn('foo.ext');
  863. $destination = "/bar";
  864. $this->builtin
  865. ->expects(self::once())
  866. ->method('is_dir')
  867. ->with($destination)
  868. ->willReturn(True);
  869. $newDestination = $destination . "/foo.ext";
  870. $this->builtin
  871. ->expects(self::once())
  872. ->method('file_exists')
  873. ->with($newDestination)
  874. ->willReturn(False);
  875. $this->builtin
  876. ->expects(self::once())
  877. ->method('rename')
  878. ->with('foo.ext', $newDestination)
  879. ->willReturn(True);
  880. $destinationPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  881. $destinationPath->method('eq')->with($newDestination)->willReturn(True);
  882. $path->method('cast')->with($newDestination)->willReturn($destinationPath);
  883. $result = $path->move($destination);
  884. $this->assertTrue(
  885. $result->eq($newDestination)
  886. );
  887. }
  888. /**
  889. * @throws IOException
  890. * @throws FileExistsException
  891. */
  892. public function testMoveFileExist()
  893. {
  894. $path = $this->getMock('foo.ext', 'move');
  895. $path->method('basename')->willReturn('foo.ext');
  896. $destination = "/bar/foo2.ext";
  897. $this->builtin
  898. ->expects(self::once())
  899. ->method('is_dir')
  900. ->with($destination)
  901. ->willReturn(False);
  902. $this->builtin
  903. ->expects(self::once())
  904. ->method('file_exists')
  905. ->with($destination)
  906. ->willReturn(True);
  907. $this->builtin
  908. ->expects(self::never())
  909. ->method('rename');
  910. $this->expectException(FileExistsException::class);
  911. $path->move($destination);
  912. }
  913. /**
  914. * @throws IOException
  915. * @throws FileExistsException
  916. */
  917. public function testMoveWithError()
  918. {
  919. $path = $this->getMock('foo.ext', 'move');
  920. $path->method('basename')->willReturn('foo.ext');
  921. $destination = "/bar/foo2.ext";
  922. $this->builtin
  923. ->expects(self::once())
  924. ->method('is_dir')
  925. ->with($destination)
  926. ->willReturn(False);
  927. $this->builtin
  928. ->expects(self::once())
  929. ->method('file_exists')
  930. ->with($destination)
  931. ->willReturn(False);
  932. $this->builtin
  933. ->expects(self::once())
  934. ->method('rename')
  935. ->with('foo.ext', $destination)
  936. ->willReturn(False);
  937. $this->expectException(IOException::class);
  938. $path->move($destination);
  939. }
  940. /**
  941. * @throws IOException
  942. */
  943. public function testTouchWithNoTimestamps()
  944. {
  945. $path = $this->getMock('foo.ext', 'touch');
  946. $this->builtin
  947. ->expects(self::once())
  948. ->method('touch')
  949. ->with('foo.ext', null, null)
  950. ->willReturn(True);
  951. $path->touch();
  952. }
  953. /**
  954. * @throws IOException
  955. */
  956. public function testTouchWithTimestamps()
  957. {
  958. $path = $this->getMock('foo.ext', 'touch');
  959. $this->builtin
  960. ->expects(self::once())
  961. ->method('touch')
  962. ->with('foo.ext', 123, 456)
  963. ->willReturn(True);
  964. $path->touch(123, 456);
  965. }
  966. /**
  967. * @throws IOException
  968. */
  969. public function testTouchWithDatetimes()
  970. {
  971. $path = $this->getMock('foo.ext', 'touch');
  972. $datetime1 = $this->getMockBuilder(\DateTime::class)->getMock();
  973. $datetime1->method('getTimestamp')->willReturn(123);
  974. $datetime2 = $this->getMockBuilder(\DateTime::class)->getMock();
  975. $datetime2->method('getTimestamp')->willReturn(456);
  976. $this->builtin
  977. ->expects(self::once())
  978. ->method('touch')
  979. ->with('foo.ext', 123, 456)
  980. ->willReturn(True);
  981. $path->touch($datetime1, $datetime2);
  982. }
  983. /**
  984. * @throws IOException
  985. */
  986. public function testTouchWithError()
  987. {
  988. $path = $this->getMock('foo.ext', 'touch');
  989. $this->builtin
  990. ->expects(self::once())
  991. ->method('touch')
  992. ->with('foo.ext')
  993. ->willReturn(False);
  994. $this->expectException(IOException::class);
  995. $path->touch();
  996. }
  997. /**
  998. * @throws IOException
  999. * @throws FileNotFoundException
  1000. */
  1001. public function testSize()
  1002. {
  1003. $path = $this->getMock('foo.ext', 'size');
  1004. $path->method('isFile')->willReturn(True);
  1005. $this->builtin
  1006. ->expects(self::once())
  1007. ->method('filesize')
  1008. ->with('foo.ext')
  1009. ->willReturn(123456);
  1010. $this->assertEquals(
  1011. 123456,
  1012. $path->size()
  1013. );
  1014. }
  1015. /**
  1016. * @throws IOException
  1017. * @throws FileNotFoundException
  1018. */
  1019. public function testSizeFileNotExist()
  1020. {
  1021. $path = $this->getMock('foo.ext', 'size');
  1022. $path->method('isFile')->willReturn(False);
  1023. $this->builtin
  1024. ->expects(self::never())
  1025. ->method('filesize');
  1026. $this->expectException(FileNotFoundException::class);
  1027. $path->size();
  1028. }
  1029. /**
  1030. * @throws IOException
  1031. * @throws FileNotFoundException
  1032. */
  1033. public function testSizeFileWithError()
  1034. {
  1035. $path = $this->getMock('foo.ext', 'size');
  1036. $path->method('isFile')->willReturn(True);
  1037. $this->builtin
  1038. ->expects(self::once())
  1039. ->method('filesize')
  1040. ->with('foo.ext')
  1041. ->willReturn(False);
  1042. $this->expectException(IOException::class);
  1043. $path->size();
  1044. }
  1045. public function testParent(): void
  1046. {
  1047. $path = $this->getMock('/foo/foo.ext', 'parent');
  1048. $this->builtin
  1049. ->expects(self::once())
  1050. ->method('dirname')
  1051. ->with('/foo/foo.ext')
  1052. ->willReturn('/foo');
  1053. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  1054. $path->method('cast')->with('/foo')->willReturn($newPath);
  1055. $newPath->method('eq')->with('/foo')->willReturn(True);
  1056. $this->assertTrue(
  1057. $path->parent()->eq('/foo')
  1058. );
  1059. }
  1060. public function testDirName(): void
  1061. {
  1062. $path = $this->getMock('/foo/foo.ext', 'dirname');
  1063. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  1064. $newPath->method('eq')->with('/foo')->willReturn(True);
  1065. $path
  1066. ->expects(self::once())
  1067. ->method('parent')
  1068. ->willReturn($newPath);
  1069. $this->assertTrue(
  1070. $path->dirname()->eq('/foo')
  1071. );
  1072. }
  1073. /**
  1074. * @throws FileNotFoundException
  1075. */
  1076. public function testDirs()
  1077. {
  1078. $path = $this->getMock('/foo', 'dirs');
  1079. $this->builtin
  1080. ->method('is_dir')
  1081. ->willReturnMap(
  1082. [
  1083. ['/foo', True],
  1084. ['/foo/file.ext', False],
  1085. ['/foo/dir1', True],
  1086. ['/foo/dir2', True],
  1087. ]
  1088. );
  1089. $results = [
  1090. '..',
  1091. '.',
  1092. 'file.ext',
  1093. 'dir1',
  1094. 'dir2'
  1095. ];
  1096. $this->builtin
  1097. ->expects(self::once())
  1098. ->method('scandir')
  1099. ->with('/foo')
  1100. ->willReturn($results);
  1101. $this->assertEquals(
  1102. ['dir1', 'dir2'],
  1103. $path->dirs()
  1104. );
  1105. }
  1106. /**
  1107. * @throws FileNotFoundException
  1108. */
  1109. public function testDirsIsNotDir()
  1110. {
  1111. $path = $this->getMock('/foo', 'dirs');
  1112. $this->builtin
  1113. ->method('is_dir')
  1114. ->with('/foo')
  1115. ->willReturn(False);
  1116. $this->expectException(FileNotFoundException::class);
  1117. $path->dirs();
  1118. }
  1119. /**
  1120. * @throws FileNotFoundException
  1121. */
  1122. public function testFiles()
  1123. {
  1124. $path = $this->getMock('/foo', 'files');
  1125. $this->builtin
  1126. ->method('is_dir')
  1127. ->with('/foo')
  1128. ->willReturn(True);
  1129. $this->builtin
  1130. ->method('is_file')
  1131. ->willReturnMap(
  1132. [
  1133. ['/foo/file1.ext', True],
  1134. ['/foo/file2.ext', True],
  1135. ['/foo/dir1', False],
  1136. ]
  1137. );
  1138. $results = [
  1139. '..',
  1140. '.',
  1141. 'file1.ext',
  1142. 'file2.ext',
  1143. 'dir1'
  1144. ];
  1145. $this->builtin
  1146. ->expects(self::once())
  1147. ->method('scandir')
  1148. ->with('/foo')
  1149. ->willReturn($results);
  1150. $this->assertEquals(
  1151. ['file1.ext', 'file2.ext'],
  1152. $path->files()
  1153. );
  1154. }
  1155. /**
  1156. * @throws FileNotFoundException
  1157. */
  1158. public function testFilesIsNotDir()
  1159. {
  1160. $path = $this->getMock('/foo', 'files');
  1161. $this->builtin
  1162. ->method('is_dir')
  1163. ->with('/foo')
  1164. ->willReturn(False);
  1165. $this->expectException(FileNotFoundException::class);
  1166. $path->files();
  1167. }
  1168. // public function testFnMatch(): void
  1169. // {
  1170. // //TODO: implements
  1171. // }
  1172. /**
  1173. * @throws IOException
  1174. * @throws FileNotFoundException
  1175. */
  1176. public function testGetContent(): void
  1177. {
  1178. $path = $this->getMock('/foo/file.ext', 'getContent');
  1179. $this->builtin
  1180. ->method('is_file')
  1181. ->with('/foo/file.ext')
  1182. ->willReturn(True);
  1183. $this->builtin
  1184. ->method('file_get_contents')
  1185. ->with('/foo/file.ext')
  1186. ->willReturn('azerty');
  1187. $this->assertEquals(
  1188. 'azerty',
  1189. $path->getContent()
  1190. );
  1191. }
  1192. /**
  1193. * @throws IOException
  1194. * @throws FileNotFoundException
  1195. */
  1196. public function testGetContentFileDoesNotExist(): void
  1197. {
  1198. $path = $this->getMock('/foo/file.ext', 'getContent');
  1199. $this->builtin
  1200. ->method('is_file')
  1201. ->with('/foo/file.ext')
  1202. ->willReturn(False);
  1203. $this->builtin
  1204. ->expects(self::never())
  1205. ->method('file_get_contents');
  1206. $this->expectException(FileNotFoundException::class);
  1207. $path->getContent();
  1208. }
  1209. /**
  1210. * @throws IOException
  1211. * @throws FileNotFoundException
  1212. */
  1213. public function testGetContentErrorWhileReading(): void
  1214. {
  1215. $path = $this->getMock('/foo/file.ext', 'getContent');
  1216. $this->builtin
  1217. ->method('is_file')
  1218. ->with('/foo/file.ext')
  1219. ->willReturn(True);
  1220. $this->builtin
  1221. ->method('file_get_contents')
  1222. ->with('/foo/file.ext')
  1223. ->willReturn(False);
  1224. $this->expectException(IOException::class);
  1225. $path->getContent();
  1226. }
  1227. /**
  1228. * @throws IOException
  1229. * @throws FileNotFoundException
  1230. */
  1231. public function testPutContent(): void
  1232. {
  1233. $path = $this->getMock('/foo/file.ext', 'putContent');
  1234. $this->builtin
  1235. ->method('is_file')
  1236. ->with('/foo/file.ext')
  1237. ->willReturn(True);
  1238. $this->builtin
  1239. ->expects(self::once())
  1240. ->method('file_put_contents')
  1241. ->with('/foo/file.ext', 'azerty')
  1242. ->willReturn(6);
  1243. $this->assertEquals(
  1244. 6,
  1245. $path->putContent('azerty')
  1246. );
  1247. }
  1248. /**
  1249. * @throws IOException
  1250. * @throws FileNotFoundException
  1251. */
  1252. public function testPutContentAndAppend(): void
  1253. {
  1254. $path = $this->getMock('/foo/file.ext', 'putContent');
  1255. $this->builtin
  1256. ->method('is_file')
  1257. ->with('/foo/file.ext')
  1258. ->willReturn(True);
  1259. $this->builtin
  1260. ->expects(self::once())
  1261. ->method('file_put_contents')
  1262. ->with('/foo/file.ext', 'azerty', FILE_APPEND)
  1263. ->willReturn(6);
  1264. $this->assertEquals(
  1265. 6,
  1266. $path->putContent('azerty', True)
  1267. );
  1268. }
  1269. /**
  1270. * @throws IOException
  1271. * @throws FileNotFoundException
  1272. */
  1273. public function testPutContentFileDoesNotExist(): void
  1274. {
  1275. $path = $this->getMock('/foo/file.ext', 'putContent');
  1276. $this->builtin
  1277. ->method('is_file')
  1278. ->with('/foo/file.ext')
  1279. ->willReturn(False);
  1280. $this->builtin
  1281. ->expects(self::never())
  1282. ->method('file_put_contents');
  1283. $this->expectException(FileNotFoundException::class);
  1284. $path->putContent('azerty');
  1285. }
  1286. /**
  1287. * @throws IOException
  1288. * @throws FileNotFoundException
  1289. */
  1290. public function testPutContentErrorWhileWriting(): void
  1291. {
  1292. $path = $this->getMock('/foo/file.ext', 'putContent');
  1293. $this->builtin
  1294. ->method('is_file')
  1295. ->with('/foo/file.ext')
  1296. ->willReturn(True);
  1297. $this->builtin
  1298. ->method('file_put_contents')
  1299. ->with('/foo/file.ext')
  1300. ->willReturn(False);
  1301. $this->expectException(IOException::class);
  1302. $path->putContent('azerty');
  1303. }
  1304. /**
  1305. * @throws IOException
  1306. * @throws FileNotFoundException
  1307. */
  1308. public function testPutLines(): void
  1309. {
  1310. $path = $this->getMock('/foo/file.ext', 'putLines');
  1311. $lines = [
  1312. 'once upon a time',
  1313. 'a man and a spoon',
  1314. 'had a great time drinking coffee'
  1315. ];
  1316. $path
  1317. ->expects(self::once())
  1318. ->method('putContent')
  1319. ->with(implode(PHP_EOL, $lines))
  1320. ->willReturn(123);
  1321. $this->assertEquals(
  1322. 123,
  1323. $path->putLines($lines)
  1324. );
  1325. }
  1326. /**
  1327. * @throws IOException
  1328. * @throws FileNotFoundException
  1329. */
  1330. public function testGetPermissions(): void
  1331. {
  1332. $path = $this->getMock('/foo/file.ext', 'getPermissions');
  1333. $path->method('isFile')->willReturn(True);
  1334. $this->builtin
  1335. ->method('fileperms')
  1336. ->with('/foo/file.ext')
  1337. ->willReturn(16895);
  1338. $this->assertEquals(
  1339. 777,
  1340. $path->getPermissions()
  1341. );
  1342. }
  1343. /**
  1344. * @throws IOException
  1345. * @throws FileNotFoundException
  1346. */
  1347. public function testGetPermissionsFileDoesNotExist(): void
  1348. {
  1349. $path = $this->getMock('/foo/file.ext', 'getPermissions');
  1350. $path->method('isFile')->willReturn(False);
  1351. $this->builtin
  1352. ->expects(self::never())
  1353. ->method('fileperms');
  1354. $this->expectException(FileNotFoundException::class);
  1355. $path->getPermissions();
  1356. }
  1357. /**
  1358. * @throws IOException
  1359. * @throws FileNotFoundException
  1360. */
  1361. public function testGetPermissionsWithError(): void
  1362. {
  1363. $path = $this->getMock('/foo/file.ext', 'getPermissions');
  1364. $path->method('isFile')->willReturn(True);
  1365. $this->builtin
  1366. ->method('fileperms')
  1367. ->with('/foo/file.ext')
  1368. ->willReturn(False);
  1369. $this->expectException(IOException::class);
  1370. $path->getPermissions();
  1371. }
  1372. }