PathTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. // TODO: consider using sys_get_temp_dir()
  12. protected Path $pathClass;
  13. public function setUp(): void
  14. {
  15. mkdir(self::TEMP_TEST_DIR);
  16. chdir(self::TEMP_TEST_DIR);
  17. }
  18. private function rmDirs(string $dir): void {
  19. // Remove and replace by a proper tempdir method
  20. foreach(scandir($dir) as $file) {
  21. if ('.' === $file || '..' === $file) continue;
  22. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
  23. else unlink($dir . DIRECTORY_SEPARATOR . $file);
  24. }
  25. rmdir($dir);
  26. }
  27. public function tearDown(): void
  28. {
  29. $this->rmDirs(self::TEMP_TEST_DIR);
  30. chdir(__DIR__);
  31. }
  32. public function testToString(): void
  33. {
  34. $path = new Path('/foo/bar');
  35. $this->assertEquals('/foo/bar', $path->__toString());
  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. /**
  59. * Test 'Path' class 'copy_dir' method to copy a directory
  60. *
  61. * @return void
  62. * @throws FileExistsException
  63. * @throws FileNotFoundException
  64. */
  65. public function testCopyDir(): void
  66. {
  67. $src = self::TEMP_TEST_DIR . "/some_dir";
  68. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  69. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  70. mkdir($src);
  71. touch($srcContent);
  72. mkdir($dst);
  73. Path::copy_dir($src, $dst);
  74. $this->assertTrue(
  75. file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
  76. );
  77. }
  78. /**
  79. * Test 'Path' class 'copy_dir' method when the destination directory does not exist
  80. *
  81. * @throws FileNotFoundException|FileExistsException
  82. */
  83. public function testCopyDirWhenDestinationDirectoryNotExists(): void
  84. {
  85. $src = self::TEMP_TEST_DIR . "/some_dir";
  86. $dst = self::TEMP_TEST_DIR . "/non_existing_dir";
  87. mkdir($src);
  88. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  89. $this->expectException(FileNotFoundException::class);
  90. $this->expectExceptionMessage("Directory does not exist : " . $dst);
  91. Path::copy_dir($src, $dst);
  92. }
  93. /**
  94. * Test 'Path' class 'copy_dir' method when the destination directory already exists.
  95. *
  96. * @throws FileNotFoundException|FileExistsException if the destination directory already exists.
  97. */
  98. public function testCopyDirWhenDirectoryAlreadyExistsAtDestination(): void
  99. {
  100. $src = self::TEMP_TEST_DIR . "/some_dir";
  101. $dst = self::TEMP_TEST_DIR . "/other_dir";
  102. mkdir($src);
  103. touch($src . DIRECTORY_SEPARATOR . "foo.txt");
  104. mkdir($dst);
  105. mkdir($dst . DIRECTORY_SEPARATOR . "some_dir");
  106. $this->expectException(FileExistsException::class);
  107. $this->expectExceptionMessage("Directory already exists : " . $dst);
  108. Path::copy_dir($src, $dst);
  109. }
  110. /**
  111. * Test `eq` method with equal paths.
  112. *
  113. * Check that the method returns the correct result when the paths are equal.
  114. */
  115. public function testEqWithEqualPaths(): void
  116. {
  117. $path = new Path('/foo/bar');
  118. $this->assertTrue($path->eq('/foo/bar'));
  119. }
  120. /**
  121. * Test `eq` method with different paths.
  122. *
  123. * Check that the method returns the correct result when the paths are different.
  124. */
  125. public function testEqWithDifferentPaths(): void
  126. {
  127. $path = new Path('/foo/bar');
  128. $this->assertFalse($path->eq('/foo/zzz'));
  129. }
  130. /**
  131. * Test `eq` method with empty path.
  132. *
  133. * Check that the method returns the correct result when the path is empty.
  134. */
  135. public function testEqWithEmptyPath(): void
  136. {
  137. $path = new Path('/foo/bar');
  138. $this->assertFalse($path->eq(''));
  139. }
  140. /**
  141. * Test the append method of the Path class.
  142. *
  143. * @return void
  144. */
  145. public function testAppend(): void
  146. {
  147. $path = new Path('/foo');
  148. $this->assertEquals(
  149. "/foo/bar",
  150. $path->append('bar')
  151. );
  152. // One part
  153. $this->assertTrue(
  154. (new Path('/home'))->append('user')->eq('/home/user')
  155. );
  156. // Multiple parts
  157. $this->assertTrue(
  158. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  159. );
  160. // Absolute path passed in $parts
  161. $this->assertTrue(
  162. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  163. );
  164. }
  165. /**
  166. * Test the abspath method of the Path class.
  167. *
  168. * @return void
  169. */
  170. public function testAbsPath(): void
  171. {
  172. touch(self::TEMP_TEST_DIR . "/foo");
  173. chdir(self::TEMP_TEST_DIR);
  174. $this->assertEquals(
  175. self::TEMP_TEST_DIR . "/foo",
  176. (new Path('foo'))->abspath()
  177. );
  178. }
  179. /**
  180. * Test the abspath method of the Path class with a relative path.
  181. *
  182. * @return void
  183. */
  184. public function testAbsPathWithRelative(): void
  185. {
  186. mkdir(self::TEMP_TEST_DIR . "/foo");
  187. touch(self::TEMP_TEST_DIR . "/bar");
  188. chdir(self::TEMP_TEST_DIR . "/foo");
  189. $this->assertEquals(
  190. self::TEMP_TEST_DIR . "/bar",
  191. (new Path('../bar'))->abspath()
  192. );
  193. }
  194. /**
  195. * Test 'Path' class 'access' method to check existence of the file
  196. */
  197. public function testAccessCheckExistenceOfFile(): void
  198. {
  199. $filePath = self::TEMP_TEST_DIR . "/foo";
  200. touch($filePath);
  201. chmod($filePath, 777);
  202. $result = (new Path('foo'))->access(Path::F_OK);
  203. $this->assertTrue($result);
  204. }
  205. /**
  206. * Test 'Path' class 'access' method to check existence of the non-existent file
  207. */
  208. public function testAccessCheckExistenceOfNonExistingFile(): void
  209. {
  210. $result = (new Path('foo'))->access(Path::F_OK);
  211. $this->assertFalse($result);
  212. }
  213. /**
  214. * Test 'Path' class 'access' method to check read permission of the file
  215. */
  216. public function testAccessCheckReadPermissionOfFile(): void
  217. {
  218. $filePath = self::TEMP_TEST_DIR . "/foo";
  219. touch($filePath);
  220. chmod($filePath, 777);
  221. $result = (new Path('foo'))->access(Path::R_OK);
  222. $this->assertTrue($result);
  223. }
  224. // /**
  225. // * Test 'Path' class 'access' method to check read permission of the file (no permission)
  226. // */
  227. // public function testAccessCheckReadPermissionOfFileNoRight(): void
  228. // {
  229. // $filePath = self::TEMP_TEST_DIR . "/foo";
  230. // touch($filePath);
  231. // chmod($filePath, 000);
  232. //
  233. // $result = (new Path('foo'))->access(Path::R_OK);
  234. // $this->assertFalse($result);
  235. // }
  236. /**
  237. * Test 'Path' class 'access' method to check write permission of the file
  238. */
  239. public function testAccessCheckWritePermissionOfFile(): void
  240. {
  241. $filePath = self::TEMP_TEST_DIR . "/foo";
  242. touch($filePath);
  243. chmod($filePath, 777);
  244. $result = (new Path('foo'))->access(Path::W_OK);
  245. $this->assertTrue($result);
  246. }
  247. // /**
  248. // * Test 'Path' class 'access' method to check write permission of the file (no permission)
  249. // */
  250. // public function testAccessCheckWritePermissionOfFileNoRight(): void
  251. // {
  252. // $filePath = self::TEMP_TEST_DIR . "/foo";
  253. // touch($filePath);
  254. // chmod($filePath, 000);
  255. //
  256. // $result = (new Path('foo'))->access(Path::W_OK);
  257. // $this->assertFalse($result);
  258. // }
  259. /**
  260. * Test 'Path' class 'access' method to check execute permission of the file
  261. */
  262. public function testAccessCheckExecutePermissionOfFile(): void
  263. {
  264. $filePath = self::TEMP_TEST_DIR . "/foo";
  265. touch($filePath);
  266. chmod($filePath, 777);
  267. $result = (new Path('foo'))->access(Path::X_OK);
  268. $this->assertTrue($result);
  269. }
  270. /**
  271. * Test 'Path' class 'access' method to check existence of the file
  272. */
  273. public function testAccessCheckExecutePermissionOfFileNoRight(): void
  274. {
  275. $filePath = self::TEMP_TEST_DIR . "/foo";
  276. touch($filePath);
  277. chmod($filePath, 000);
  278. $result = (new Path('foo'))->access(Path::X_OK);
  279. $this->assertFalse($result);
  280. }
  281. /**
  282. * Test 'Path' class 'access' method with an invalid mode parameter
  283. */
  284. public function testAccessInvalidModeParameter(): void
  285. {
  286. $this->expectException(\RuntimeException::class);
  287. (new Path('foo'))->access(123);
  288. }
  289. /**
  290. * Test 'Path' class 'atime' method to get the access time of a file
  291. *
  292. * @return void
  293. */
  294. public function testATime()
  295. {
  296. touch(self::TEMP_TEST_DIR . "/foo");
  297. $atime = (new Path('foo'))->atime();
  298. $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
  299. $this->assertMatchesRegularExpression(
  300. "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
  301. $atime
  302. );
  303. }
  304. /**
  305. * Test 'Path' class 'isFile' method to check if the file exists
  306. *
  307. * @return void
  308. */
  309. public function testIsFileOnActualFile(): void
  310. {
  311. touch(self::TEMP_TEST_DIR . "/foo");
  312. $this->assertTrue((new Path('foo'))->isFile());
  313. }
  314. /**
  315. * Test 'Path' class 'isFile' method to check if a non-existent file exists
  316. *
  317. * @return void
  318. */
  319. public function testIsFileOnNonExistentFile(): void
  320. {
  321. $this->assertFalse((new Path('foo'))->isFile());
  322. }
  323. /**
  324. * Test 'Path' class 'isFile' method to check if a file exists on the given directory
  325. *
  326. * @return void
  327. */
  328. public function testIsFileOnExistentDir(): void
  329. {
  330. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  331. $this->assertFalse((new Path('some_dir'))->isFile());
  332. }
  333. /**
  334. * Test 'Path' class 'isDir' method to check if the path is a directory
  335. *
  336. * @return void
  337. */
  338. public function testIsDirOnActualDir(): void
  339. {
  340. mkdir(self::TEMP_TEST_DIR . "/some_dir");
  341. $this->assertTrue((new Path('some_dir'))->isDir());
  342. }
  343. /**
  344. * Test 'Path' class 'isDir' method to check if a file's path is a directory
  345. *
  346. * @return void
  347. */
  348. public function testIsDirOnExistentFile(): void
  349. {
  350. touch(self::TEMP_TEST_DIR . "/foo");
  351. $this->assertFalse((new Path('some_dir'))->isDir());
  352. }
  353. /**
  354. * Test 'Path' class 'isDir' method on a non-existent directory
  355. *
  356. * @return void
  357. */
  358. public function testIsDirOnNonExistentDir(): void
  359. {
  360. $this->assertFalse((new Path('some_dir'))->isDir());
  361. }
  362. /**
  363. * Test 'Path' class 'ext' method to get the extension of the file
  364. *
  365. * @return void
  366. */
  367. public function testFileExtension(): void
  368. {
  369. $this->assertEquals(
  370. 'txt',
  371. (new Path("foo.txt"))->ext()
  372. );
  373. }
  374. /**
  375. * Test 'Path' class 'ext' method to get the extension of the file
  376. *
  377. * @return void
  378. */
  379. public function testEmptyExtension(): void
  380. {
  381. $this->assertEquals(
  382. '',
  383. (new Path("foo"))->ext()
  384. );
  385. }
  386. /**
  387. * Test 'Path' class 'basename' method to get the base name of a file
  388. *
  389. * @return void
  390. */
  391. public function testBaseName()
  392. {
  393. touch(self::TEMP_TEST_DIR . "/foo.txt");
  394. $this->assertEquals(
  395. 'foo.txt',
  396. (new Path("foo.txt"))->basename()
  397. );
  398. }
  399. /**
  400. * Test 'Path' class 'name' method to get the name of the file without extension
  401. *
  402. * @return void
  403. */
  404. public function testName()
  405. {
  406. touch(self::TEMP_TEST_DIR . "/foo");
  407. $this->assertEquals(
  408. 'foo',
  409. (new Path("foo"))->name()
  410. );
  411. }
  412. /**
  413. * Test 'Path' class 'name' method to get the name of the file with extension
  414. *
  415. * @return void
  416. */
  417. public function testNameWithExt()
  418. {
  419. touch(self::TEMP_TEST_DIR . "/foo.txt");
  420. $this->assertEquals(
  421. 'foo',
  422. (new Path("foo.txt"))->name()
  423. );
  424. }
  425. /**
  426. * Test 'Path' class 'mkdir' method to create a directory
  427. *
  428. * @return void
  429. * @throws FileExistsException
  430. */
  431. public function testMkDir(): void
  432. {
  433. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  434. $path->mkdir();
  435. $this->assertTrue($path->isDir());
  436. }
  437. /**
  438. * Test 'Path' class 'mkdir' method when directory already exists
  439. *
  440. * @throws FileExistsException If directory already exists
  441. */
  442. public function testMkDirExistingDir(): void {
  443. mkdir(self::TEMP_TEST_DIR . "/foo");
  444. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  445. $this->expectException(FileExistsException::class);
  446. $this->expectExceptionMessage("Directory already exists : " . self::TEMP_TEST_DIR . "/foo");
  447. $path->mkdir();
  448. }
  449. /**
  450. * Test 'Path' class 'mkdir' method to create a directory with existing directory and recursive option
  451. *
  452. * @return void
  453. * @throws FileExistsException
  454. */
  455. public function testMkDirExistingDirAndRecursive(): void {
  456. mkdir(self::TEMP_TEST_DIR . "/foo");
  457. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  458. $path->mkdir(0777, true);
  459. $this->assertTrue($path->isDir());
  460. }
  461. /**
  462. * Test 'Path' class 'mkdir' method to create a directory when a file with the same name already exists
  463. *
  464. * @return void
  465. * @throws FileExistsException When a file with the same name already exists
  466. */
  467. public function testMkDirExistingFile(): void {
  468. touch(self::TEMP_TEST_DIR . "/foo");
  469. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  470. $this->expectException(FileExistsException::class);
  471. $this->expectExceptionMessage("A file with this name already exists : " . self::TEMP_TEST_DIR . "/foo");
  472. $path->mkdir();
  473. }
  474. /**
  475. * Test 'Path' class 'mkdir' method to create a directory recursively
  476. *
  477. * @return void
  478. * @throws FileExistsException
  479. */
  480. public function testMkDirRecursive(): void {
  481. $path = new Path(self::TEMP_TEST_DIR . "/foo/bar");
  482. $path->mkdir(0777, true);
  483. $this->assertTrue($path->isDir());
  484. }
  485. /**
  486. * Test 'Path' class 'delete' method to delete a file.
  487. *
  488. * @return void
  489. * @throws FileNotFoundException
  490. */
  491. public function testDeleteFileSuccess(): void
  492. {
  493. touch(self::TEMP_TEST_DIR . "/foo");
  494. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  495. $this->assertTrue($path->isFile());
  496. $path->delete();
  497. $this->assertFalse($path->isFile());
  498. }
  499. /**
  500. * Test 'Path' class 'delete' method to delete a directory successfully
  501. *
  502. * @return void
  503. * @throws FileNotFoundException
  504. */
  505. public function testDeleteDirSuccess(): void
  506. {
  507. mkdir(self::TEMP_TEST_DIR . "/foo");
  508. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  509. $this->assertTrue($path->isDir());
  510. $path->delete();
  511. $this->assertFalse($path->isDir());
  512. }
  513. /**
  514. * Test 'Path' class 'delete' method to delete a non-existing file or dir
  515. *
  516. * @throws FileNotFoundException When the file does not exist
  517. */
  518. public function testDeleteNonExistingFile(): void
  519. {
  520. $path = new Path(self::TEMP_TEST_DIR . "/foo");
  521. $this->assertFalse($path->isDir());
  522. $this->expectException(FileNotFoundException::class);
  523. $this->expectExceptionMessage("File does not exist : " . self::TEMP_TEST_DIR . "/foo");
  524. $path->delete();
  525. }
  526. /**
  527. * Test 'Path' class 'copy_dir' method to copy a file
  528. *
  529. * @return void
  530. * @throws FileExistsException
  531. * @throws FileNotFoundException
  532. */
  533. public function testCopyWithFile(): void
  534. {
  535. $src = self::TEMP_TEST_DIR . "/some_dir";
  536. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  537. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  538. mkdir($src);
  539. mkdir($dst);
  540. touch($srcContent);
  541. $path = new Path($srcContent);
  542. $path->copy($dst);
  543. $this->assertTrue(
  544. file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
  545. );
  546. }
  547. /**
  548. * Test 'Path' class 'copy_dir' method to copy a directory
  549. *
  550. * @return void
  551. * @throws FileExistsException
  552. * @throws FileNotFoundException
  553. */
  554. public function testCopyWithDir(): void
  555. {
  556. $src = self::TEMP_TEST_DIR . "/some_dir";
  557. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  558. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  559. mkdir($src);
  560. mkdir($dst);
  561. touch($srcContent);
  562. $path = new Path($src);
  563. $path->copy($dst);
  564. $this->assertTrue(
  565. file_exists($srcContent)
  566. );
  567. }
  568. /**
  569. * Test 'Path' class 'copy' method when the source file does not exist.
  570. *
  571. * @return void
  572. * @throws FileExistsException
  573. * @throws FileNotFoundException
  574. */
  575. public function testCopyFileNotExists(): void
  576. {
  577. $src = self::TEMP_TEST_DIR . "/some_dir";
  578. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  579. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  580. mkdir($src);
  581. mkdir($dst);
  582. touch($srcContent);
  583. $path = new Path($src);
  584. $path->copy($dst);
  585. $this->assertTrue(
  586. file_exists($srcContent)
  587. );
  588. }
  589. /**
  590. * Test 'Path' class 'copy' method when the source file does not exist.
  591. *
  592. * @return void
  593. * @throws FileExistsException
  594. * @throws FileNotFoundException
  595. */
  596. public function testCopyDirDestAlreadyExists(): void
  597. {
  598. $src = self::TEMP_TEST_DIR . "/some_dir";
  599. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  600. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  601. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  602. mkdir($src);
  603. touch($srcContent);
  604. mkdir($dst);
  605. mkdir($dstContent);
  606. $this->expectException(FileExistsException::class);
  607. $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
  608. $path = new Path($srcContent);
  609. $path->copy($dst);
  610. }
  611. /**
  612. * Test 'Path' class 'copy' method when the source file does not exist.
  613. *
  614. * @return void
  615. * @throws FileExistsException
  616. * @throws FileNotFoundException
  617. */
  618. public function testCopyFileDestAlreadyExists(): void
  619. {
  620. $src = self::TEMP_TEST_DIR . "/some_dir";
  621. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  622. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  623. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  624. mkdir($src);
  625. touch($srcContent);
  626. mkdir($dst);
  627. touch($dstContent);
  628. $this->expectException(FileExistsException::class);
  629. $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
  630. $path = new Path($srcContent);
  631. $path->copy($dst);
  632. }
  633. /**
  634. * Test 'Path' class 'move' method to move a file from source directory to destination directory
  635. * @throws FileExistsException
  636. */
  637. public function testMoveWithFile(): 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. mkdir($dst);
  646. $path = new Path($srcContent);
  647. $path->move($dst);
  648. $this->assertFalse(
  649. file_exists($srcContent)
  650. );
  651. $this->assertTrue(
  652. file_exists($dstContent)
  653. );
  654. }
  655. /**
  656. * Test 'Path' class 'move' method to move a directory to a different location
  657. *
  658. * @return void
  659. * @throws FileExistsException
  660. */
  661. public function testMoveWithDir(): void
  662. {
  663. $src = self::TEMP_TEST_DIR . "/some_dir";
  664. $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
  665. $dst = self::TEMP_TEST_DIR . "/some_other_dir";
  666. $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
  667. mkdir($src);
  668. touch($srcContent);
  669. $path = new Path($src);
  670. $path->move($dst);
  671. $this->assertFalse(
  672. file_exists($srcContent)
  673. );
  674. $this->assertTrue(
  675. file_exists($dstContent)
  676. );
  677. }
  678. /**
  679. * Test 'Path' class 'touch' method to create a file that does not exist
  680. *
  681. * @return void
  682. */
  683. public function testTouchFileDoesNotExist()
  684. {
  685. $src = self::TEMP_TEST_DIR . "/foo.txt";
  686. $path = new Path($src);
  687. $this->assertFalse(is_file($src));
  688. $path->touch();
  689. $this->assertTrue(is_file($src));
  690. }
  691. public function testTouchFileExistsNothingChange() {
  692. $src = self::TEMP_TEST_DIR . "/foo.txt";
  693. touch($src);
  694. $this->assertTrue(is_file($src));
  695. $path = new Path($src);
  696. $path->touch();
  697. $this->assertTrue(is_file($src));
  698. }
  699. public function testTouchFileExistsUpdateMtimeWithInt() {
  700. $src = self::TEMP_TEST_DIR . "/foo.txt";
  701. touch($src);
  702. $path = new Path($src);
  703. $timestamp = 1000;
  704. $path->touch($timestamp);
  705. $this->assertEquals(
  706. $timestamp,
  707. filemtime($src)
  708. );
  709. }
  710. public function testTouchFileExistsUpdateMtimeWithDatetime() {
  711. $src = self::TEMP_TEST_DIR . "/foo.txt";
  712. touch($src);
  713. $path = new Path($src);
  714. $dateTime = new \DateTime("2000-01-01");
  715. $path->touch($dateTime);
  716. $this->assertEquals(
  717. $dateTime->getTimestamp(),
  718. filemtime($src)
  719. );
  720. }
  721. public function testTouchFileExistsUpdateAtimeWithInt() {
  722. $src = self::TEMP_TEST_DIR . "/foo.txt";
  723. touch($src);
  724. $path = new Path($src);
  725. $timestamp = 1000;
  726. $path->touch($timestamp, $timestamp);
  727. $this->assertEquals(
  728. $timestamp,
  729. fileatime($src)
  730. );
  731. }
  732. public function testTouchFileExistsUpdateAtimeWithDatetime() {
  733. $src = self::TEMP_TEST_DIR . "/foo.txt";
  734. touch($src);
  735. $path = new Path($src);
  736. $dateTime = new \DateTime("2000-01-01");
  737. $path->touch($dateTime, $dateTime);
  738. $this->assertEquals(
  739. $dateTime->getTimestamp(),
  740. fileatime($src)
  741. );
  742. }
  743. }