PathTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. namespace Path\Tests;
  3. use Path\Exception\FileExistsException;
  4. use Path\Exception\FileNotFoundException;
  5. use Path\Path;
  6. use PHPUnit\Framework\TestCase;
  7. // TODO: tested args should be both typed Path and string
  8. class PathTest extends TestCase
  9. {
  10. const TEMP_TEST_DIR = __DIR__ . "/temp";
  11. protected Path $pathClass;
  12. public function setUp(): void
  13. {
  14. mkdir(self::TEMP_TEST_DIR);
  15. chdir(self::TEMP_TEST_DIR);
  16. }
  17. private function rmDirs(string $dir): void {
  18. // Remove and replace by a proper tempdir method
  19. foreach(scandir($dir) as $file) {
  20. if ('.' === $file || '..' === $file) continue;
  21. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
  22. else unlink($dir . DIRECTORY_SEPARATOR . $file);
  23. }
  24. rmdir($dir);
  25. }
  26. public function tearDown(): void
  27. {
  28. $this->rmDirs(self::TEMP_TEST_DIR);
  29. chdir(__DIR__);
  30. }
  31. public function testToString(): void
  32. {
  33. $path = new Path('/foo/bar');
  34. $this->assertEquals('/foo/bar', $path->__toString());
  35. }
  36. /**
  37. * Test 'join' method.
  38. */
  39. public function testJoin(): void
  40. {
  41. // One part
  42. $this->assertEquals(
  43. '/home/user',
  44. Path::join('/home', 'user')
  45. );
  46. // Multiple parts
  47. $this->assertEquals(
  48. '/home/user/documents',
  49. Path::join('/home', 'user', 'documents')
  50. );
  51. // Absolute path passed in $parts
  52. $this->assertEquals(
  53. '/user/documents',
  54. Path::join('home', '/user', 'documents')
  55. );
  56. }
  57. /**
  58. * Test 'Path' class 'copy_dir' method to copy a directory
  59. *
  60. * @return void
  61. * @throws FileExistsException
  62. * @throws FileNotFoundException
  63. */
  64. public function testCopyDir(): void
  65. {
  66. $src = self::TEMP_TEST_DIR . "/some_dir";
  67. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  68. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  69. mkdir($src);
  70. mkdir($dst);
  71. touch($srcContent);
  72. Path::copy_dir($src, $dst);
  73. $this->assertTrue(
  74. file_exists($srcContent)
  75. );
  76. }
  77. /**
  78. * Test 'Path' class 'copy_dir' method when the destination directory does not exist
  79. *
  80. * @throws FileNotFoundException|FileExistsException
  81. */
  82. public function testCopyDirWhenDestinationDirectoryNotExists(): void
  83. {
  84. $src = self::TEMP_TEST_DIR . "/some_dir";
  85. $dst = self::TEMP_TEST_DIR . "/non_existing_dir";
  86. mkdir($src);
  87. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  88. $this->expectException(FileNotFoundException::class);
  89. $this->expectExceptionMessage("Directory does not exist : " . $dst);
  90. Path::copy_dir($src, $dst);
  91. }
  92. /**
  93. * Test 'Path' class 'copy_dir' method when the destination directory already exists.
  94. *
  95. * @throws FileNotFoundException|FileExistsException if the destination directory already exists.
  96. */
  97. public function testCopyDirWhenDirectoryAlreadyExistsAtDestination(): void
  98. {
  99. $src = self::TEMP_TEST_DIR . "/some_dir";
  100. $dst = self::TEMP_TEST_DIR . "/other_dir";
  101. mkdir($src);
  102. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  103. mkdir($dst);
  104. mkdir($dst . DIRECTORY_SEPARATOR . "some_dir");
  105. $this->expectException(FileExistsException::class);
  106. $this->expectExceptionMessage("Directory already exists : " . $dst);
  107. Path::copy_dir($src, $dst);
  108. }
  109. /**
  110. * Test `eq` method with equal paths.
  111. *
  112. * Check that the method returns the correct result when the paths are equal.
  113. */
  114. public function testEqWithEqualPaths(): void
  115. {
  116. $path = new Path('/foo/bar');
  117. $this->assertTrue($path->eq('/foo/bar'));
  118. }
  119. /**
  120. * Test `eq` method with different paths.
  121. *
  122. * Check that the method returns the correct result when the paths are different.
  123. */
  124. public function testEqWithDifferentPaths(): void
  125. {
  126. $path = new Path('/foo/bar');
  127. $this->assertFalse($path->eq('/foo/zzz'));
  128. }
  129. /**
  130. * Test `eq` method with empty path.
  131. *
  132. * Check that the method returns the correct result when the path is empty.
  133. */
  134. public function testEqWithEmptyPath(): void
  135. {
  136. $path = new Path('/foo/bar');
  137. $this->assertFalse($path->eq(''));
  138. }
  139. /**
  140. * Test the append method of the Path class.
  141. *
  142. * @return void
  143. */
  144. public function testAppend(): void
  145. {
  146. $path = new Path('/foo');
  147. $this->assertEquals(
  148. "/foo/bar",
  149. $path->append('bar')
  150. );
  151. // One part
  152. $this->assertTrue(
  153. (new Path('/home'))->append('user')->eq('/home/user')
  154. );
  155. // Multiple parts
  156. $this->assertTrue(
  157. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  158. );
  159. // Absolute path passed in $parts
  160. $this->assertTrue(
  161. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  162. );
  163. }
  164. /**
  165. * Test the abspath method of the Path class.
  166. *
  167. * @return void
  168. */
  169. public function testAbsPath(): void
  170. {
  171. touch(self::TEMP_TEST_DIR . "/foo");
  172. chdir(self::TEMP_TEST_DIR);
  173. $this->assertEquals(
  174. self::TEMP_TEST_DIR . "/foo",
  175. (new Path('foo'))->abspath()
  176. );
  177. }
  178. /**
  179. * Test the abspath method of the Path class with a relative path.
  180. *
  181. * @return void
  182. */
  183. public function testAbsPathWithRelative(): void
  184. {
  185. mkdir(self::TEMP_TEST_DIR . "/foo");
  186. touch(self::TEMP_TEST_DIR . "/bar");
  187. chdir(self::TEMP_TEST_DIR . "/foo");
  188. $this->assertEquals(
  189. self::TEMP_TEST_DIR . "/bar",
  190. (new Path('../bar'))->abspath()
  191. );
  192. }
  193. /**
  194. * Test 'Path' class 'access' method to check existence of the file
  195. */
  196. public function testAccessCheckExistenceOfFile(): void
  197. {
  198. $filePath = self::TEMP_TEST_DIR . "/foo";
  199. touch($filePath);
  200. chmod($filePath, 777);
  201. $result = (new Path('foo'))->access(Path::F_OK);
  202. $this->assertTrue($result);
  203. }
  204. /**
  205. * Test 'Path' class 'access' method to check existence of the non-existent file
  206. */
  207. public function testAccessCheckExistenceOfNonExistingFile(): void
  208. {
  209. $result = (new Path('foo'))->access(Path::F_OK);
  210. $this->assertFalse($result);
  211. }
  212. /**
  213. * Test 'Path' class 'access' method to check read permission of the file
  214. */
  215. public function testAccessCheckReadPermissionOfFile(): void
  216. {
  217. $filePath = self::TEMP_TEST_DIR . "/foo";
  218. touch($filePath);
  219. chmod($filePath, 777);
  220. $result = (new Path('foo'))->access(Path::R_OK);
  221. $this->assertTrue($result);
  222. }
  223. // /**
  224. // * Test 'Path' class 'access' method to check read permission of the file (no permission)
  225. // */
  226. // public function testAccessCheckReadPermissionOfFileNoRight(): void
  227. // {
  228. // $filePath = self::TEMP_TEST_DIR . "/foo";
  229. // touch($filePath);
  230. // chmod($filePath, 000);
  231. //
  232. // $result = (new Path('foo'))->access(Path::R_OK);
  233. // $this->assertFalse($result);
  234. // }
  235. /**
  236. * Test 'Path' class 'access' method to check write permission of the file
  237. */
  238. public function testAccessCheckWritePermissionOfFile(): void
  239. {
  240. $filePath = self::TEMP_TEST_DIR . "/foo";
  241. touch($filePath);
  242. chmod($filePath, 777);
  243. $result = (new Path('foo'))->access(Path::W_OK);
  244. $this->assertTrue($result);
  245. }
  246. // /**
  247. // * Test 'Path' class 'access' method to check write permission of the file (no permission)
  248. // */
  249. // public function testAccessCheckWritePermissionOfFileNoRight(): void
  250. // {
  251. // $filePath = self::TEMP_TEST_DIR . "/foo";
  252. // touch($filePath);
  253. // chmod($filePath, 000);
  254. //
  255. // $result = (new Path('foo'))->access(Path::W_OK);
  256. // $this->assertFalse($result);
  257. // }
  258. /**
  259. * Test 'Path' class 'access' method to check execute permission of the file
  260. */
  261. public function testAccessCheckExecutePermissionOfFile(): void
  262. {
  263. $filePath = self::TEMP_TEST_DIR . "/foo";
  264. touch($filePath);
  265. chmod($filePath, 777);
  266. $result = (new Path('foo'))->access(Path::X_OK);
  267. $this->assertTrue($result);
  268. }
  269. /**
  270. * Test 'Path' class 'access' method to check existence of the file
  271. */
  272. public function testAccessCheckExecutePermissionOfFileNoRight(): void
  273. {
  274. $filePath = self::TEMP_TEST_DIR . "/foo";
  275. touch($filePath);
  276. chmod($filePath, 000);
  277. $result = (new Path('foo'))->access(Path::X_OK);
  278. $this->assertFalse($result);
  279. }
  280. /**
  281. * Test 'Path' class 'access' method with an invalid mode parameter
  282. */
  283. public function testAccessInvalidModeParameter(): void
  284. {
  285. $this->expectException(\RuntimeException::class);
  286. (new Path('foo'))->access(123);
  287. }
  288. /**
  289. * Test 'Path' class 'atime' method to get the access time of a file
  290. *
  291. * @return void
  292. */
  293. public function testATime()
  294. {
  295. touch(self::TEMP_TEST_DIR . "/foo");
  296. $atime = (new Path('foo'))->atime();
  297. $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
  298. $this->assertMatchesRegularExpression(
  299. "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
  300. $atime
  301. );
  302. }
  303. /**
  304. * Test 'Path' class 'isFile' method to check if the file exists
  305. *
  306. * @return void
  307. */
  308. public function testIsFileOnActualFile(): void
  309. {
  310. touch(self::TEMP_TEST_DIR . "/foo");
  311. $this->assertTrue((new Path('foo'))->isFile());
  312. }
  313. /**
  314. * Test 'Path' class 'isFile' method to check if a non-existent file exists
  315. *
  316. * @return void
  317. */
  318. public function testIsFileOnNonExistentFile(): void
  319. {
  320. $this->assertFalse((new Path('foo'))->isFile());
  321. }
  322. /**
  323. * Test 'Path' class 'isFile' method to check if a file exists on the given directory
  324. *
  325. * @return void
  326. */
  327. public function testIsFileOnExistentDir(): void
  328. {
  329. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  330. $this->assertFalse((new Path('some_dir'))->isFile());
  331. }
  332. /**
  333. * Test 'Path' class 'isDir' method to check if the path is a directory
  334. *
  335. * @return void
  336. */
  337. public function testIsDirOnActualDir(): void
  338. {
  339. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  340. $this->assertTrue((new Path('some_dir'))->isDir());
  341. }
  342. /**
  343. * Test 'Path' class 'isDir' method to check if a file's path is a directory
  344. *
  345. * @return void
  346. */
  347. public function testIsDirOnExistentFile(): void
  348. {
  349. touch(self::TEMP_TEST_DIR . "/foo");
  350. $this->assertFalse((new Path('some_dir'))->isDir());
  351. }
  352. /**
  353. * Test 'Path' class 'isDir' method on a non-existent directory
  354. *
  355. * @return void
  356. */
  357. public function testIsDirOnNonExistentDir(): void
  358. {
  359. $this->assertFalse((new Path('some_dir'))->isDir());
  360. }
  361. /**
  362. * Test 'Path' class 'ext' method to get the extension of the file
  363. *
  364. * @return void
  365. */
  366. public function testFileExtension(): void
  367. {
  368. $this->assertEquals(
  369. 'txt',
  370. (new Path("foo.txt"))->ext()
  371. );
  372. }
  373. /**
  374. * Test 'Path' class 'ext' method to get the extension of the file
  375. *
  376. * @return void
  377. */
  378. public function testEmptyExtension(): void
  379. {
  380. $this->assertEquals(
  381. '',
  382. (new Path("foo"))->ext()
  383. );
  384. }
  385. /**
  386. * Test 'Path' class 'basename' method to get the base name of a file
  387. *
  388. * @return void
  389. */
  390. public function testBaseName()
  391. {
  392. touch(self::TEMP_TEST_DIR . "/foo.txt");
  393. $this->assertEquals(
  394. 'foo.txt',
  395. (new Path("foo.txt"))->basename()
  396. );
  397. }
  398. /**
  399. * Test 'Path' class 'name' method to get the name of the file without extension
  400. *
  401. * @return void
  402. */
  403. public function testName()
  404. {
  405. touch(self::TEMP_TEST_DIR . "/foo");
  406. $this->assertEquals(
  407. 'foo',
  408. (new Path("foo"))->name()
  409. );
  410. }
  411. /**
  412. * Test 'Path' class 'name' method to get the name of the file with extension
  413. *
  414. * @return void
  415. */
  416. public function testNameWithExt()
  417. {
  418. touch(self::TEMP_TEST_DIR . "/foo.txt");
  419. $this->assertEquals(
  420. 'foo',
  421. (new Path("foo.txt"))->name()
  422. );
  423. }
  424. /**
  425. * Test 'Path' class 'mkdir' method to create a directory
  426. *
  427. * @return void
  428. * @throws FileExistsException
  429. */
  430. public function testMkDir(): void
  431. {
  432. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  433. $path->mkdir();
  434. $this->assertTrue($path->isDir());
  435. }
  436. /**
  437. * Test 'Path' class 'mkdir' method when directory already exists
  438. *
  439. * @throws FileExistsException If directory already exists
  440. */
  441. public function testMkDirExistingDir(): void {
  442. mkdir(self::TEMP_TEST_DIR . "/foo");
  443. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  444. $this->expectException(FileExistsException::class);
  445. $this->expectExceptionMessage("Directory already exists : " . self::TEMP_TEST_DIR . "/foo");
  446. $path->mkdir();
  447. }
  448. /**
  449. * Test 'Path' class 'mkdir' method to create a directory with existing directory and recursive option
  450. *
  451. * @return void
  452. * @throws FileExistsException
  453. */
  454. public function testMkDirExistingDirAndRecursive(): void {
  455. mkdir(self::TEMP_TEST_DIR . "/foo");
  456. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  457. $path->mkdir(0777, true);
  458. $this->assertTrue($path->isDir());
  459. }
  460. /**
  461. * Test 'Path' class 'mkdir' method to create a directory when a file with the same name already exists
  462. *
  463. * @return void
  464. * @throws FileExistsException When a file with the same name already exists
  465. */
  466. public function testMkDirExistingFile(): void {
  467. touch(self::TEMP_TEST_DIR . "/foo");
  468. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  469. $this->expectException(FileExistsException::class);
  470. $this->expectExceptionMessage("A file with this name already exists : " . self::TEMP_TEST_DIR . "/foo");
  471. $path->mkdir();
  472. }
  473. /**
  474. * Test 'Path' class 'mkdir' method to create a directory recursively
  475. *
  476. * @return void
  477. * @throws FileExistsException
  478. */
  479. public function testMkDirRecursive(): void {
  480. $path = new Path(self::TEMP_TEST_DIR . "/foo/bar");
  481. $path->mkdir(0777, true);
  482. $this->assertTrue($path->isDir());
  483. }
  484. /**
  485. * Test 'Path' class 'delete' method to delete a file.
  486. *
  487. * @return void
  488. * @throws FileNotFoundException
  489. */
  490. public function testDeleteFileSuccess(): void
  491. {
  492. touch(self::TEMP_TEST_DIR . "/foo");
  493. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  494. $this->assertTrue($path->isFile());
  495. $path->delete();
  496. $this->assertFalse($path->isFile());
  497. }
  498. /**
  499. * Test 'Path' class 'delete' method to delete a directory successfully
  500. *
  501. * @return void
  502. * @throws FileNotFoundException
  503. */
  504. public function testDeleteDirSuccess(): void
  505. {
  506. mkdir(self::TEMP_TEST_DIR . "/foo");
  507. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  508. $this->assertTrue($path->isDir());
  509. $path->delete();
  510. $this->assertFalse($path->isDir());
  511. }
  512. /**
  513. * Test 'Path' class 'delete' method to delete a non-existing file or dir
  514. *
  515. * @throws FileNotFoundException When the file does not exist
  516. */
  517. public function testDeleteNonExistingFile(): void
  518. {
  519. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  520. $this->assertFalse($path->isDir());
  521. $this->expectException(FileNotFoundException::class);
  522. $this->expectExceptionMessage("File does not exist : " . self::TEMP_TEST_DIR . "/foo");
  523. $path->delete();
  524. }
  525. }