Browse Source

add LastModified, Size, Parent, GetContent, PutContent, AppendContent

olinox14 1 year ago
parent
commit
ef08a57566
2 changed files with 160 additions and 17 deletions
  1. 27 11
      src/Path.php
  2. 133 6
      tests/PathTest.php

+ 27 - 11
src/Path.php

@@ -402,10 +402,14 @@ class Path
     /**
      * Calculates the size of a file.
      *
-     * @return bool|int The size of the file in bytes. Returns false if the file does not exist or on failure.
+     * @return int The size of the file in bytes.
+     * @throws FileNotFoundException
      */
-    public function size(): bool|int
+    public function size(): int
     {
+        if (!$this->isFile()) {
+            throw new FileNotFoundException("File does not exist : " . $this->path);
+        }
         return filesize($this->path);
     }
 
@@ -416,40 +420,52 @@ class Path
      */
     public function parent(): string
     {
+        // TODO: check on special cases
         return dirname($this->path);
     }
 
     /**
-     * Retrieves the contents of a file.
+     * Retrieves the content of a file.
      *
-     * @return bool|string The contents of the file as a string. Returns false if the file does not exist or on failure.
+     * @return bool|string The content of the file as a string.
+     * @throws FileNotFoundException
      */
-    public function getContents(): bool|string
+    public function getContent(): bool|string
     {
+        if (!$this->isFile()) {
+            throw new FileNotFoundException("File does not exist : " . $this->path);
+        }
+        // TODO: review use-cases
         return file_get_contents($this->path);
     }
 
     /**
      * Writes contents to a file.
      *
-     * @param mixed $contents The contents to be written to the file.
+     * @param string $content The contents to be written to the file.
      * @return void
      */
-    public function putContents($contents): void
+    public function putContent(string $content): void
     {
-        file_put_contents($this->path, $contents);
+        // TODO: review use-cases
+        // TODO: complete the input types
+        // TODO: add a condition on the creation of the file if not existing
+        file_put_contents($this->path, $content);
     }
 
     /**
      * Appends contents to a file.
      *
-     * @param string $contents The contents to append to the file.
+     * @param string $content The contents to append to the file.
      *
      * @return void
      */
-    public function appendContents($contents): void
+    public function appendContent(string $content): void
     {
-        file_put_contents($this->path, $contents, FILE_APPEND);
+        // TODO: review use-cases
+        // TODO: complete the input types
+        // TODO: add a condition on the creation of the file if not existing
+        file_put_contents($this->path, $content, FILE_APPEND);
     }
 
     /**

+ 133 - 6
tests/PathTest.php

@@ -795,7 +795,7 @@ class PathTest extends TestCase
      *
      * @return void
      */
-    public function testTouchFileDoesNotExist()
+    public function testTouchFileDoesNotExist(): void
     {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         $path = new Path($src);
@@ -805,7 +805,7 @@ class PathTest extends TestCase
         $this->assertTrue(is_file($src));
     }
 
-    public function testTouchFileExistsNothingChange() {
+    public function testTouchFileExistsNothingChange(): void {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         touch($src);
 
@@ -817,7 +817,7 @@ class PathTest extends TestCase
         $this->assertTrue(is_file($src));
     }
 
-    public function testTouchFileExistsUpdateMtimeWithInt() {
+    public function testTouchFileExistsUpdateMtimeWithInt(): void {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         touch($src);
 
@@ -832,7 +832,7 @@ class PathTest extends TestCase
         );
     }
 
-    public function testTouchFileExistsUpdateMtimeWithDatetime() {
+    public function testTouchFileExistsUpdateMtimeWithDatetime(): void {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         touch($src);
 
@@ -847,7 +847,7 @@ class PathTest extends TestCase
         );
     }
 
-    public function testTouchFileExistsUpdateAtimeWithInt() {
+    public function testTouchFileExistsUpdateAtimeWithInt(): void {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         touch($src);
 
@@ -862,7 +862,7 @@ class PathTest extends TestCase
         );
     }
 
-    public function testTouchFileExistsUpdateAtimeWithDatetime() {
+    public function testTouchFileExistsUpdateAtimeWithDatetime(): void {
         $src = self::TEMP_TEST_DIR . "/foo.txt";
         touch($src);
 
@@ -876,4 +876,131 @@ class PathTest extends TestCase
             fileatime($src)
         );
     }
+
+    public function testLastModified(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        $dateTime = new \DateTime("2000-01-01");
+
+        touch($src, $dateTime->getTimestamp());
+
+        $path = new Path($src);
+
+        $this->assertEquals(
+            $dateTime->getTimestamp(),
+            $path->lastModified()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'size' method to get the size of the file
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testSize(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        file_put_contents($src, "nova");
+
+        $path = new Path($src);
+
+        $this->assertEquals(
+            4,
+            $path->size()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'size' method to get the size of the file
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testSizeNotExistingFile(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage("File does not exist : " . $src);
+
+        $path->size();
+    }
+
+    public function testParent(): void
+    {
+        $this->assertEquals(
+            '/foo/bar',
+            (new Path('/foo/bar/baz'))->parent()
+        );
+        $this->assertEquals(
+            '/foo/bar',
+            (new Path('/foo/bar/baz.txt'))->parent()
+        );
+        $this->assertEquals(
+            '/',
+            (new Path('/foo'))->parent()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'getContent' method to retrieve the content of a file
+     *
+     * @return void
+     * @throws FileNotFoundException
+     */
+    public function testGetContent(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        file_put_contents($src, "nova");
+
+        $path = new Path($src);
+
+        $this->assertEquals(
+            "nova",
+            $path->getContent()
+        );
+    }
+
+    /**
+     * Test 'Path' class 'getContent' method to get the content of a non-existing file
+     *
+     * @throws FileNotFoundException If the file does not exist
+     */
+    public function testGetContentNotExistingFile(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+
+        $this->expectException(FileNotFoundException::class);
+        $this->expectExceptionMessage("File does not exist : " . $src);
+
+        $path->getContent();
+    }
+
+    public function testPutContent(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+
+        $path = new Path($src);
+        $path->putContent("ocarina");
+
+        $this->assertEquals(
+            "ocarina",
+            file_get_contents($src)
+        );
+    }
+
+    public function testAppendContent(): void {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+        file_put_contents($src, "oca");
+
+        $path = new Path($src);
+        $path->appendContent("rina");
+
+        $this->assertEquals(
+            "ocarina",
+            file_get_contents($src)
+        );
+    }
 }