PathTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. <?php
  2. namespace Path\Tests\functionnal;
  3. use Path\Exception\FileExistsException;
  4. use Path\Exception\FileNotFoundException;
  5. use Path\Exception\IOException;
  6. use Path\Path;
  7. use PHPUnit\Framework\TestCase;
  8. // TODO: tested args should be both typed Path and string
  9. class PathTest
  10. {
  11. const TEMP_TEST_DIR = __DIR__ . "/temp";
  12. // TODO: consider using sys_get_temp_dir()
  13. protected Path $pathClass;
  14. public function setUp(): void
  15. {
  16. clearstatcache();
  17. mkdir(self::TEMP_TEST_DIR, 0777, true);
  18. chdir(self::TEMP_TEST_DIR);
  19. }
  20. private function rmDirs(string $dir): void {
  21. // Remove and replace by a proper tempdir method
  22. foreach(scandir($dir) as $file) {
  23. if ('.' === $file || '..' === $file) continue;
  24. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
  25. else unlink($dir . DIRECTORY_SEPARATOR . $file);
  26. }
  27. rmdir($dir);
  28. }
  29. public function tearDown(): void
  30. {
  31. $this->rmDirs(self::TEMP_TEST_DIR);
  32. chdir(__DIR__);
  33. }
  34. /**
  35. * Test 'Path' class 'copy_dir' method to copy a directory
  36. *
  37. * @return void
  38. * @throws FileExistsException
  39. * @throws FileNotFoundException
  40. */
  41. public function testCopyDir(): void
  42. {
  43. $src = self::TEMP_TEST_DIR . "/some_dir";
  44. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  45. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  46. mkdir($src);
  47. touch($srcContent);
  48. mkdir($dst);
  49. Path::copytree($src, $dst);
  50. $this->assertTrue(
  51. file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
  52. );
  53. }
  54. /**
  55. * Test 'Path' class 'copy_dir' method when the destination directory does not exist
  56. *
  57. * @throws FileNotFoundException|FileExistsException
  58. */
  59. public function testCopyDirWhenDestinationDirectoryNotExists(): void
  60. {
  61. $src = self::TEMP_TEST_DIR . "/some_dir";
  62. $dst = self::TEMP_TEST_DIR . "/non_existing_dir";
  63. mkdir($src);
  64. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  65. $this->expectException(FileNotFoundException::class);
  66. $this->expectExceptionMessage("Directory does not exist : " . $dst);
  67. Path::copytree($src, $dst);
  68. }
  69. /**
  70. * Test 'Path' class 'copy_dir' method when the destination directory already exists.
  71. *
  72. * @throws FileNotFoundException|FileExistsException if the destination directory already exists.
  73. */
  74. public function testCopyDirWhenDirectoryAlreadyExistsAtDestination(): void
  75. {
  76. $src = self::TEMP_TEST_DIR . "/some_dir";
  77. $dst = self::TEMP_TEST_DIR . "/other_dir";
  78. mkdir($src);
  79. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  80. mkdir($dst);
  81. mkdir($dst . DIRECTORY_SEPARATOR . "some_dir");
  82. $this->expectException(FileExistsException::class);
  83. $this->expectExceptionMessage("Directory already exists : " . $dst);
  84. Path::copytree($src, $dst);
  85. }
  86. /**
  87. * Test `eq` method with equal paths.
  88. *
  89. * Check that the method returns the correct result when the paths are equal.
  90. */
  91. public function testEqWithEqualPaths(): void
  92. {
  93. $path = new Path('/foo/bar');
  94. $this->assertTrue($path->eq('/foo/bar'));
  95. }
  96. /**
  97. * Test `eq` method with different paths.
  98. *
  99. * Check that the method returns the correct result when the paths are different.
  100. */
  101. public function testEqWithDifferentPaths(): void
  102. {
  103. $path = new Path('/foo/bar');
  104. $this->assertFalse($path->eq('/foo/zzz'));
  105. }
  106. /**
  107. * Test `eq` method with empty path.
  108. *
  109. * Check that the method returns the correct result when the path is empty.
  110. */
  111. public function testEqWithEmptyPath(): void
  112. {
  113. $path = new Path('/foo/bar');
  114. $this->assertFalse($path->eq(''));
  115. }
  116. /**
  117. * Test the append method of the Path class.
  118. *
  119. * @return void
  120. */
  121. public function testAppend(): void
  122. {
  123. $path = new Path('/foo');
  124. $this->assertEquals(
  125. "/foo/bar",
  126. $path->append('bar')
  127. );
  128. // One part
  129. $this->assertTrue(
  130. (new Path('/home'))->append('user')->eq('/home/user')
  131. );
  132. // Multiple parts
  133. $this->assertTrue(
  134. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  135. );
  136. // Absolute path passed in $parts
  137. $this->assertTrue(
  138. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  139. );
  140. }
  141. /**
  142. * Test the abspath method of the Path class.
  143. *
  144. * @return void
  145. */
  146. public function testAbsPath(): void
  147. {
  148. touch(self::TEMP_TEST_DIR . "/foo");
  149. chdir(self::TEMP_TEST_DIR);
  150. $this->assertEquals(
  151. self::TEMP_TEST_DIR . "/foo",
  152. (new Path('foo'))->abspath()
  153. );
  154. }
  155. /**
  156. * Test the abspath method of the Path class with a relative path.
  157. *
  158. * @return void
  159. */
  160. public function testAbsPathWithRelative(): void
  161. {
  162. mkdir(self::TEMP_TEST_DIR . "/foo");
  163. touch(self::TEMP_TEST_DIR . "/bar");
  164. chdir(self::TEMP_TEST_DIR . "/foo");
  165. $this->assertEquals(
  166. self::TEMP_TEST_DIR . "/bar",
  167. (new Path('../bar'))->abspath()
  168. );
  169. }
  170. /**
  171. * Test 'Path' class 'access' method to check existence of the file
  172. */
  173. public function testAccessCheckExistenceOfFile(): void
  174. {
  175. $filePath = self::TEMP_TEST_DIR . "/foo";
  176. touch($filePath);
  177. chmod($filePath, 777);
  178. $result = (new Path('foo'))->access(Path::F_OK);
  179. $this->assertTrue($result);
  180. }
  181. /**
  182. * Test 'Path' class 'access' method to check existence of the non-existent file
  183. */
  184. public function testAccessCheckExistenceOfNonExistingFile(): void
  185. {
  186. $result = (new Path('foo'))->access(Path::F_OK);
  187. $this->assertFalse($result);
  188. }
  189. /**
  190. * Test 'Path' class 'access' method to check read permission of the file
  191. */
  192. public function testAccessCheckReadPermissionOfFile(): void
  193. {
  194. $filePath = self::TEMP_TEST_DIR . "/foo";
  195. touch($filePath);
  196. chmod($filePath, 777);
  197. $result = (new Path('foo'))->access(Path::R_OK);
  198. $this->assertTrue($result);
  199. }
  200. // /**
  201. // * Test 'Path' class 'access' method to check read permission of the file (no permission)
  202. // */
  203. // public function testAccessCheckReadPermissionOfFileNoRight(): void
  204. // {
  205. // $filePath = self::TEMP_TEST_DIR . "/foo";
  206. // touch($filePath);
  207. // chmod($filePath, 000);
  208. //
  209. // $result = (new Path('foo'))->access(Path::R_OK);
  210. // $this->assertFalse($result);
  211. // }
  212. /**
  213. * Test 'Path' class 'access' method to check write permission of the file
  214. */
  215. public function testAccessCheckWritePermissionOfFile(): void
  216. {
  217. $filePath = self::TEMP_TEST_DIR . "/foo";
  218. touch($filePath);
  219. chmod($filePath, 777);
  220. $result = (new Path('foo'))->access(Path::W_OK);
  221. $this->assertTrue($result);
  222. }
  223. // /**
  224. // * Test 'Path' class 'access' method to check write permission of the file (no permission)
  225. // */
  226. // public function testAccessCheckWritePermissionOfFileNoRight(): void
  227. // {
  228. // $filePath = self::TEMP_TEST_DIR . "/foo";
  229. // touch($filePath);
  230. // chmod($filePath, 000);
  231. //
  232. // $result = (new Path('foo'))->access(Path::W_OK);
  233. // $this->assertFalse($result);
  234. // }
  235. /**
  236. * Test 'Path' class 'access' method to check execute permission of the file
  237. */
  238. public function testAccessCheckExecutePermissionOfFile(): void
  239. {
  240. $filePath = self::TEMP_TEST_DIR . "/foo";
  241. touch($filePath);
  242. chmod($filePath, 777);
  243. $result = (new Path('foo'))->access(Path::X_OK);
  244. $this->assertTrue($result);
  245. }
  246. /**
  247. * Test 'Path' class 'access' method to check existence of the file
  248. */
  249. public function testAccessCheckExecutePermissionOfFileNoRight(): void
  250. {
  251. $filePath = self::TEMP_TEST_DIR . "/foo";
  252. touch($filePath);
  253. chmod($filePath, 000);
  254. $result = (new Path('foo'))->access(Path::X_OK);
  255. $this->assertFalse($result);
  256. }
  257. /**
  258. * Test 'Path' class 'access' method with an invalid mode parameter
  259. */
  260. public function testAccessInvalidModeParameter(): void
  261. {
  262. $this->expectException(\RuntimeException::class);
  263. (new Path('foo'))->access(123);
  264. }
  265. /**
  266. * Test 'Path' class 'atime' method to get the access time of a file
  267. *
  268. * @return void
  269. */
  270. public function testATime()
  271. {
  272. touch(self::TEMP_TEST_DIR . "/foo");
  273. $atime = (new Path('foo'))->atime();
  274. $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
  275. $this->assertMatchesRegularExpression(
  276. "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
  277. $atime
  278. );
  279. }
  280. /**
  281. * Test 'Path' class 'isFile' method to check if the file exists
  282. *
  283. * @return void
  284. */
  285. public function testIsFileOnActualFile(): void
  286. {
  287. touch(self::TEMP_TEST_DIR . "/foo");
  288. $this->assertTrue((new Path('foo'))->isFile());
  289. }
  290. /**
  291. * Test 'Path' class 'isFile' method to check if a non-existent file exists
  292. *
  293. * @return void
  294. */
  295. public function testIsFileOnNonExistentFile(): void
  296. {
  297. $this->assertFalse((new Path('foo'))->isFile());
  298. }
  299. /**
  300. * Test 'Path' class 'isFile' method to check if a file exists on the given directory
  301. *
  302. * @return void
  303. */
  304. public function testIsFileOnExistentDir(): void
  305. {
  306. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  307. $this->assertFalse((new Path('some_dir'))->isFile());
  308. }
  309. /**
  310. * Test 'Path' class 'isDir' method to check if the path is a directory
  311. *
  312. * @return void
  313. */
  314. public function testIsDirOnActualDir(): void
  315. {
  316. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  317. $this->assertTrue((new Path('some_dir'))->isDir());
  318. }
  319. /**
  320. * Test 'Path' class 'isDir' method to check if a file's path is a directory
  321. *
  322. * @return void
  323. */
  324. public function testIsDirOnExistentFile(): void
  325. {
  326. touch(self::TEMP_TEST_DIR . "/foo");
  327. $this->assertFalse((new Path('some_dir'))->isDir());
  328. }
  329. /**
  330. * Test 'Path' class 'isDir' method on a non-existent directory
  331. *
  332. * @return void
  333. */
  334. public function testIsDirOnNonExistentDir(): void
  335. {
  336. $this->assertFalse((new Path('some_dir'))->isDir());
  337. }
  338. /**
  339. * Test 'Path' class 'ext' method to get the extension of the file
  340. *
  341. * @return void
  342. */
  343. public function testFileExtension(): void
  344. {
  345. $this->assertEquals(
  346. 'txt',
  347. (new Path("foo.txt"))->ext()
  348. );
  349. }
  350. /**
  351. * Test 'Path' class 'ext' method to get the extension of the file
  352. *
  353. * @return void
  354. */
  355. public function testEmptyExtension(): void
  356. {
  357. $this->assertEquals(
  358. '',
  359. (new Path("foo"))->ext()
  360. );
  361. }
  362. /**
  363. * Test 'Path' class 'basename' method to get the base name of a file
  364. *
  365. * @return void
  366. */
  367. public function testBaseName()
  368. {
  369. touch(self::TEMP_TEST_DIR . "/foo.txt");
  370. $this->assertEquals(
  371. 'foo.txt',
  372. (new Path("foo.txt"))->basename()
  373. );
  374. }
  375. /**
  376. * Test 'Path' class 'name' method to get the name of the file without extension
  377. *
  378. * @return void
  379. */
  380. public function testName()
  381. {
  382. touch(self::TEMP_TEST_DIR . "/foo");
  383. $this->assertEquals(
  384. 'foo',
  385. (new Path("foo"))->name()
  386. );
  387. }
  388. /**
  389. * Test 'Path' class 'name' method to get the name of the file with extension
  390. *
  391. * @return void
  392. */
  393. public function testNameWithExt()
  394. {
  395. touch(self::TEMP_TEST_DIR . "/foo.txt");
  396. $this->assertEquals(
  397. 'foo',
  398. (new Path("foo.txt"))->name()
  399. );
  400. }
  401. /**
  402. * Test 'Path' class 'mkdir' method to create a directory
  403. *
  404. * @return void
  405. * @throws FileExistsException
  406. */
  407. public function testMkDir(): void
  408. {
  409. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  410. $path->mkdir();
  411. $this->assertTrue($path->isDir());
  412. }
  413. /**
  414. * Test 'Path' class 'mkdir' method when directory already exists
  415. *
  416. * @throws FileExistsException If directory already exists
  417. */
  418. public function testMkDirExistingDir(): void {
  419. mkdir(self::TEMP_TEST_DIR . "/foo");
  420. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  421. $this->expectException(FileExistsException::class);
  422. $this->expectExceptionMessage("Directory already exists : " . self::TEMP_TEST_DIR . "/foo");
  423. $path->mkdir();
  424. }
  425. /**
  426. * Test 'Path' class 'mkdir' method to create a directory with existing directory and recursive option
  427. *
  428. * @return void
  429. * @throws FileExistsException
  430. */
  431. public function testMkDirExistingDirAndRecursive(): void {
  432. mkdir(self::TEMP_TEST_DIR . "/foo");
  433. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  434. $path->mkdir(0777, true);
  435. $this->assertTrue($path->isDir());
  436. }
  437. /**
  438. * Test 'Path' class 'mkdir' method to create a directory when a file with the same name already exists
  439. *
  440. * @return void
  441. * @throws FileExistsException When a file with the same name already exists
  442. */
  443. public function testMkDirExistingFile(): void {
  444. touch(self::TEMP_TEST_DIR . "/foo");
  445. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  446. $this->expectException(FileExistsException::class);
  447. $this->expectExceptionMessage("A file with this name already exists : " . self::TEMP_TEST_DIR . "/foo");
  448. $path->mkdir();
  449. }
  450. /**
  451. * Test 'Path' class 'mkdir' method to create a directory recursively
  452. *
  453. * @return void
  454. * @throws FileExistsException
  455. */
  456. public function testMkDirRecursive(): void {
  457. $path = new Path(self::TEMP_TEST_DIR . "/foo/bar");
  458. $path->mkdir(0777, true);
  459. $this->assertTrue($path->isDir());
  460. }
  461. /**
  462. * Test 'Path' class 'delete' method to delete a file.
  463. *
  464. * @return void
  465. * @throws FileNotFoundException
  466. */
  467. public function testDeleteFileSuccess(): void
  468. {
  469. touch(self::TEMP_TEST_DIR . "/foo");
  470. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  471. $this->assertTrue($path->isFile());
  472. $path->delete();
  473. $this->assertFalse($path->isFile());
  474. }
  475. /**
  476. * Test 'Path' class 'delete' method to delete a directory successfully
  477. *
  478. * @return void
  479. * @throws FileNotFoundException
  480. */
  481. public function testDeleteDirSuccess(): void
  482. {
  483. mkdir(self::TEMP_TEST_DIR . "/foo");
  484. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  485. $this->assertTrue($path->isDir());
  486. $path->delete();
  487. $this->assertFalse($path->isDir());
  488. }
  489. /**
  490. * Test 'Path' class 'delete' method to delete a non-existing file or dir
  491. *
  492. * @throws FileNotFoundException When the file does not exist
  493. */
  494. public function testDeleteNonExistingFile(): void
  495. {
  496. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  497. $this->assertFalse($path->isDir());
  498. $this->expectException(FileNotFoundException::class);
  499. $this->expectExceptionMessage("File does not exist : " . self::TEMP_TEST_DIR . "/foo");
  500. $path->delete();
  501. }
  502. /**
  503. * Test 'Path' class 'copy_dir' method to copy a file
  504. *
  505. * @return void
  506. * @throws FileExistsException
  507. * @throws FileNotFoundException
  508. */
  509. public function testCopyWithFile(): void
  510. {
  511. $src = self::TEMP_TEST_DIR . "/some_dir";
  512. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  513. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  514. mkdir($src);
  515. mkdir($dst);
  516. touch($srcContent);
  517. $path = new Path($srcContent);
  518. $path->copy($dst);
  519. $this->assertTrue(
  520. file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
  521. );
  522. }
  523. /**
  524. * Test 'Path' class 'copy_dir' method to copy a directory
  525. *
  526. * @return void
  527. * @throws FileExistsException
  528. * @throws FileNotFoundException
  529. */
  530. public function testCopyWithDir(): void
  531. {
  532. $src = self::TEMP_TEST_DIR . "/some_dir";
  533. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  534. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  535. mkdir($src);
  536. mkdir($dst);
  537. touch($srcContent);
  538. $path = new Path($src);
  539. $path->copy($dst);
  540. $this->assertTrue(
  541. file_exists($srcContent)
  542. );
  543. }
  544. /**
  545. * Test 'Path' class 'copy' method when the source file does not exist.
  546. *
  547. * @return void
  548. * @throws FileExistsException
  549. * @throws FileNotFoundException
  550. */
  551. public function testCopyFileNotExists(): void
  552. {
  553. $src = self::TEMP_TEST_DIR . "/some_dir";
  554. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  555. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  556. mkdir($src);
  557. mkdir($dst);
  558. touch($srcContent);
  559. $path = new Path($src);
  560. $path->copy($dst);
  561. $this->assertTrue(
  562. file_exists($srcContent)
  563. );
  564. }
  565. /**
  566. * Test 'Path' class 'copy' method when the source file does not exist.
  567. *
  568. * @return void
  569. * @throws FileExistsException
  570. * @throws FileNotFoundException
  571. */
  572. public function testCopyDirDestAlreadyExists(): void
  573. {
  574. $src = self::TEMP_TEST_DIR . "/some_dir";
  575. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  576. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  577. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  578. mkdir($src);
  579. touch($srcContent);
  580. mkdir($dst);
  581. mkdir($dstContent);
  582. $this->expectException(FileExistsException::class);
  583. $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
  584. $path = new Path($srcContent);
  585. $path->copy($dst);
  586. }
  587. /**
  588. * Test 'Path' class 'copy' method when the source file does not exist.
  589. *
  590. * @return void
  591. * @throws FileExistsException
  592. * @throws FileNotFoundException
  593. */
  594. public function testCopyFileDestAlreadyExists(): void
  595. {
  596. $src = self::TEMP_TEST_DIR . "/some_dir";
  597. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  598. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  599. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  600. mkdir($src);
  601. touch($srcContent);
  602. mkdir($dst);
  603. touch($dstContent);
  604. $this->expectException(FileExistsException::class);
  605. $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
  606. $path = new Path($srcContent);
  607. $path->copy($dst);
  608. }
  609. /**
  610. * Test 'Path' class 'move' method to move a file from source directory to destination directory
  611. * @throws FileExistsException
  612. */
  613. public function testMoveWithFile(): void
  614. {
  615. $src = self::TEMP_TEST_DIR . "/some_dir";
  616. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  617. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  618. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  619. mkdir($src);
  620. touch($srcContent);
  621. mkdir($dst);
  622. $path = new Path($srcContent);
  623. $path->move($dst);
  624. $this->assertFalse(
  625. file_exists($srcContent)
  626. );
  627. $this->assertTrue(
  628. file_exists($dstContent)
  629. );
  630. }
  631. /**
  632. * Test 'Path' class 'move' method to move a directory to a different location
  633. *
  634. * @return void
  635. * @throws FileExistsException
  636. */
  637. public function testMoveWithDir(): void
  638. {
  639. $src = self::TEMP_TEST_DIR . "/some_dir";
  640. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  641. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  642. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  643. mkdir($src);
  644. touch($srcContent);
  645. $path = new Path($src);
  646. $path->move($dst);
  647. $this->assertFalse(
  648. file_exists($srcContent)
  649. );
  650. $this->assertTrue(
  651. file_exists($dstContent)
  652. );
  653. }
  654. /**
  655. * Test 'Path' class 'touch' method to create a file that does not exist
  656. *
  657. * @return void
  658. */
  659. public function testTouchFileDoesNotExist(): void
  660. {
  661. $src = self::TEMP_TEST_DIR . "/foo.txt";
  662. $path = new Path($src);
  663. $this->assertFalse(is_file($src));
  664. $path->touch();
  665. $this->assertTrue(is_file($src));
  666. }
  667. public function testTouchFileExistsNothingChange(): void {
  668. $src = self::TEMP_TEST_DIR . "/foo.txt";
  669. touch($src);
  670. $this->assertTrue(is_file($src));
  671. $path = new Path($src);
  672. $path->touch();
  673. $this->assertTrue(is_file($src));
  674. }
  675. public function testTouchFileExistsUpdateMtimeWithInt(): void {
  676. $src = self::TEMP_TEST_DIR . "/foo.txt";
  677. touch($src);
  678. $path = new Path($src);
  679. $timestamp = 1000;
  680. $path->touch($timestamp);
  681. $this->assertEquals(
  682. $timestamp,
  683. filemtime($src)
  684. );
  685. }
  686. public function testTouchFileExistsUpdateMtimeWithDatetime(): void {
  687. $src = self::TEMP_TEST_DIR . "/foo.txt";
  688. touch($src);
  689. $path = new Path($src);
  690. $dateTime = new \DateTime("2000-01-01");
  691. $path->touch($dateTime);
  692. $this->assertEquals(
  693. $dateTime->getTimestamp(),
  694. filemtime($src)
  695. );
  696. }
  697. public function testTouchFileExistsUpdateAtimeWithInt(): void {
  698. $src = self::TEMP_TEST_DIR . "/foo.txt";
  699. touch($src);
  700. $path = new Path($src);
  701. $timestamp = 1000;
  702. $path->touch($timestamp, $timestamp);
  703. $this->assertEquals(
  704. $timestamp,
  705. fileatime($src)
  706. );
  707. }
  708. public function testTouchFileExistsUpdateAtimeWithDatetime(): void {
  709. $src = self::TEMP_TEST_DIR . "/foo.txt";
  710. touch($src);
  711. $path = new Path($src);
  712. $dateTime = new \DateTime("2000-01-01");
  713. $path->touch($dateTime, $dateTime);
  714. $this->assertEquals(
  715. $dateTime->getTimestamp(),
  716. fileatime($src)
  717. );
  718. }
  719. public function testLastModified(): void {
  720. $src = self::TEMP_TEST_DIR . "/foo.txt";
  721. $dateTime = new \DateTime("2000-01-01");
  722. touch($src, $dateTime->getTimestamp());
  723. $path = new Path($src);
  724. $this->assertEquals(
  725. $dateTime->getTimestamp(),
  726. $path->lastModified()
  727. );
  728. }
  729. /**
  730. * Test 'Path' class 'size' method to get the size of the file
  731. *
  732. * @return void
  733. * @throws FileNotFoundException
  734. */
  735. public function testSize(): void {
  736. $src = self::TEMP_TEST_DIR . "/foo.txt";
  737. file_put_contents($src, "nova");
  738. $path = new Path($src);
  739. $this->assertEquals(
  740. 4,
  741. $path->size()
  742. );
  743. }
  744. /**
  745. * Test 'Path' class 'size' method to get the size of the file
  746. *
  747. * @return void
  748. * @throws FileNotFoundException
  749. */
  750. public function testSizeNotExistingFile(): void {
  751. $src = self::TEMP_TEST_DIR . "/foo.txt";
  752. $path = new Path($src);
  753. $this->expectException(FileNotFoundException::class);
  754. $this->expectExceptionMessage("File does not exist : " . $src);
  755. $path->size();
  756. }
  757. public function testParent(): void
  758. {
  759. $this->assertEquals(
  760. '/foo/bar',
  761. (new Path('/foo/bar/baz'))->parent()
  762. );
  763. $this->assertEquals(
  764. '/foo/bar',
  765. (new Path('/foo/bar/baz.txt'))->parent()
  766. );
  767. $this->assertEquals(
  768. '/',
  769. (new Path('/foo'))->parent()
  770. );
  771. }
  772. /**
  773. * Test 'Path' class 'getContent' method to retrieve the content of a file
  774. *
  775. * @return void
  776. * @throws FileNotFoundException
  777. */
  778. public function testGetContent(): void {
  779. $src = self::TEMP_TEST_DIR . "/foo.txt";
  780. file_put_contents($src, "nova");
  781. $path = new Path($src);
  782. $this->assertEquals(
  783. "nova",
  784. $path->getContent()
  785. );
  786. }
  787. /**
  788. * Test 'Path' class 'getContent' method to get the content of a non-existing file
  789. *
  790. * @throws FileNotFoundException If the file does not exist
  791. */
  792. public function testGetContentNotExistingFile(): void {
  793. $src = self::TEMP_TEST_DIR . "/foo.txt";
  794. $path = new Path($src);
  795. $this->expectException(FileNotFoundException::class);
  796. $this->expectExceptionMessage("File does not exist : " . $src);
  797. $path->getContent();
  798. }
  799. public function testPutContent(): void {
  800. $src = self::TEMP_TEST_DIR . "/foo.txt";
  801. $path = new Path($src);
  802. $path->putContent("ocarina");
  803. $this->assertEquals(
  804. "ocarina",
  805. file_get_contents($src)
  806. );
  807. }
  808. public function testAppendContent(): void {
  809. $src = self::TEMP_TEST_DIR . "/foo.txt";
  810. touch($src);
  811. file_put_contents($src, "oca");
  812. $path = new Path($src);
  813. $path->appendContent("rina");
  814. $this->assertEquals(
  815. "ocarina",
  816. file_get_contents($src)
  817. );
  818. }
  819. /**
  820. * Test 'Path' class 'getPermissions' method to retrieve the file permissions
  821. *
  822. * @return void
  823. * @throws FileNotFoundException
  824. */
  825. public function testGetPermissions(): void
  826. {
  827. $src = self::TEMP_TEST_DIR . "/foo.txt";
  828. touch($src);
  829. chmod($src, 0777);
  830. $path = new Path($src);
  831. $this->assertEquals(
  832. 777,
  833. $path->getPermissions()
  834. );
  835. }
  836. /**
  837. * Test 'Path' class 'getPermissions' method to retrieve file permissions
  838. * @throws FileNotFoundException
  839. */
  840. public function testGetPermissionsAlt(): void
  841. {
  842. $src = self::TEMP_TEST_DIR . "/foo.txt";
  843. touch($src);
  844. chmod($src, 0755);
  845. $path = new Path($src);
  846. $this->assertEquals(
  847. 755,
  848. $path->getPermissions()
  849. );
  850. }
  851. /**
  852. * Test 'Path' class 'getPermissions' method to retrieve file permissions
  853. * @throws FileNotFoundException
  854. */
  855. public function testGetPermissionsFileNotExists(): void
  856. {
  857. $src = self::TEMP_TEST_DIR . "/foo.txt";
  858. $path = new Path($src);
  859. $this->expectException(FileNotFoundException::class);
  860. $this->expectExceptionMessage("File or dir does not exist : " . $src);
  861. $path->getPermissions();
  862. }
  863. /**
  864. * Test 'Path' class 'setPermissions' method to change the permissions of a file
  865. * @throws FileNotFoundException
  866. */
  867. public function testSetPermissions(): void
  868. {
  869. $src = self::TEMP_TEST_DIR . "/foo.txt";
  870. touch($src);
  871. chmod($src, 0777);
  872. $path = new Path($src);
  873. $result = $path->setPermissions(0666);
  874. $this->assertTrue($result);
  875. $this->assertEquals(
  876. '0666',
  877. substr(sprintf('%o', fileperms($src)), -4)
  878. );
  879. }
  880. /**
  881. * Test 'Path' class 'setPermissions' method when file does not exist
  882. *
  883. * @throws FileNotFoundException If the file does not exist
  884. */
  885. public function testSetPermissionsFileNotExists(): void
  886. {
  887. $src = self::TEMP_TEST_DIR . "/foo.txt";
  888. $path = new Path($src);
  889. $this->expectException(FileNotFoundException::class);
  890. $this->expectExceptionMessage("File or dir does not exist : " . $src);
  891. $path->setPermissions(777);
  892. }
  893. /**
  894. * Test 'Path' class 'exists' method to check existence of the file
  895. *
  896. * @return void
  897. */
  898. public function testExistsExistingFile(): void
  899. {
  900. $src = self::TEMP_TEST_DIR . "/foo.txt";
  901. touch($src);
  902. $path = new Path($src);
  903. $this->assertTrue(
  904. $path->exists()
  905. );
  906. }
  907. /**
  908. * Test 'Path' class 'exists' method to check existence of the directory
  909. *
  910. * @return void
  911. */
  912. public function testExistsExistingDir(): void
  913. {
  914. $src = self::TEMP_TEST_DIR . "/foo";
  915. mkdir($src);
  916. $path = new Path($src);
  917. $this->assertTrue(
  918. $path->exists()
  919. );
  920. }
  921. /**
  922. * Test 'Path' class 'exists' method to check existence of a non-existing file
  923. *
  924. * @return void
  925. */
  926. public function testExistsNonExistingFile(): void
  927. {
  928. $src = self::TEMP_TEST_DIR . "/foo.txt";
  929. $path = new Path($src);
  930. $this->assertFalse(
  931. $path->exists()
  932. );
  933. }
  934. /**
  935. * Test 'Path' class 'glob' method to match and retrieve file names in a directory
  936. *
  937. * @return void
  938. */
  939. public function testGlob(): void
  940. {
  941. $src = self::TEMP_TEST_DIR;
  942. touch($src . "/foo.txt");
  943. touch($src . "/bar.txt");
  944. touch($src . "/pic.png");
  945. $path = new Path($src);
  946. $results = [];
  947. foreach ($path->glob('*.txt') as $filename) {
  948. $results[] = (string)$filename;
  949. }
  950. $this->assertEquals(
  951. ['bar.txt', 'foo.txt'],
  952. $results
  953. );
  954. }
  955. /**
  956. * Test 'Path' class 'rmdir' method to remove a directory and its contents
  957. *
  958. * @return void
  959. * @throws FileNotFoundException
  960. */
  961. public function testRmDir(): void
  962. {
  963. $src = self::TEMP_TEST_DIR . "/foo";
  964. mkdir($src);
  965. $path = new Path($src);
  966. $path->rmdir();
  967. $this->assertFalse(
  968. is_dir($src)
  969. );
  970. }
  971. public function testRmDirIsFile(): void {
  972. $src = self::TEMP_TEST_DIR . "/foo";
  973. touch($src);
  974. $path = new Path($src);
  975. $this->expectException(FileNotFoundException::class);
  976. $this->expectExceptionMessage($src . " is not a directory");
  977. $path->rmdir();
  978. }
  979. public function testRmDirNonExistingDir(): void {
  980. $src = self::TEMP_TEST_DIR . "/foo";
  981. $path = new Path($src);
  982. $this->expectException(FileNotFoundException::class);
  983. $this->expectExceptionMessage($src . " is not a directory");
  984. $path->rmdir();
  985. }
  986. /**
  987. * Test 'Path' class 'rmdir' method to remove a directory and its contents recursively
  988. *
  989. * @return void
  990. * @throws FileNotFoundException
  991. */
  992. public function testRmDirRecursive(): void {
  993. $src = self::TEMP_TEST_DIR . "/foo/bar";
  994. mkdir($src, 0777, true);
  995. touch($src . "/file.txt");
  996. $path = new Path($src);
  997. $path->rmdir(true);
  998. $this->assertFalse(
  999. is_dir($src)
  1000. );
  1001. }
  1002. /**
  1003. * @throws IOException
  1004. * @throws FileNotFoundException
  1005. */
  1006. public function testOpen(): void {
  1007. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1008. touch($src);
  1009. $path = new Path($src);
  1010. $handle = $path->open();
  1011. $this->assertIsResource($handle);
  1012. }
  1013. /**
  1014. * @throws IOException
  1015. * @throws FileNotFoundException
  1016. */
  1017. public function testOpenNotExistingFile(): void {
  1018. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1019. $path = new Path($src);
  1020. $this->expectException(FileNotFoundException::class);
  1021. $this->expectExceptionMessage(self::TEMP_TEST_DIR . "/foo.txt is not a file");
  1022. $path->open();
  1023. }
  1024. /**
  1025. * @throws IOException
  1026. * @throws FileNotFoundException
  1027. */
  1028. public function testOpenIsDir(): void {
  1029. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1030. $path = new Path($src);
  1031. $this->expectException(FileNotFoundException::class);
  1032. $this->expectExceptionMessage(self::TEMP_TEST_DIR . "/foo.txt is not a file");
  1033. $path->open();
  1034. }
  1035. public function testIsAbs() {
  1036. $path1 = new Path('/absolute/path');
  1037. $this->assertTrue($path1->isAbs());
  1038. $path2 = new Path('relative/path');
  1039. $this->assertFalse($path2->isAbs());
  1040. }
  1041. /**
  1042. * @throws FileNotFoundException
  1043. */
  1044. public function testChmod(): void
  1045. {
  1046. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1047. $path = $this
  1048. ->getMockBuilder(Path::class)
  1049. ->onlyMethods(['setPermissions'])
  1050. ->setConstructorArgs([$src])
  1051. ->getMock();
  1052. $path->expects(self::once())->method('setPermissions')->with(0770);
  1053. $path->chmod(0770);
  1054. }
  1055. /**
  1056. * @throws FileNotFoundException
  1057. */
  1058. public function testChown(): void
  1059. {
  1060. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1061. $path = $this
  1062. ->getMockBuilder(Path::class)
  1063. ->onlyMethods(['setOwner'])
  1064. ->setConstructorArgs([$src])
  1065. ->getMock();
  1066. $path->expects(self::once())->method('setOwner')->with(2, 2);
  1067. $path->chown(2, 2);
  1068. }
  1069. public function testIsLink(): void
  1070. {
  1071. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1072. touch($src);
  1073. $path1 = new Path($src);
  1074. $target = self::TEMP_TEST_DIR . "/link.txt";
  1075. symlink($src, $target);
  1076. $path2 = new Path($target);
  1077. $this->assertFalse($path1->isLink());
  1078. $this->assertTrue($path2->isLink());
  1079. }
  1080. /**
  1081. * @throws FileNotFoundException
  1082. */
  1083. public function testIterDir(): void {
  1084. $dir = self::TEMP_TEST_DIR . "/foo";
  1085. mkdir($dir);
  1086. $file1 = $dir . "/file1.ext";
  1087. touch($file1);
  1088. $file2 = $dir . "/file2.ext";
  1089. touch($file2);
  1090. $subDir = $dir . "/sub_dir";
  1091. mkdir($subDir);
  1092. $path = new Path($dir);
  1093. // TODO: test the generator behavior without array conversion
  1094. $this->assertEquals(
  1095. ['file1.ext', 'file2.ext', 'sub_dir'],
  1096. iterator_to_array($path->iterDir())
  1097. );
  1098. }
  1099. /**
  1100. * @throws FileNotFoundException
  1101. */
  1102. public function testIterDirNotExisting(): void {
  1103. $dir = self::TEMP_TEST_DIR . "/foo";
  1104. $path = new Path($dir);
  1105. $this->expectException(FileNotFoundException::class);
  1106. $this->expectExceptionMessage($dir . " is not a directory");
  1107. iterator_to_array($path->iterDir());
  1108. }
  1109. /**
  1110. * @throws FileNotFoundException
  1111. */
  1112. public function testIterDirIsFile(): void {
  1113. $src = self::TEMP_TEST_DIR . "/foo";
  1114. touch($src);
  1115. $path = new Path($src);
  1116. $this->expectException(FileNotFoundException::class);
  1117. $this->expectExceptionMessage($src . " is not a directory");
  1118. iterator_to_array($path->iterDir());
  1119. }
  1120. public function testLink(): void
  1121. {
  1122. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1123. touch($src);
  1124. $dst = self::TEMP_TEST_DIR . "/link.txt";
  1125. $path = new Path($src);
  1126. $path->link($dst);
  1127. $this->assertTrue(file_exists($dst));
  1128. }
  1129. public function testLinkWithPath(): void
  1130. {
  1131. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1132. touch($src);
  1133. $dst = self::TEMP_TEST_DIR . "/link.txt";
  1134. $path = new Path($src);
  1135. $path->link(new Path($dst));
  1136. $this->assertTrue(file_exists($dst));
  1137. }
  1138. public function testLstat(): void
  1139. {
  1140. $src = self::TEMP_TEST_DIR . "/foo.txt";
  1141. file_put_contents($src,'foo');
  1142. $path = new Path($src);
  1143. self::assertSame(
  1144. lstat($src),
  1145. $path->lstat()
  1146. );
  1147. }
  1148. public function testParts(): void
  1149. {
  1150. $path = new Path("foo/bar/my_file.txt");
  1151. self::assertEquals(
  1152. ['foo', 'bar', 'my_file.txt'],
  1153. $path->parts()
  1154. );
  1155. }
  1156. public function testPartsWithLeadingSeparator(): void
  1157. {
  1158. $path = new Path("/foo/bar/my_file.txt");
  1159. self::assertEquals(
  1160. ['/', 'foo', 'bar', 'my_file.txt'],
  1161. $path->parts()
  1162. );
  1163. }
  1164. /**
  1165. * @throws FileNotFoundException
  1166. */
  1167. public function testGetRelativePath()
  1168. {
  1169. $src = self::TEMP_TEST_DIR . "/foo";
  1170. touch($src);
  1171. $path = new Path($src);
  1172. $this->assertSame(
  1173. "foo",
  1174. $path->getRelativePath(self::TEMP_TEST_DIR)
  1175. );
  1176. }
  1177. }