Browse Source

add tests for move method

olinox14 1 year ago
parent
commit
72eedc1553
2 changed files with 91 additions and 4 deletions
  1. 11 3
      src/Path.php
  2. 80 1
      tests/PathTest.php

+ 11 - 3
src/Path.php

@@ -339,8 +339,8 @@ class Path
             if (is_dir($destination)) {
                 $destination = self::join($destination, $this->basename());
             }
-            if (is_file($destination)) {
-                throw new FileExistsException("File already exists : " . $destination);
+            if (file_exists($destination)) {
+                throw new FileExistsException("File or dir already exists : " . $destination);
             }
             copy($this->path, $destination);
         } else if ($this->isDir()) {
@@ -356,10 +356,18 @@ class Path
      * @param string|Path $destination The new location where the file or directory should be moved to.
      *
      * @return void
+     * @throws FileExistsException
      */
     public function move(string|self $destination): void
     {
-        rename($this->path, (string)$destination);
+        $destination = (string)$destination;
+        if (is_dir($destination)) {
+            $destination = self::join($destination, $this->basename());
+        }
+        if (file_exists($destination)) {
+            throw new FileExistsException("File or dir already exists : " . $destination);
+        }
+        rename($this->path, $destination);
     }
 
     /**

+ 80 - 1
tests/PathTest.php

@@ -685,6 +685,32 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'copy' method when the source file does not exist.
+     *
+     * @return void
+     * @throws FileExistsException
+     * @throws FileNotFoundException
+     */
+    public function testCopyDirDestAlreadyExists(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/some_dir";
+        $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
+        $dst = self::TEMP_TEST_DIR . "/some_other_dir";
+        $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
+
+        mkdir($src);
+        touch($srcContent);
+        mkdir($dst);
+        mkdir($dstContent);
+
+        $this->expectException(FileExistsException::class);
+        $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
+
+        $path = new Path($srcContent);
+        $path->copy($dst);
+    }
+
     /**
      * Test 'Path' class 'copy' method when the source file does not exist.
      *
@@ -705,9 +731,62 @@ class PathTest extends TestCase
         touch($dstContent);
 
         $this->expectException(FileExistsException::class);
-        $this->expectExceptionMessage("File already exists : " . $dstContent);
+        $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
 
         $path = new Path($srcContent);
         $path->copy($dst);
     }
+
+    /**
+     * Test 'Path' class 'move' method to move a file from source directory to destination directory
+     * @throws FileExistsException
+     */
+    public function testMoveWithFile(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/some_dir";
+        $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
+        $dst = self::TEMP_TEST_DIR . "/some_other_dir";
+        $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
+
+        mkdir($src);
+        touch($srcContent);
+        mkdir($dst);
+
+        $path = new Path($srcContent);
+        $path->move($dst);
+
+        $this->assertFalse(
+            file_exists($srcContent)
+        );
+        $this->assertTrue(
+            file_exists($dstContent)
+        );
+    }
+
+    /**
+     * Test 'Path' class 'move' method to move a directory to a different location
+     *
+     * @return void
+     * @throws FileExistsException
+     */
+    public function testMoveWithDir(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/some_dir";
+        $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
+        $dst = self::TEMP_TEST_DIR . "/some_other_dir";
+        $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
+
+        mkdir($src);
+        touch($srcContent);
+
+        $path = new Path($src);
+        $path->move($dst);
+
+        $this->assertFalse(
+            file_exists($srcContent)
+        );
+        $this->assertTrue(
+            file_exists($dstContent)
+        );
+    }
 }