Browse Source

complete the access method and add tests

olinox14 1 year ago
parent
commit
6dd2e37fc0
2 changed files with 133 additions and 11 deletions
  1. 23 11
      src/Path.php
  2. 110 0
      tests/PathTest.php

+ 23 - 11
src/Path.php

@@ -14,14 +14,25 @@ use function Path\Path\lchmod;
  */
 class Path
 {
-    const F_OK = 0; // Check existence of the file
-    const R_OK = 4; // Check read permission of the file
-    const W_OK = 2; // Check write permission of the file
-    const X_OK = 1; // Check execute permission of the file
+    /**
+     * File exists
+     */
+    const F_OK = 0;
+    /**
+     * Has read permission on the file
+     */
+    const R_OK = 4;
+    /**
+     * Has write permission on the file
+     */
+    const W_OK = 2;
+    /**
+     * Has execute permission on the file
+     */
+    const X_OK = 1;
 
     protected string $path;
 
-
     /**
      * Joins two or more parts of a path together, inserting '/' as needed.
      * If any component is an absolute path, all previous path components
@@ -104,6 +115,7 @@ class Path
 
     /**
      * Checks the access rights for a given file or directory.
+     * From the python `os.access` method
      *
      * @param string $path The path to the file or directory.
      * @param int $mode The access mode to check. Permitted values:
@@ -114,14 +126,14 @@ class Path
      * @return bool Returns true if the permission check is successful; otherwise, returns false.
      * @throws InvalidArgumentException Throws an exception if an invalid mode is provided.
      */
-    function access(string $path, int $mode): bool
+    function access(int $mode): bool
     {
         return match ($mode) {
-            self::F_OK => file_exists($path),
-            self::R_OK => is_readable($path),
-            self::W_OK => is_writable($path),
-            self::X_OK => is_executable($path),
-            default => throw new InvalidArgumentException('Invalid mode'),
+            self::F_OK => file_exists($this->path),
+            self::R_OK => is_readable($this->path),
+            self::W_OK => is_writable($this->path),
+            self::X_OK => is_executable($this->path),
+            default => throw new \RuntimeException('Invalid mode'),
         };
     }
 

+ 110 - 0
tests/PathTest.php

@@ -14,6 +14,7 @@ class PathTest extends TestCase
     public function setUp(): void
     {
         mkdir(self::TEMP_TEST_DIR);
+        chdir(self::TEMP_TEST_DIR);
     }
 
     private function rmDirs(string $dir): void {
@@ -156,4 +157,113 @@ class PathTest extends TestCase
             (new Path('../bar'))->abspath()
         );
     }
+
+    /**
+     * Test 'Path' class 'access' method to check existence of the file
+     */
+    public function testAccessCheckExistenceOfFile(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 777);
+
+        $result = (new Path('foo'))->access(Path::F_OK);
+        $this->assertTrue($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check existence of the non-existent file
+     */
+    public function testAccessCheckExistenceOfNonExistingFile(): void
+    {
+        $result = (new Path('foo'))->access(Path::F_OK);
+        $this->assertFalse($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check read permission of the file
+     */
+    public function testAccessCheckReadPermissionOfFile(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 777);
+
+        $result = (new Path('foo'))->access(Path::R_OK);
+        $this->assertTrue($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check read permission of the file (no permission)
+     */
+    public function testAccessCheckReadPermissionOfFileNoRight(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 000);
+
+        $result = (new Path('foo'))->access(Path::R_OK);
+        $this->assertFalse($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check write permission of the file
+     */
+    public function testAccessCheckWritePermissionOfFile(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 777);
+
+        $result = (new Path('foo'))->access(Path::W_OK);
+        $this->assertTrue($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check write permission of the file (no permission)
+     */
+    public function testAccessCheckWritePermissionOfFileNoRight(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 000);
+
+        $result = (new Path('foo'))->access(Path::W_OK);
+        $this->assertFalse($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check execute permission of the file
+     */
+    public function testAccessCheckExecutePermissionOfFile(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 777);
+
+        $result = (new Path('foo'))->access(Path::X_OK);
+        $this->assertTrue($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method to check existence of the file
+     */
+    public function testAccessCheckExecutePermissionOfFileNoRight(): void
+    {
+        $filePath = self::TEMP_TEST_DIR . "/foo";
+        touch($filePath);
+        chmod($filePath, 000);
+
+        $result = (new Path('foo'))->access(Path::X_OK);
+        $this->assertFalse($result);
+    }
+
+    /**
+     * Test 'Path' class 'access' method with an invalid mode parameter
+     */
+    public function testAccessInvalidModeParameter(): void
+    {
+        $this->expectException(\RuntimeException::class);
+        (new Path('foo'))->access(123);
+    }
 }