Browse Source

add unit tests for ext method

olinox14 1 year ago
parent
commit
e464661636
2 changed files with 48 additions and 2 deletions
  1. 2 2
      src/Path.php
  2. 46 0
      tests/PathTest.php

+ 2 - 2
src/Path.php

@@ -174,9 +174,9 @@ class Path
     /**
      * Get the extension of the given path.
      *
-     * @return array|string Returns the extension of the path as a string if it exists, or an empty string otherwise.
+     * @return string Returns the extension of the path as a string if it exists, or an empty string otherwise.
      */
-    public function extension(): array|string
+    public function ext(): string
     {
         return pathinfo($this->path, PATHINFO_EXTENSION);
     }

+ 46 - 0
tests/PathTest.php

@@ -283,6 +283,11 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'isFile' method to check if the file exists
+     *
+     * @return void
+     */
     public function testIsFileOnActualFile(): void
     {
         touch(self::TEMP_TEST_DIR . "/foo");
@@ -290,11 +295,21 @@ class PathTest extends TestCase
         $this->assertTrue((new Path('foo'))->isFile());
     }
 
+    /**
+     * Test 'Path' class 'isFile' method to check if a non-existent file exists
+     *
+     * @return void
+     */
     public function testIsFileOnNonExistentFile(): void
     {
         $this->assertFalse((new Path('foo'))->isFile());
     }
 
+    /**
+     * Test 'Path' class 'isFile' method to check if a file exists on the given directory
+     *
+     * @return void
+     */
     public function testIsFileOnExistentDir(): void
     {
         mkdir(self::TEMP_TEST_DIR . "/some_dir");
@@ -302,6 +317,11 @@ class PathTest extends TestCase
         $this->assertFalse((new Path('some_dir'))->isFile());
     }
 
+    /**
+     * Test 'Path' class 'isDir' method to check if the path is a directory
+     *
+     * @return void
+     */
     public function testIsDirOnActualDir(): void
     {
         mkdir(self::TEMP_TEST_DIR . "/some_dir");
@@ -309,6 +329,11 @@ class PathTest extends TestCase
         $this->assertTrue((new Path('some_dir'))->isDir());
     }
 
+    /**
+     * Test 'Path' class 'isDir' method to check if a file's path is a directory
+     *
+     * @return void
+     */
     public function testIsDirOnExistentFile(): void
     {
         touch(self::TEMP_TEST_DIR . "/foo");
@@ -316,8 +341,29 @@ class PathTest extends TestCase
         $this->assertFalse((new Path('some_dir'))->isDir());
     }
 
+    /**
+     * Test 'Path' class 'isDir' method on a non-existent directory
+     *
+     * @return void
+     */
     public function testIsDirOnNonExistentDir(): void
     {
         $this->assertFalse((new Path('some_dir'))->isDir());
     }
+
+    public function testFileExtension(): void
+    {
+        $this->assertEquals(
+            'txt',
+            (new Path("foo.txt"))->ext()
+        );
+    }
+
+    public function testEmptyExtension(): void
+    {
+        $this->assertSame(
+            '',
+            (new Path("foo"))->ext()
+        );
+    }
 }