Browse Source

add getPermissions, setPermissions and glob tests

olinox14 1 year ago
parent
commit
8da7bb8d6d
2 changed files with 171 additions and 5 deletions
  1. 23 5
      src/Path.php
  2. 148 0
      tests/PathTest.php

+ 23 - 5
src/Path.php

@@ -2,6 +2,7 @@
 
 namespace Path;
 
+use Generator;
 use InvalidArgumentException;
 use Path\Exception\FileExistsException;
 use Path\Exception\FileNotFoundException;
@@ -471,21 +472,32 @@ class Path
     /**
      * Retrieves the permissions of a file or directory.
      *
-     * @return string The permissions of the file or directory in octal notation. Returns an empty string if the file or directory does not exist.
+     * @return int The permissions of the file or directory in octal notation.
+     * @throws FileNotFoundException
      */
-    public function getPermissions(): string
+    public function getPermissions(): int
     {
-        return substr(sprintf('%o', fileperms($this->path)), -4);
+        if (!$this->isFile()) {
+            throw new FileNotFoundException("File or dir does not exist : " . $this->path);
+        }
+        return (int)substr(sprintf('%o', fileperms($this->path)), -4);
     }
 
+    // TODO; add some more user-friendly methods to get permissions (read, write, exec...)
+
     /**
      * Changes the permissions of a file or directory.
      *
      * @param int $permissions The new permissions to set. The value should be an octal number.
      * @return bool Returns true on success, false on failure.
+     * @throws FileNotFoundException
      */
-    public function changePermissions($permissions): bool
+    public function setPermissions(int $permissions): bool
     {
+        if (!$this->isFile()) {
+            throw new FileNotFoundException("File or dir does not exist : " . $this->path);
+        }
+        clearstatcache(); // TODO: check for a better way of dealing with PHP cache
         return chmod($this->path, $permissions);
     }
 
@@ -499,7 +511,13 @@ class Path
         return file_exists($this->path);
     }
 
-    public static function glob(string $pattern)
+    /**
+     * Retrieves a list of files and directories that match a specified pattern.
+     *
+     * @param string $pattern The pattern to search for.
+     * @return Generator An iterable list of objects representing files and directories that match the pattern.
+     */
+    public static function glob(string $pattern): Generator
     {
         foreach (glob($pattern) as $filename) {
             yield new static($filename);

+ 148 - 0
tests/PathTest.php

@@ -1003,4 +1003,152 @@ class PathTest extends TestCase
             file_get_contents($src)
         );
     }
+
+    /**
+     * Test 'Path' class 'getPermissions' method to retrieve the file permissions
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testGetPermissions(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+        chmod($src, 0777);
+
+        $path = new Path($src);
+        $this->assertEquals(
+            777,
+            $path->getPermissions()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'getPermissions' method to retrieve file permissions
+     * @throws FileNotFoundException
+     */
+    public function testGetPermissionsAlt(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+        chmod($src, 0755);
+
+        $path = new Path($src);
+
+        $this->assertEquals(
+            755,
+            $path->getPermissions()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'getPermissions' method to retrieve file permissions
+     * @throws FileNotFoundException
+     */
+    public function testGetPermissionsFileNotExists(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage("File or dir does not exist : " . $src);
+
+        $path->getPermissions();
+    }
+
+    /**
+     * Test 'Path' class 'setPermissions' method to change the permissions of a file
+     * @throws FileNotFoundException
+     */
+    public function testSetPermissions(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+        chmod($src, 0777);
+
+        $path = new Path($src);
+        $result = $path->setPermissions(0666);
+
+        $this->assertTrue($result);
+
+        $this->assertEquals(
+            '0666',
+            substr(sprintf('%o', fileperms($src)), -4)
+        );
+    }
+
+    /**
+     * Test 'Path' class 'setPermissions' method when file does not exist
+     *
+     * @throws FileNotFoundException If the file does not exist
+     */
+    public function testSetPermissionsFileNotExists(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage("File or dir does not exist : " . $src);
+
+        $path->setPermissions(777);
+    }
+
+    public function testExistsExistingFile(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $path = new Path($src);
+
+        $this->assertTrue(
+            $path->exists()
+        );
+    }
+
+    public function testExistsExistingDir(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo";
+        mkdir($src);
+
+        $path = new Path($src);
+
+        $this->assertTrue(
+            $path->exists()
+        );
+    }
+
+    public function testExistsNonExistingFile(): void
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+
+        $this->assertFalse(
+            $path->exists()
+        );
+    }
+
+    public function testGlob(): void
+    {
+        $src = self::TEMP_TEST_DIR;
+        touch($src . "/foo.txt");
+        touch($src . "/bar.txt");
+        touch($src . "/pic.png");
+
+        $path = new Path($src);
+
+        $results = [];
+
+        foreach ($path->glob('*.txt') as $filename) {
+            $results[] = (string)$filename;
+        }
+
+        $this->assertEquals(
+            ['bar.txt', 'foo.txt'],
+            $results
+        );
+    }
+
 }