PathTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 `eq` method with equal paths.
  59. *
  60. * Check that the method returns the correct result when the paths are equal.
  61. */
  62. public function testEqWithEqualPaths(): void
  63. {
  64. $path = new Path('/foo/bar');
  65. $this->assertTrue($path->eq('/foo/bar'));
  66. }
  67. /**
  68. * Test `eq` method with different paths.
  69. *
  70. * Check that the method returns the correct result when the paths are different.
  71. */
  72. public function testEqWithDifferentPaths(): void
  73. {
  74. $path = new Path('/foo/bar');
  75. $this->assertFalse($path->eq('/foo/zzz'));
  76. }
  77. /**
  78. * Test `eq` method with empty path.
  79. *
  80. * Check that the method returns the correct result when the path is empty.
  81. */
  82. public function testEqWithEmptyPath(): void
  83. {
  84. $path = new Path('/foo/bar');
  85. $this->assertFalse($path->eq(''));
  86. }
  87. /**
  88. * Test the append method of the Path class.
  89. *
  90. * @return void
  91. */
  92. public function testAppend(): void
  93. {
  94. $path = new Path('/foo');
  95. $this->assertEquals(
  96. "/foo/bar",
  97. $path->append('bar')
  98. );
  99. // One part
  100. $this->assertTrue(
  101. (new Path('/home'))->append('user')->eq('/home/user')
  102. );
  103. // Multiple parts
  104. $this->assertTrue(
  105. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  106. );
  107. // Absolute path passed in $parts
  108. $this->assertTrue(
  109. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  110. );
  111. }
  112. /**
  113. * Test the abspath method of the Path class.
  114. *
  115. * @return void
  116. */
  117. public function testAbsPath(): void
  118. {
  119. touch(self::TEMP_TEST_DIR . "/foo");
  120. chdir(self::TEMP_TEST_DIR);
  121. $this->assertEquals(
  122. self::TEMP_TEST_DIR . "/foo",
  123. (new Path('foo'))->abspath()
  124. );
  125. }
  126. /**
  127. * Test the abspath method of the Path class with a relative path.
  128. *
  129. * @return void
  130. */
  131. public function testAbsPathWithRelative(): void
  132. {
  133. mkdir(self::TEMP_TEST_DIR . "/foo");
  134. touch(self::TEMP_TEST_DIR . "/bar");
  135. chdir(self::TEMP_TEST_DIR . "/foo");
  136. $this->assertEquals(
  137. self::TEMP_TEST_DIR . "/bar",
  138. (new Path('../bar'))->abspath()
  139. );
  140. }
  141. /**
  142. * Test 'Path' class 'access' method to check existence of the file
  143. */
  144. public function testAccessCheckExistenceOfFile(): void
  145. {
  146. $filePath = self::TEMP_TEST_DIR . "/foo";
  147. touch($filePath);
  148. chmod($filePath, 777);
  149. $result = (new Path('foo'))->access(Path::F_OK);
  150. $this->assertTrue($result);
  151. }
  152. /**
  153. * Test 'Path' class 'access' method to check existence of the non-existent file
  154. */
  155. public function testAccessCheckExistenceOfNonExistingFile(): void
  156. {
  157. $result = (new Path('foo'))->access(Path::F_OK);
  158. $this->assertFalse($result);
  159. }
  160. /**
  161. * Test 'Path' class 'access' method to check read permission of the file
  162. */
  163. public function testAccessCheckReadPermissionOfFile(): void
  164. {
  165. $filePath = self::TEMP_TEST_DIR . "/foo";
  166. touch($filePath);
  167. chmod($filePath, 777);
  168. $result = (new Path('foo'))->access(Path::R_OK);
  169. $this->assertTrue($result);
  170. }
  171. // /**
  172. // * Test 'Path' class 'access' method to check read permission of the file (no permission)
  173. // */
  174. // public function testAccessCheckReadPermissionOfFileNoRight(): void
  175. // {
  176. // $filePath = self::TEMP_TEST_DIR . "/foo";
  177. // touch($filePath);
  178. // chmod($filePath, 000);
  179. //
  180. // $result = (new Path('foo'))->access(Path::R_OK);
  181. // $this->assertFalse($result);
  182. // }
  183. /**
  184. * Test 'Path' class 'access' method to check write permission of the file
  185. */
  186. public function testAccessCheckWritePermissionOfFile(): void
  187. {
  188. $filePath = self::TEMP_TEST_DIR . "/foo";
  189. touch($filePath);
  190. chmod($filePath, 777);
  191. $result = (new Path('foo'))->access(Path::W_OK);
  192. $this->assertTrue($result);
  193. }
  194. // /**
  195. // * Test 'Path' class 'access' method to check write permission of the file (no permission)
  196. // */
  197. // public function testAccessCheckWritePermissionOfFileNoRight(): void
  198. // {
  199. // $filePath = self::TEMP_TEST_DIR . "/foo";
  200. // touch($filePath);
  201. // chmod($filePath, 000);
  202. //
  203. // $result = (new Path('foo'))->access(Path::W_OK);
  204. // $this->assertFalse($result);
  205. // }
  206. /**
  207. * Test 'Path' class 'access' method to check execute permission of the file
  208. */
  209. public function testAccessCheckExecutePermissionOfFile(): void
  210. {
  211. $filePath = self::TEMP_TEST_DIR . "/foo";
  212. touch($filePath);
  213. chmod($filePath, 777);
  214. $result = (new Path('foo'))->access(Path::X_OK);
  215. $this->assertTrue($result);
  216. }
  217. /**
  218. * Test 'Path' class 'access' method to check existence of the file
  219. */
  220. public function testAccessCheckExecutePermissionOfFileNoRight(): void
  221. {
  222. $filePath = self::TEMP_TEST_DIR . "/foo";
  223. touch($filePath);
  224. chmod($filePath, 000);
  225. $result = (new Path('foo'))->access(Path::X_OK);
  226. $this->assertFalse($result);
  227. }
  228. /**
  229. * Test 'Path' class 'access' method with an invalid mode parameter
  230. */
  231. public function testAccessInvalidModeParameter(): void
  232. {
  233. $this->expectException(\RuntimeException::class);
  234. (new Path('foo'))->access(123);
  235. }
  236. /**
  237. * Test 'Path' class 'atime' method to get the access time of a file
  238. *
  239. * @return void
  240. */
  241. public function testATime()
  242. {
  243. touch(self::TEMP_TEST_DIR . "/foo");
  244. $atime = (new Path('foo'))->atime();
  245. $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
  246. $this->assertMatchesRegularExpression(
  247. "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
  248. $atime
  249. );
  250. }
  251. /**
  252. * Test 'Path' class 'isFile' method to check if the file exists
  253. *
  254. * @return void
  255. */
  256. public function testIsFileOnActualFile(): void
  257. {
  258. touch(self::TEMP_TEST_DIR . "/foo");
  259. $this->assertTrue((new Path('foo'))->isFile());
  260. }
  261. /**
  262. * Test 'Path' class 'isFile' method to check if a non-existent file exists
  263. *
  264. * @return void
  265. */
  266. public function testIsFileOnNonExistentFile(): void
  267. {
  268. $this->assertFalse((new Path('foo'))->isFile());
  269. }
  270. /**
  271. * Test 'Path' class 'isFile' method to check if a file exists on the given directory
  272. *
  273. * @return void
  274. */
  275. public function testIsFileOnExistentDir(): void
  276. {
  277. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  278. $this->assertFalse((new Path('some_dir'))->isFile());
  279. }
  280. /**
  281. * Test 'Path' class 'isDir' method to check if the path is a directory
  282. *
  283. * @return void
  284. */
  285. public function testIsDirOnActualDir(): void
  286. {
  287. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  288. $this->assertTrue((new Path('some_dir'))->isDir());
  289. }
  290. /**
  291. * Test 'Path' class 'isDir' method to check if a file's path is a directory
  292. *
  293. * @return void
  294. */
  295. public function testIsDirOnExistentFile(): void
  296. {
  297. touch(self::TEMP_TEST_DIR . "/foo");
  298. $this->assertFalse((new Path('some_dir'))->isDir());
  299. }
  300. /**
  301. * Test 'Path' class 'isDir' method on a non-existent directory
  302. *
  303. * @return void
  304. */
  305. public function testIsDirOnNonExistentDir(): void
  306. {
  307. $this->assertFalse((new Path('some_dir'))->isDir());
  308. }
  309. /**
  310. * Test 'Path' class 'ext' method to get the extension of the file
  311. *
  312. * @return void
  313. */
  314. public function testFileExtension(): void
  315. {
  316. $this->assertEquals(
  317. 'txt',
  318. (new Path("foo.txt"))->ext()
  319. );
  320. }
  321. /**
  322. * Test 'Path' class 'ext' method to get the extension of the file
  323. *
  324. * @return void
  325. */
  326. public function testEmptyExtension(): void
  327. {
  328. $this->assertEquals(
  329. '',
  330. (new Path("foo"))->ext()
  331. );
  332. }
  333. /**
  334. * Test 'Path' class 'basename' method to get the base name of a file
  335. *
  336. * @return void
  337. */
  338. public function testBaseName()
  339. {
  340. touch(self::TEMP_TEST_DIR . "/foo.txt");
  341. $this->assertEquals(
  342. 'foo.txt',
  343. (new Path("foo.txt"))->basename()
  344. );
  345. }
  346. /**
  347. * Test 'Path' class 'name' method to get the name of the file without extension
  348. *
  349. * @return void
  350. */
  351. public function testName()
  352. {
  353. touch(self::TEMP_TEST_DIR . "/foo");
  354. $this->assertEquals(
  355. 'foo',
  356. (new Path("foo"))->name()
  357. );
  358. }
  359. /**
  360. * Test 'Path' class 'name' method to get the name of the file with extension
  361. *
  362. * @return void
  363. */
  364. public function testNameWithExt()
  365. {
  366. touch(self::TEMP_TEST_DIR . "/foo.txt");
  367. $this->assertEquals(
  368. 'foo',
  369. (new Path("foo.txt"))->name()
  370. );
  371. }
  372. /**
  373. * Test 'Path' class 'mkdir' method to create a directory
  374. *
  375. * @return void
  376. * @throws FileExistsException
  377. */
  378. public function testMkDir(): void
  379. {
  380. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  381. $path->mkdir();
  382. $this->assertTrue($path->isDir());
  383. }
  384. /**
  385. * Test 'Path' class 'mkdir' method when directory already exists
  386. *
  387. * @throws FileExistsException If directory already exists
  388. */
  389. public function testMkDirExistingDir(): void {
  390. mkdir(self::TEMP_TEST_DIR . "/foo");
  391. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  392. $this->expectException(FileExistsException::class);
  393. $this->expectExceptionMessage("Directory already exists : " . self::TEMP_TEST_DIR . "/foo");
  394. $path->mkdir();
  395. }
  396. /**
  397. * Test 'Path' class 'mkdir' method to create a directory with existing directory and recursive option
  398. *
  399. * @return void
  400. * @throws FileExistsException
  401. */
  402. public function testMkDirExistingDirAndRecursive(): void {
  403. mkdir(self::TEMP_TEST_DIR . "/foo");
  404. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  405. $path->mkdir(0777, true);
  406. $this->assertTrue($path->isDir());
  407. }
  408. /**
  409. * Test 'Path' class 'mkdir' method to create a directory when a file with the same name already exists
  410. *
  411. * @return void
  412. * @throws FileExistsException When a file with the same name already exists
  413. */
  414. public function testMkDirExistingFile(): void {
  415. touch(self::TEMP_TEST_DIR . "/foo");
  416. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  417. $this->expectException(FileExistsException::class);
  418. $this->expectExceptionMessage("A file with this name already exists : " . self::TEMP_TEST_DIR . "/foo");
  419. $path->mkdir();
  420. }
  421. /**
  422. * Test 'Path' class 'mkdir' method to create a directory recursively
  423. *
  424. * @return void
  425. * @throws FileExistsException
  426. */
  427. public function testMkDirRecursive(): void {
  428. $path = new Path(self::TEMP_TEST_DIR . "/foo/bar");
  429. $path->mkdir(0777, true);
  430. $this->assertTrue($path->isDir());
  431. }
  432. /**
  433. * Test 'Path' class 'delete' method to delete a file.
  434. *
  435. * @return void
  436. * @throws FileNotFoundException
  437. */
  438. public function testDeleteFileSuccess(): void
  439. {
  440. touch(self::TEMP_TEST_DIR . "/foo");
  441. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  442. $this->assertTrue($path->isFile());
  443. $path->delete();
  444. $this->assertFalse($path->isFile());
  445. }
  446. /**
  447. * Test 'Path' class 'delete' method to delete a directory successfully
  448. *
  449. * @return void
  450. * @throws FileNotFoundException
  451. */
  452. public function testDeleteDirSuccess(): void
  453. {
  454. mkdir(self::TEMP_TEST_DIR . "/foo");
  455. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  456. $this->assertTrue($path->isDir());
  457. $path->delete();
  458. $this->assertFalse($path->isDir());
  459. }
  460. /**
  461. * Test 'Path' class 'delete' method to delete a non-existing file or dir
  462. *
  463. * @throws FileNotFoundException When the file does not exist
  464. */
  465. public function testDeleteNonExistingFile(): void
  466. {
  467. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  468. $this->assertFalse($path->isDir());
  469. $this->expectException(FileNotFoundException::class);
  470. $this->expectExceptionMessage("File does not exist : " . self::TEMP_TEST_DIR . "/foo");
  471. $path->delete();
  472. }
  473. }