Test trivial relatives

master
Anton Smirnov 11 months ago
parent cfec611306
commit 6ac37ac667
  1. 2
      src/RelativePath.php
  2. 36
      tests/RelativePathTest.php
  3. 52
      tests/UnixPathTest.php

@ -64,7 +64,7 @@ final class RelativePath extends AbstractPath implements RelativePathInterface
$directorySeparator = $this->windows ? '\\' : '/';
$components = $this->components;
if ($components[0] === '.') {
if ($components[0] === '.' && $components->count() > 1) {
$components = clone $components;
$components->shift();
}

@ -24,6 +24,12 @@ class RelativePathTest extends TestCase
// relative path from the parent directory
$path = RelativePath::unix('.././../i/am/./skipme/../test/./relative/path');
self::assertEquals('../../i/am/test/relative/path', $path->toString());
$path = RelativePath::unix('..');
self::assertEquals('..', $path->toString());
$path = RelativePath::unix('.');
self::assertEquals('.', $path->toString());
}
public function testCreateWindows(): void
@ -48,6 +54,8 @@ class RelativePathTest extends TestCase
new RelativePath('i/am/test/relative/path'),
new RelativePath('../../i/am/test/relative/path'),
new RelativePath('../../../../../../../../i/am/test/relative/path'),
new RelativePath('..'),
new RelativePath('.'),
];
$relativePaths = $paths;
@ -57,24 +65,48 @@ class RelativePathTest extends TestCase
'/i/am/test/relative/path/i/am/test/relative/path',
'/i/am/test/i/am/test/relative/path',
'/i/am/test/relative/path',
'/i/am/test/relative',
'/i/am/test/relative/path',
],
[
'/i/am/test/relative/path',
'i/am/test/relative/path/i/am/test/relative/path',
'i/am/test/i/am/test/relative/path',
'../../../i/am/test/relative/path',
'i/am/test/relative',
'i/am/test/relative/path',
],
[
'/i/am/test/relative/path',
'../../i/am/test/relative/path/i/am/test/relative/path',
'../../i/am/test/i/am/test/relative/path',
'../../../../../i/am/test/relative/path',
'../../i/am/test/relative',
'../../i/am/test/relative/path'
],
[
'/i/am/test/relative/path',
'../../../../../../../../i/am/test/relative/path/i/am/test/relative/path',
'../../../../../../../../i/am/test/i/am/test/relative/path',
'../../../../../../../../../../../i/am/test/relative/path',
'../../../../../../../../i/am/test/relative',
'../../../../../../../../i/am/test/relative/path',
],
[
'/i/am/test/relative/path',
'../i/am/test/relative/path',
'../../../i/am/test/relative/path',
'../../../../../../../../../i/am/test/relative/path',
'../..',
'..',
],
[
'/i/am/test/relative/path',
'i/am/test/relative/path',
'../../i/am/test/relative/path',
'../../../../../../../../i/am/test/relative/path',
'..',
'.',
],
];
@ -172,7 +204,7 @@ class RelativePathTest extends TestCase
throw new \BadMethodCallException('Irrelevant');
}
public function resolveRelative($path, bool $strict = false): PathInterface
public function resolveRelative(RelativePathInterface $path, bool $strict = false): PathInterface
{
throw new \BadMethodCallException('Irrelevant');
}
@ -204,7 +236,7 @@ class RelativePathTest extends TestCase
throw new \BadMethodCallException('Irrelevant');
}
public function resolveRelative($path, bool $strict = false): PathInterface
public function resolveRelative(RelativePathInterface $path, bool $strict = false): PathInterface
{
throw new \BadMethodCallException('Irrelevant');
}

@ -42,28 +42,40 @@ class UnixPathTest extends TestCase
{
$path = UnixPath::parse('/i/am/test/unix/path');
$rp1 = new RelativePath('/i/am/test/relative/path');
$rp = new RelativePath('/i/am/test/relative/path');
self::assertEquals(
'/i/am/test/relative/path',
$path->resolveRelative($rp1)->toString()
$path->resolveRelative($rp)->toString()
);
$rp2 = new RelativePath('i/am/test/relative/path');
$rp = new RelativePath('i/am/test/relative/path');
self::assertEquals(
'/i/am/test/unix/path/i/am/test/relative/path',
$path->resolveRelative($rp2)->toString()
$path->resolveRelative($rp)->toString()
);
$rp3 = new RelativePath('../../i/am/test/relative/path');
$rp = new RelativePath('../../i/am/test/relative/path');
self::assertEquals(
'/i/am/test/i/am/test/relative/path',
$path->resolveRelative($rp3)->toString()
$path->resolveRelative($rp)->toString()
);
$rp4 = new RelativePath('../../../../../../../../i/am/test/relative/path');
$rp = new RelativePath('../../../../../../../../i/am/test/relative/path');
self::assertEquals(
'/i/am/test/relative/path',
$path->resolveRelative($rp4)->toString()
$path->resolveRelative($rp)->toString()
);
$rp = new RelativePath('..');
self::assertEquals(
'/i/am/test/unix',
$path->resolveRelative($rp)->toString()
);
$rp = new RelativePath('.');
self::assertEquals(
'/i/am/test/unix/path',
$path->resolveRelative($rp)->toString()
);
}
@ -71,22 +83,34 @@ class UnixPathTest extends TestCase
{
$path = UnixPath::parse('/i/am/test/unix/path');
$rp1 = new RelativePath('/i/am/test/relative/path');
$rp = new RelativePath('/i/am/test/relative/path');
self::assertEquals(
'/i/am/test/relative/path',
$path->resolveRelative($rp1, true)->toString()
$path->resolveRelative($rp, true)->toString()
);
$rp2 = new RelativePath('i/am/test/relative/path');
$rp = new RelativePath('i/am/test/relative/path');
self::assertEquals(
'/i/am/test/unix/path/i/am/test/relative/path',
$path->resolveRelative($rp2, true)->toString()
$path->resolveRelative($rp, true)->toString()
);
$rp3 = new RelativePath('../../i/am/test/relative/path');
$rp = new RelativePath('../../i/am/test/relative/path');
self::assertEquals(
'/i/am/test/i/am/test/relative/path',
$path->resolveRelative($rp3, true)->toString()
$path->resolveRelative($rp, true)->toString()
);
$rp = new RelativePath('..');
self::assertEquals(
'/i/am/test/unix',
$path->resolveRelative($rp)->toString()
);
$rp = new RelativePath('.');
self::assertEquals(
'/i/am/test/unix/path',
$path->resolveRelative($rp)->toString()
);
}

Loading…
Cancel
Save