Browse Source

complete unit tests

olinox14 1 year ago
parent
commit
b2d5ad7a34
3 changed files with 248 additions and 4 deletions
  1. 1 0
      TODO.txt
  2. 10 4
      src/Path.php
  3. 237 0
      tests/unit/PathTest.php

+ 1 - 0
TODO.txt

@@ -1,4 +1,5 @@
 - Finir les tests unitaires
+- revoir les conditions isFile et isDir (il se peut qu'il faille remplacer par exists de ci de là)
 - ajouter une couverture de test
 - Faire un test en live sur linux
 - Déployer sur github

+ 10 - 4
src/Path.php

@@ -1148,11 +1148,16 @@ class Path
     /**
      * Like stat(), but do not follow symbolic links.
      *
-     * @return array|false
+     * @return array
+     * @throws IOException
      */
-    public function lstat(): bool|array
+    public function lstat(): array
     {
-        return $this->builtin->lstat($this->path);
+        $result = $this->builtin->lstat($this->path);
+        if ($result === false) {
+            throw new IOException("Error while getting lstat of " . $this->path);
+        }
+        return $result;
     }
 
     public function splitDrive()
@@ -1201,6 +1206,7 @@ class Path
      * @param string|Path $basePath
      * @return string
      * @throws FileNotFoundException
+     * @throws IOException
      */
     public function getRelativePath(string|self $basePath): string
     {
@@ -1208,7 +1214,7 @@ class Path
             throw new FileNotFoundException("{$this->path} is not a file or directory");
         }
 
-        $path = $this->absPath();
+        $path = (string)$this->absPath();
         $basePath = (string)$basePath;
 
         $realBasePath = $this->builtin->realpath($basePath);

+ 237 - 0
tests/unit/PathTest.php

@@ -2490,6 +2490,8 @@ class PathTest extends TestCase
         $target->method('isFile')->willReturn(False);
         $target->method('__toString')->willReturn('/bar/link.ext');
 
+        $path->method('cast')->with($target)->willReturn($target);
+
         $this->builtin
                 ->expects(self::once())
                 ->method('link')
@@ -2498,4 +2500,239 @@ class PathTest extends TestCase
 
         $path->link($target);
     }
+
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws FileExistsException
+     */
+    public function testLinkWithStringTarget(): void
+    {
+        $path = $this->getMock('/foo/file.ext', 'link');
+        $path->method('isFile')->willReturn(True);
+
+        $target = $this
+            ->getMockBuilder(TestablePath::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $target->method('isFile')->willReturn(False);
+        $target->method('__toString')->willReturn('/bar/link.ext');
+
+        $path->method('cast')->with('/bar/link.ext')->willReturn($target);
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('link')
+            ->with('/foo/file.ext', '/bar/link.ext')
+            ->willReturn(True);
+
+        $path->link($target);
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws FileExistsException
+     */
+    public function testLinkFileDoesNotExist(): void
+    {
+        $path = $this->getMock('/foo/file.ext', 'link');
+        $path->method('isFile')->willReturn(False);
+
+        $this->builtin
+                ->expects(self::never())
+                ->method('link');
+
+        $this->expectException(FileNotFoundException::class);
+
+        $path->link('/bar/link.ext');
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws FileExistsException
+     */
+    public function testLinkTargetExists(): void
+    {
+        $path = $this->getMock('/foo/file.ext', 'link');
+        $path->method('isFile')->willReturn(True);
+
+        $target = $this
+            ->getMockBuilder(TestablePath::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $target->method('isFile')->willReturn(True);
+
+        $path->method('cast')->with($target)->willReturn($target);
+
+        $this->builtin
+            ->expects(self::never())
+            ->method('link');
+
+        $this->expectException(FileExistsException::class);
+
+        $path->link($target);
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws FileExistsException
+     */
+    public function testLinkWithError(): void
+    {
+        $path = $this->getMock('/foo/file.ext', 'link');
+        $path->method('isFile')->willReturn(True);
+
+        $target = $this
+            ->getMockBuilder(TestablePath::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $target->method('isFile')->willReturn(False);
+        $target->method('__toString')->willReturn('/bar/link.ext');
+
+        $path->method('cast')->with($target)->willReturn($target);
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('link')
+            ->with('/foo/file.ext', '/bar/link.ext')
+            ->willReturn(False);
+
+        $this->expectException(IOException::class);
+
+        $path->link($target);
+    }
+
+    /**
+     * @throws IOException
+     */
+    public function testLStat(): void
+    {
+        $path = $this->getMock('foo/file.ext', 'lstat');
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('lstat')
+            ->willReturn(['a', 'b', 'c']);
+
+        $this->assertEquals(
+            ['a', 'b', 'c'],
+            $path->lstat()
+        );
+    }
+
+    /**
+     * @throws IOException
+     */
+    public function testLStatWithError(): void
+    {
+        $path = $this->getMock('foo/file.ext', 'lstat');
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('lstat')
+            ->willReturn(False);
+
+        $this->expectException(IOException::class);
+
+        $path->lstat();
+    }
+
+    public function testParts(): void
+    {
+        $path = $this->getMock('foo/bar/file.ext', 'parts');
+
+        $this->assertEquals(
+            ['foo', 'bar', 'file.ext'],
+            $path->parts()
+        );
+    }
+
+    public function testPartsWithLeadingSlash(): void
+    {
+        $path = $this->getMock('/foo/bar/file.ext', 'parts');
+
+        $this->assertEquals(
+            ['/', 'foo', 'bar', 'file.ext'],
+            $path->parts()
+        );
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    public function testGetRelativePath(): void
+    {
+        $path = $this->getMock('./bar/file.ext', 'getRelativePath');
+        $path->method('exists')->willReturn(True);
+
+        $absPath = $this
+            ->getMockBuilder(TestablePath::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $absPath->method('__toString')->willReturn('/foo/bar/file.ext');
+
+        $path->method('absPath')->willReturn($absPath);
+
+        $basePath = '/other/path';
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('realpath')
+            ->with($basePath)
+            ->willReturn('/other/path');
+
+        $this->assertEquals(
+            '../../foo/bar/file.ext',
+            $path->getRelativePath($basePath)
+        );
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    public function testGetRelativePathFileDoesNotExist(): void
+    {
+        $path = $this->getMock('./bar/file.ext', 'getRelativePath');
+        $path->method('exists')->willReturn(False);
+
+        $this->expectException(FileNotFoundException::class);
+
+        $path->getRelativePath('/other/path');
+    }
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    public function testGetRelativePathTargetDoesNotExist(): void
+    {
+        $path = $this->getMock('./bar/file.ext', 'getRelativePath');
+        $path->method('exists')->willReturn(True);
+
+        $absPath = $this
+            ->getMockBuilder(TestablePath::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $absPath->method('__toString')->willReturn('/foo/bar/file.ext');
+
+        $path->method('absPath')->willReturn($absPath);
+
+        $basePath = '/other/path';
+
+        $this->builtin
+            ->expects(self::once())
+            ->method('realpath')
+            ->with($basePath)
+            ->willReturn(False);
+
+        $this->expectException(FileNotFoundException::class);
+
+        $path->getRelativePath($basePath);
+    }
 }