Browse Source

add tests for touch method

olinox14 1 year ago
parent
commit
415d6fb46a
2 changed files with 96 additions and 5 deletions
  1. 9 5
      src/Path.php
  2. 87 0
      tests/PathTest.php

+ 9 - 5
src/Path.php

@@ -373,16 +373,20 @@ class Path
     /**
      * Updates the access and modification time of a file or creates a new empty file if it doesn't exist.
      *
-     * @param int|null $time (optional) The access and modification time to set. Default is the current time.
-     * @param int|null $atime (optional) The access time to set. Default is the value of $time.
+     * @param int|\DateTime|null $time (optional) The access and modification time to set. Default is the current time.
+     * @param int|\DateTime|null $atime (optional) The access time to set. Default is the value of $time.
      *
      * @return void
      */
-    public function touch($time = null, $atime = null): void
+    public function touch(int|\DateTime $time = null, int|\DateTime $atime = null): void
     {
-        if (!file_exists($this->path)) {
-            touch($this->path, $time, $atime);
+        if ($time instanceof \DateTime) {
+            $time = $time->getTimestamp();
         }
+        if ($atime instanceof \DateTime) {
+            $atime = $atime->getTimestamp();
+        }
+        touch($this->path, $time, $atime);
     }
 
     /**

+ 87 - 0
tests/PathTest.php

@@ -789,4 +789,91 @@ class PathTest extends TestCase
             file_exists($dstContent)
         );
     }
+
+    /**
+     * Test 'Path' class 'touch' method to create a file that does not exist
+     *
+     * @return void
+     */
+    public function testTouchFileDoesNotExist()
+    {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        $path = new Path($src);
+
+        $this->assertFalse(is_file($src));
+        $path->touch();
+        $this->assertTrue(is_file($src));
+    }
+
+    public function testTouchFileExistsNothingChange() {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $this->assertTrue(is_file($src));
+
+        $path = new Path($src);
+        $path->touch();
+
+        $this->assertTrue(is_file($src));
+    }
+
+    public function testTouchFileExistsUpdateMtimeWithInt() {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $path = new Path($src);
+        $timestamp = 1000;
+
+        $path->touch($timestamp);
+
+        $this->assertEquals(
+            $timestamp,
+            filemtime($src)
+        );
+    }
+
+    public function testTouchFileExistsUpdateMtimeWithDatetime() {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $path = new Path($src);
+        $dateTime = new \DateTime("2000-01-01");
+
+        $path->touch($dateTime);
+
+        $this->assertEquals(
+            $dateTime->getTimestamp(),
+            filemtime($src)
+        );
+    }
+
+    public function testTouchFileExistsUpdateAtimeWithInt() {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $path = new Path($src);
+        $timestamp = 1000;
+
+        $path->touch($timestamp, $timestamp);
+
+        $this->assertEquals(
+            $timestamp,
+            fileatime($src)
+        );
+    }
+
+    public function testTouchFileExistsUpdateAtimeWithDatetime() {
+        $src = self::TEMP_TEST_DIR . "/foo.txt";
+        touch($src);
+
+        $path = new Path($src);
+        $dateTime = new \DateTime("2000-01-01");
+
+        $path->touch($dateTime, $dateTime);
+
+        $this->assertEquals(
+            $dateTime->getTimestamp(),
+            fileatime($src)
+        );
+    }
 }