Browse Source

add unit tests for name and basename methods

olinox14 1 year ago
parent
commit
a43e447eba
2 changed files with 66 additions and 11 deletions
  1. 8 10
      src/Path.php
  2. 58 1
      tests/PathTest.php

+ 8 - 10
src/Path.php

@@ -182,25 +182,23 @@ class Path
     }
 
     /**
-     * Get the name of the file or path.
+     * Get the base name of the path.
      *
-     * @return array|string Returns the name of the file or path.
-     * If the path has an extension, it returns the name without the extension as a string.
-     * If the path doesn't have an extension, it returns the name as an array containing the directory name and the file name.
+     * @return string The base name of the path.
      */
-    public function name(): array|string
+    public function basename(): string
     {
-        return pathinfo($this->path, PATHINFO_FILENAME);
+        return pathinfo($this->path, PATHINFO_BASENAME);
     }
 
     /**
-     * Get the base name of the path.
+     * Get the name of the file or path.
      *
-     * @return string The base name of the path.
+     * @return string Returns the name of the file without its extension
      */
-    public function basename(): string
+    public function name(): string
     {
-        return pathinfo($this->path, PATHINFO_BASENAME);
+        return pathinfo($this->path, PATHINFO_FILENAME);
     }
 
     /**

+ 58 - 1
tests/PathTest.php

@@ -351,6 +351,11 @@ class PathTest extends TestCase
         $this->assertFalse((new Path('some_dir'))->isDir());
     }
 
+    /**
+     * Test 'Path' class 'ext' method to get the extension of the file
+     *
+     * @return void
+     */
     public function testFileExtension(): void
     {
         $this->assertEquals(
@@ -359,11 +364,63 @@ class PathTest extends TestCase
         );
     }
 
+    /**
+     * Test 'Path' class 'ext' method to get the extension of the file
+     *
+     * @return void
+     */
     public function testEmptyExtension(): void
     {
-        $this->assertSame(
+        $this->assertEquals(
             '',
             (new Path("foo"))->ext()
         );
     }
+
+    /**
+     * Test 'Path' class 'basename' method to get the base name of a file
+     *
+     * @return void
+     */
+    public function testBaseName()
+    {
+        touch(self::TEMP_TEST_DIR . "/foo.txt");
+
+        $this->assertEquals(
+            'foo.txt',
+            (new Path("foo.txt"))->basename()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'name' method to get the name of the file without extension
+     *
+     * @return void
+     */
+    public function testName()
+    {
+        touch(self::TEMP_TEST_DIR . "/foo");
+
+        $this->assertEquals(
+            'foo',
+            (new Path("foo"))->name()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'name' method to get the name of the file with extension
+     *
+     * @return void
+     */
+    public function testNameWithExt()
+    {
+        touch(self::TEMP_TEST_DIR . "/foo.txt");
+
+        $this->assertEquals(
+            'foo',
+            (new Path("foo.txt"))->name()
+        );
+    }
+
+
 }