Skip to content

Commit

Permalink
complete unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olinox14 committed Apr 17, 2024
1 parent 54c2b14 commit 4292578
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 32 deletions.
7 changes: 7 additions & 0 deletions src/BuiltinProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class BuiltinProxy
{
public static string $DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR;

public function getHome(): string
{
return PHP_OS_FAMILY == 'Windows' ?
$_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'] :
$_SERVER['HOME'];
}

public function date(string $format, int $time): string
{
return date($format, $time);
Expand Down
2 changes: 1 addition & 1 deletion src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ public function expandUser(): self
if (!str_starts_with($this->path(), '~/')) {
return $this;
}
$home = PHP_OS_FAMILY == 'Windows' ? $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'] : $_SERVER['HOME'];
$home = $this->builtin->getHome();

if (!$home) {
throw new IOException("Error while getting home directory");
Expand Down
213 changes: 182 additions & 31 deletions tests/unit/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PathTest extends TestCase

public function setUp(): void
{
BuiltinProxy::$DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR;
BuiltinProxy::$DIRECTORY_SEPARATOR = '/';
$this->builtin = $this->getMockBuilder(BuiltinProxy::class)->getMock();
}

Expand Down Expand Up @@ -627,127 +627,233 @@ public function testNormPath(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('/foo/bar');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('/foo/bar')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'/foo/bar',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathWithBackSlashes(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('\\foo\\bar');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('/foo/bar')
->willReturn($newPath);

$this->assertEquals(
$newPath,
$path->normPath()
);
}

public function testNormPathWithEmptyPath(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('.')
->willReturn($newPath);

$this->assertEquals(
$newPath,
$path->normPath()
);
}

public function testNormPathNoSep(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('foo');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('foo')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'foo',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathIsRoot(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('/');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('/')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'/',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathIsCurrent(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('..');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('..')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'..',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathIsParent(): void {
public function testNormPathWithTrailingSep(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('.');
$path->method('path')->willReturn('/foo/bar/');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('.')
->willReturn($path);
->with('/foo/bar')
->willReturn($newPath);

$this->assertEquals(
'.',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathWithDrive(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('c:\\foo\\..\\bar');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('c:/bar')
->willReturn($newPath);

$this->assertEquals(
$newPath,
$path->normPath()
);
}

public function testNormPathWithUNC(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('//network/computer/home/../var');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('//network/computer/var')
->willReturn($newPath);

$this->assertEquals(
$newPath,
$path->normPath()
);
}

public function testNormPathIsEmpty(): void {
public function testNormPathIsParent(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('.');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('.')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'.',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathWithParentPart(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('/bar');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('/bar')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
$newPath,
$path->normPath()
);
}

public function testNormPathWithRelativeParts(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('/bar/dir1/./dir2/../file.ext');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('/bar/dir1/file.ext')
->willReturn($newPath);

$this->assertEquals(
'/bar',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

public function testNormPathWithLeadingParentPart(): void {
$path = $this->getMock('', 'normPath');
$path->method('path')->willReturn('../bar');

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$path
->expects(self::once())
->method('cast')
->with('../bar')
->willReturn($path);
->willReturn($newPath);

$this->assertEquals(
'../bar',
$path->normPath()->path()
$newPath,
$path->normPath()
);
}

Expand Down Expand Up @@ -2836,15 +2942,21 @@ public function testExpand(): void
);
}

/**
* @throws IOException
*/
public function testExpandUser(): void
{
$path = $this->getMock('~/file.ext', 'expandUser');
$path->method('path')->willReturn('~/file.ext');

$expandedPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
$this->builtin->expects(self::once())->method('getHome')->willReturn('/home/foo');

$homePath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
$path->method('cast')->with($_SERVER['HOME'])->willReturn($homePath);

$path->method('cast')->with('/home/foo')->willReturn($homePath);

$expandedPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();

$homePath
->expects(self::once())
Expand All @@ -2869,6 +2981,21 @@ public function testExpandUserNothingToExpand(): void
);
}

/**
* @throws IOException
*/
public function testExpandUserNoHome(): void
{
$path = $this->getMock('~/file.ext', 'expandUser');
$path->method('path')->willReturn('~/file.ext');

$this->builtin->expects(self::once())->method('getHome')->willReturn('');

$this->expectException(IOException::class);

$path->expandUser();
}

public function testExpandVars(): void
{
$path = $this->getMock('~/file.ext', 'expandVars');
Expand Down Expand Up @@ -4604,6 +4731,30 @@ public function testPartsWithLeadingSlash(): void
);
}

public function testPartsWithDrive(): void
{
BuiltinProxy::$DIRECTORY_SEPARATOR = '\\';

$path = $this->getMock('', 'parts');
$path->method('path')->willReturn('c:\\bar\\file.ext');

$this->assertEquals(
['c:', 'bar', 'file.ext'],
$path->parts()
);
}

public function testPartsWithUNC(): void
{
$path = $this->getMock('', 'parts');
$path->method('path')->willReturn('//network/computer/home/dir');

$this->assertEquals(
['//network/computer', 'home', 'dir'],
$path->parts()
);
}

/**
* @throws IOException
* @throws FileNotFoundException
Expand Down

0 comments on commit 4292578

Please sign in to comment.