Browse Source

add rmdir tests

olinox14 1 year ago
parent
commit
5d0ca0133f
2 changed files with 111 additions and 17 deletions
  1. 31 17
      src/Path.php
  2. 80 0
      tests/PathTest.php

+ 31 - 17
src/Path.php

@@ -3,12 +3,8 @@
 namespace Path;
 
 use Generator;
-use InvalidArgumentException;
 use Path\Exception\FileExistsException;
 use Path\Exception\FileNotFoundException;
-use Path\Path\RecursiveDirectoryIterator;
-use Path\Path\RecursiveIteratorIterator;
-use function Path\Path\lchmod;
 
 /**
  * Represents a file or directory path.
@@ -138,7 +134,27 @@ class Path
         }
     }
 
+    /** @noinspection SpellCheckingInspection */
+    private static function _rrmdir(string $dir): void
+    {
+        if (!is_dir($dir)) {
+            return;
+        }
 
+        foreach (scandir($dir) as $object) {
+            if ($object == "." || $object == "..") {
+                continue;
+            }
+
+            if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object)) {
+                self::_rrmdir($dir . DIRECTORY_SEPARATOR . $object);
+            }
+            else {
+                unlink($dir . DIRECTORY_SEPARATOR . $object);
+            }
+        }
+        rmdir($dir);
+    }
 
     public function __construct(string $path)
     {
@@ -524,24 +540,22 @@ class Path
         }
     }
 
-    public function rmdir()
+    /**
+     * Removes a directory and its contents recursively.
+     *
+     * @throws FileNotFoundException
+     */
+    public function rmdir(bool $recursive = false): void
     {
         if (!is_dir($this->path)) {
-            throw new \RuntimeException("{$this->path} is not a directory");
+            throw new FileNotFoundException("{$this->path} is not a directory");
         }
 
-        $it = new RecursiveDirectoryIterator($this->path, RecursiveDirectoryIterator::SKIP_DOTS);
-        $files = new RecursiveIteratorIterator($it,
-            RecursiveIteratorIterator::CHILD_FIRST);
-
-        foreach ($files as $file) {
-            if ($file->isDir()) {
-                rmdir($file->getRealPath());
-            } else {
-                unlink($file->getRealPath());
-            }
+        if ($recursive) {
+            self::_rrmdir($this->path);
+        } else {
+            rmdir($this->path);
         }
-        rmdir($this->path);
     }
 
     public function open(string $mode = 'r')

+ 80 - 0
tests/PathTest.php

@@ -1095,6 +1095,11 @@ class PathTest extends TestCase
         $path->setPermissions(777);
     }
 
+    /**
+     * Test 'Path' class 'exists' method to check existence of the file
+     *
+     * @return void
+     */
     public function testExistsExistingFile(): void
     {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
@@ -1107,6 +1112,11 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'exists' method to check existence of the directory
+     *
+     * @return void
+     */
     public function testExistsExistingDir(): void
     {
         $src = self::TEMP_TEST_DIR . "/foo";
@@ -1119,6 +1129,11 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'exists' method to check existence of a non-existing file
+     *
+     * @return void
+     */
     public function testExistsNonExistingFile(): void
     {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
@@ -1130,6 +1145,11 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'glob' method to match and retrieve file names in a directory
+     *
+     * @return void
+     */
     public function testGlob(): void
     {
         $src = self::TEMP_TEST_DIR;
@@ -1151,4 +1171,64 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'rmdir' method to remove a directory and its contents
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testRmDir(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo";
+        mkdir($src);
+
+        $path = new Path($src);
+        $path->rmdir();
+
+        $this->assertFalse(
+            is_dir($src)
+        );
+    }
+
+    public function testRmDirIsFile(): void {
+        $src = self::TEMP_TEST_DIR . "/foo";
+        touch($src);
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage($src . " is not a directory");
+
+        $path->rmdir();
+    }
+
+    public function testRmDirNonExistingDir(): void {
+        $src = self::TEMP_TEST_DIR . "/foo";
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage($src . " is not a directory");
+
+        $path->rmdir();
+    }
+
+    /**
+     * Test 'Path' class 'rmdir' method to remove a directory and its contents recursively
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testRmDirRecursive(): void {
+        $src = self::TEMP_TEST_DIR . "/foo/bar";
+        mkdir($src, 0777, true);
+        touch($src . "/file.txt");
+
+        $path = new Path($src);
+        $path->rmdir(true);
+
+        $this->assertFalse(
+            is_dir($src)
+        );
+    }
 }