Skip to content

Commit

Permalink
Pass directory separator as argument to Path.toString(), so both arch…
Browse files Browse the repository at this point in the history
…itectures can be tested on either of them
  • Loading branch information
danon committed Jul 26, 2022
1 parent e3eb851 commit 7bb2741
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions includes/Support/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function append(string $child): Path
return new Path([...$this->children, $child]);
}

public function toString(): string
public function toString(string $pathSeparator): string
{
return \join(\DIRECTORY_SEPARATOR, \iterator_to_array($this->children()));
return \join($pathSeparator, \iterator_to_array($this->children()));
}

private function children(): Iterator
Expand Down
16 changes: 16 additions & 0 deletions tests/Psr4/Concerns/SystemConcern.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
<?php
namespace Tests\Psr4\Concerns;

use App\Support\Path;
use PHPUnit\Framework\Assert;

trait SystemConcern
{
public function assertPathWindows(string $expected, Path $actual): void
{
$this->assertPath($expected, $actual, "\\");
}

public function assertPathUnix(string $expected, Path $actual): void
{
$this->assertPath($expected, $actual, "/");
}

private function assertPath(string $expected, Path $actual, string $separator): void
{
Assert::assertSame($expected, $actual->toString($separator));
}

public function assertSameWindows($expected, $actual): void
{
if (!$this->isUnix()) {
Expand Down
58 changes: 23 additions & 35 deletions tests/Unit/Support/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public function shouldGetEmptyPath()
{
// given
$path = new Path([]);
// when
$asString = $path->toString();
// then
$this->assertSame("", $asString);
// when, then
$this->assertPathUnix("", $path);
}

/**
Expand All @@ -30,11 +28,9 @@ public function shouldIgnoreEmptyChildren()
{
// given
$path = new Path(["string", "", "", "string"]);
// when
$asString = $path->toString();
// then
$this->assertSameWindows("string\string", $asString);
$this->assertSameUnix("string/string", $asString);
// when, then
$this->assertPathWindows("string\string", $path);
$this->assertPathUnix("string/string", $path);
}

/**
Expand All @@ -45,10 +41,9 @@ public function shouldGetSingleFile(string $filename)
{
// given
$path = new Path([$filename]);
// when
$asString = $path->toString();
// then
$this->assertSame("file.txt", $asString);
// when, then
$this->assertPathWindows("file.txt", $path);
$this->assertPathUnix("file.txt", $path);
}

public function fileNames(): array
Expand All @@ -64,11 +59,9 @@ public function shouldGetManyFiles(array $pathPieces)
{
// given
$path = new Path($pathPieces);
// when
$asString = $path->toString();
// then
$this->assertSameWindows('first\second\third\file.txt', $asString);
$this->assertSameUnix("first/second/third/file.txt", $asString);
// when, then
$this->assertPathWindows('first\second\third\file.txt', $path);
$this->assertPathUnix("first/second/third/file.txt", $path);
}

public function pathPieces(): array
Expand All @@ -95,11 +88,9 @@ public function shouldRepresentPath(string $stringPath)
{
// given
$path = Path::of($stringPath);
// when
$asString = $path->toString();
// then
$this->assertSameWindows('one\two\three\file.txt', $asString);
$this->assertSameUnix("one/two/three/file.txt", $asString);
// when, then
$this->assertPathWindows('one\two\three\file.txt', $path);
$this->assertPathUnix("one/two/three/file.txt", $path);
}

public function paths(): array
Expand All @@ -116,8 +107,8 @@ public function shouldAppendPath(Path $path, string $appendant)
// when
$childPath = $path->append($appendant);
// then
$this->assertSameWindows('uno\dos\tres', $childPath->toString());
$this->assertSameUnix("uno/dos/tres", $childPath->toString());
$this->assertPathWindows('uno\dos\tres', $childPath);
$this->assertPathUnix("uno/dos/tres", $childPath);
}

public function children(): array
Expand All @@ -142,33 +133,29 @@ public function children(): array
/**
* @test
*/
public function shouldAcceptPathWithDriveOnWindows()
public function shouldAcceptPathWithDriveForWindows()
{
if ($this->isUnix()) {
$this->markTestUnnecessary("There are no drives on Unix");
}
// given
$path = Path::of("C:\directory");
// when
$child = $path->append("file.txt");
// then
$this->assertSame('C:\directory\file.txt', $child->toString());
$this->assertPathWindows('C:\directory\file.txt', $child);
$this->assertPathUnix("C:/directory/file.txt", $child);
}

/**
* @test
*/
public function shouldRemainAbsolutePathOnUnix()
{
if (!$this->isUnix()) {
$this->markTestUnnecessary("There are no leading separators on Windows");
}
// given
$path = Path::of("/usr/bin");
// when
$child = $path->append("local");
// then
$this->assertSame("/usr/bin/local", $child->toString());
$this->assertPathWindows("\usr\bin\local", $child);
$this->assertPathUnix("/usr/bin/local", $child);
}

/**
Expand All @@ -179,6 +166,7 @@ public function shouldBeImmutable()
// given
$path = Path::of("one/two/three");
// when
$this->assertSame($path->toString(), $path->toString());
$this->assertSame($path->toString("/"), $path->toString("/"));
$this->assertSame($path->toString("\\"), $path->toString("\\"));
}
}

0 comments on commit 7bb2741

Please sign in to comment.