Browse Source

complete and test the abspath method

olinox14 1 year ago
parent
commit
8fc6d90443
2 changed files with 67 additions and 5 deletions
  1. 3 4
      src/Path.php
  2. 64 1
      tests/PathTest.php

+ 3 - 4
src/Path.php

@@ -93,14 +93,13 @@ class Path
     }
 
     /**
-     * Returns an absolute version of this path.
+     * Returns an absolute version of the current path.
      *
-     * @param string $path
      * @return string
      */
-    public function abspath(string $path): string
+    public function abspath(): string
     {
-        return realpath($path);
+        return realpath($this->path);
     }
 
     /**

+ 64 - 1
tests/PathTest.php

@@ -7,10 +7,35 @@ use PHPUnit\Framework\TestCase;
 
 class PathTest extends TestCase
 {
+    const TEMP_TEST_DIR = __DIR__ . "/temp";
+
     protected Path $pathClass;
 
-    protected function setUp(): void
+    public function setUp(): void
+    {
+        mkdir(self::TEMP_TEST_DIR);
+    }
+
+    private function rmDirs(string $dir): void {
+        // Remove and replace by a proper tempdir method
+        foreach(scandir($dir) as $file) {
+            if ('.' === $file || '..' === $file) continue;
+            if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
+            else unlink($dir . DIRECTORY_SEPARATOR . $file);
+        }
+        rmdir($dir);
+    }
+
+    public function tearDown(): void
+    {
+        $this->rmDirs(self::TEMP_TEST_DIR);
+        chdir(__DIR__);
+    }
+
+    public function testToString(): void
     {
+        $path = new Path('/foo/bar');
+        $this->assertEquals('/foo/bar', $path->__toString());
     }
 
     /**
@@ -70,6 +95,11 @@ class PathTest extends TestCase
         $this->assertFalse($path->eq(''));
     }
 
+    /**
+     * Test the append method of the Path class.
+     *
+     * @return void
+     */
     public function testAppend(): void
     {
         $path = new Path('/foo');
@@ -93,4 +123,37 @@ class PathTest extends TestCase
             (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
         );
     }
+
+    /**
+     * Test the abspath method of the Path class.
+     *
+     * @return void
+     */
+    public function testAbsPath(): void
+    {
+        touch(self::TEMP_TEST_DIR . "/foo");
+        chdir(self::TEMP_TEST_DIR);
+
+        $this->assertEquals(
+            self::TEMP_TEST_DIR . "/foo",
+            (new Path('foo'))->abspath()
+        );
+    }
+
+    /**
+     * Test the abspath method of the Path class with a relative path.
+     *
+     * @return void
+     */
+    public function testAbsPathWithRelative(): void
+    {
+        mkdir(self::TEMP_TEST_DIR . "/foo");
+        touch(self::TEMP_TEST_DIR . "/bar");
+        chdir(self::TEMP_TEST_DIR . "/foo");
+
+        $this->assertEquals(
+            self::TEMP_TEST_DIR . "/bar",
+            (new Path('../bar'))->abspath()
+        );
+    }
 }