PathTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Path\Tests;
  3. use Path\Path;
  4. use PHPUnit\Framework\TestCase;
  5. class PathTest extends TestCase
  6. {
  7. const TEMP_TEST_DIR = __DIR__ . "/temp";
  8. protected Path $pathClass;
  9. public function setUp(): void
  10. {
  11. mkdir(self::TEMP_TEST_DIR);
  12. chdir(self::TEMP_TEST_DIR);
  13. }
  14. private function rmDirs(string $dir): void {
  15. // Remove and replace by a proper tempdir method
  16. foreach(scandir($dir) as $file) {
  17. if ('.' === $file || '..' === $file) continue;
  18. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
  19. else unlink($dir . DIRECTORY_SEPARATOR . $file);
  20. }
  21. rmdir($dir);
  22. }
  23. public function tearDown(): void
  24. {
  25. $this->rmDirs(self::TEMP_TEST_DIR);
  26. chdir(__DIR__);
  27. }
  28. public function testToString(): void
  29. {
  30. $path = new Path('/foo/bar');
  31. $this->assertEquals('/foo/bar', $path->__toString());
  32. }
  33. /**
  34. * Test 'join' method.
  35. */
  36. public function testJoin(): void
  37. {
  38. // One part
  39. $this->assertEquals(
  40. '/home/user',
  41. Path::join('/home', 'user')
  42. );
  43. // Multiple parts
  44. $this->assertEquals(
  45. '/home/user/documents',
  46. Path::join('/home', 'user', 'documents')
  47. );
  48. // Absolute path passed in $parts
  49. $this->assertEquals(
  50. '/user/documents',
  51. Path::join('home', '/user', 'documents')
  52. );
  53. }
  54. /**
  55. * Test `eq` method with equal paths.
  56. *
  57. * Check that the method returns the correct result when the paths are equal.
  58. */
  59. public function testEqWithEqualPaths(): void
  60. {
  61. $path = new Path('/foo/bar');
  62. $this->assertTrue($path->eq('/foo/bar'));
  63. }
  64. /**
  65. * Test `eq` method with different paths.
  66. *
  67. * Check that the method returns the correct result when the paths are different.
  68. */
  69. public function testEqWithDifferentPaths(): void
  70. {
  71. $path = new Path('/foo/bar');
  72. $this->assertFalse($path->eq('/foo/zzz'));
  73. }
  74. /**
  75. * Test `eq` method with empty path.
  76. *
  77. * Check that the method returns the correct result when the path is empty.
  78. */
  79. public function testEqWithEmptyPath(): void
  80. {
  81. $path = new Path('/foo/bar');
  82. $this->assertFalse($path->eq(''));
  83. }
  84. /**
  85. * Test the append method of the Path class.
  86. *
  87. * @return void
  88. */
  89. public function testAppend(): void
  90. {
  91. $path = new Path('/foo');
  92. $this->assertEquals(
  93. "/foo/bar",
  94. $path->append('bar')
  95. );
  96. // One part
  97. $this->assertTrue(
  98. (new Path('/home'))->append('user')->eq('/home/user')
  99. );
  100. // Multiple parts
  101. $this->assertTrue(
  102. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  103. );
  104. // Absolute path passed in $parts
  105. $this->assertTrue(
  106. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  107. );
  108. }
  109. /**
  110. * Test the abspath method of the Path class.
  111. *
  112. * @return void
  113. */
  114. public function testAbsPath(): void
  115. {
  116. touch(self::TEMP_TEST_DIR . "/foo");
  117. chdir(self::TEMP_TEST_DIR);
  118. $this->assertEquals(
  119. self::TEMP_TEST_DIR . "/foo",
  120. (new Path('foo'))->abspath()
  121. );
  122. }
  123. /**
  124. * Test the abspath method of the Path class with a relative path.
  125. *
  126. * @return void
  127. */
  128. public function testAbsPathWithRelative(): void
  129. {
  130. mkdir(self::TEMP_TEST_DIR . "/foo");
  131. touch(self::TEMP_TEST_DIR . "/bar");
  132. chdir(self::TEMP_TEST_DIR . "/foo");
  133. $this->assertEquals(
  134. self::TEMP_TEST_DIR . "/bar",
  135. (new Path('../bar'))->abspath()
  136. );
  137. }
  138. /**
  139. * Test 'Path' class 'access' method to check existence of the file
  140. */
  141. public function testAccessCheckExistenceOfFile(): void
  142. {
  143. $filePath = self::TEMP_TEST_DIR . "/foo";
  144. touch($filePath);
  145. chmod($filePath, 777);
  146. $result = (new Path('foo'))->access(Path::F_OK);
  147. $this->assertTrue($result);
  148. }
  149. /**
  150. * Test 'Path' class 'access' method to check existence of the non-existent file
  151. */
  152. public function testAccessCheckExistenceOfNonExistingFile(): void
  153. {
  154. $result = (new Path('foo'))->access(Path::F_OK);
  155. $this->assertFalse($result);
  156. }
  157. /**
  158. * Test 'Path' class 'access' method to check read permission of the file
  159. */
  160. public function testAccessCheckReadPermissionOfFile(): void
  161. {
  162. $filePath = self::TEMP_TEST_DIR . "/foo";
  163. touch($filePath);
  164. chmod($filePath, 777);
  165. $result = (new Path('foo'))->access(Path::R_OK);
  166. $this->assertTrue($result);
  167. }
  168. // /**
  169. // * Test 'Path' class 'access' method to check read permission of the file (no permission)
  170. // */
  171. // public function testAccessCheckReadPermissionOfFileNoRight(): void
  172. // {
  173. // $filePath = self::TEMP_TEST_DIR . "/foo";
  174. // touch($filePath);
  175. // chmod($filePath, 000);
  176. //
  177. // $result = (new Path('foo'))->access(Path::R_OK);
  178. // $this->assertFalse($result);
  179. // }
  180. /**
  181. * Test 'Path' class 'access' method to check write permission of the file
  182. */
  183. public function testAccessCheckWritePermissionOfFile(): void
  184. {
  185. $filePath = self::TEMP_TEST_DIR . "/foo";
  186. touch($filePath);
  187. chmod($filePath, 777);
  188. $result = (new Path('foo'))->access(Path::W_OK);
  189. $this->assertTrue($result);
  190. }
  191. // /**
  192. // * Test 'Path' class 'access' method to check write permission of the file (no permission)
  193. // */
  194. // public function testAccessCheckWritePermissionOfFileNoRight(): void
  195. // {
  196. // $filePath = self::TEMP_TEST_DIR . "/foo";
  197. // touch($filePath);
  198. // chmod($filePath, 000);
  199. //
  200. // $result = (new Path('foo'))->access(Path::W_OK);
  201. // $this->assertFalse($result);
  202. // }
  203. /**
  204. * Test 'Path' class 'access' method to check execute permission of the file
  205. */
  206. public function testAccessCheckExecutePermissionOfFile(): void
  207. {
  208. $filePath = self::TEMP_TEST_DIR . "/foo";
  209. touch($filePath);
  210. chmod($filePath, 777);
  211. $result = (new Path('foo'))->access(Path::X_OK);
  212. $this->assertTrue($result);
  213. }
  214. /**
  215. * Test 'Path' class 'access' method to check existence of the file
  216. */
  217. public function testAccessCheckExecutePermissionOfFileNoRight(): void
  218. {
  219. $filePath = self::TEMP_TEST_DIR . "/foo";
  220. touch($filePath);
  221. chmod($filePath, 000);
  222. $result = (new Path('foo'))->access(Path::X_OK);
  223. $this->assertFalse($result);
  224. }
  225. /**
  226. * Test 'Path' class 'access' method with an invalid mode parameter
  227. */
  228. public function testAccessInvalidModeParameter(): void
  229. {
  230. $this->expectException(\RuntimeException::class);
  231. (new Path('foo'))->access(123);
  232. }
  233. /**
  234. * Test 'Path' class 'atime' method to get the access time of a file
  235. *
  236. * @return void
  237. */
  238. public function testATime()
  239. {
  240. touch(self::TEMP_TEST_DIR . "/foo");
  241. $atime = (new Path('foo'))->atime();
  242. $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
  243. $this->assertMatchesRegularExpression(
  244. "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
  245. $atime
  246. );
  247. }
  248. public function testIsFileOnActualFile(): void
  249. {
  250. touch(self::TEMP_TEST_DIR . "/foo");
  251. $this->assertTrue((new Path('foo'))->isFile());
  252. }
  253. public function testIsFileOnNonExistentFile(): void
  254. {
  255. $this->assertFalse((new Path('foo'))->isFile());
  256. }
  257. }