Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an OS agnostic path assertion #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ jobs:
matrix:
php: [8.1, 8.0, 7.4]
dependency-version: [prefer-lowest, prefer-stable]
operating-system:
- "macos-latest"
- "ubuntu-latest"
- "windows-latest"
exclude:
- php: 8.1
dependency-version: prefer-lowest

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.operating-system }}

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ This will prevent any method name conflicts with core, your custom or other trai
\Astrotomic\PhpunitAssertions\PathAssertions::assertBasename('image.jpg', '/foo/bar/image.jpg');
\Astrotomic\PhpunitAssertions\PathAssertions::assertFilename('image', '/foo/bar/image.jpg');
\Astrotomic\PhpunitAssertions\PathAssertions::assertExtension('jpg', '/foo/bar/image.jpg');
\Astrotomic\PhpunitAssertions\PathAssertions::assertOsAgnosticPath(__DIR__ . '/resources/css/main.css', __DIR__ . '\resources\css\main.css');
```

### UUID
Expand Down
19 changes: 19 additions & 0 deletions src/PathAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@ public static function assertComponent($expected, $actual, int $component): void
PHPUnit::assertIsString($actual);
PHPUnit::assertSame($expected, pathinfo($actual, $component));
}

private static function agnosticPath(string $path): string
{
if (DIRECTORY_SEPARATOR === '/') {
if (str_contains($path, '\\')) {
return str_replace('\\', DIRECTORY_SEPARATOR, $path);
}
return $path;
}

return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

public static function assertOsAgnosticPath(string $expected, $actual): void
{
$osNormalizedExpected = PathAssertions::agnosticPath($expected);
PHPUnit::assertIsString($actual);
PHPUnit::assertSame($osNormalizedExpected, $actual);
}
}
26 changes: 26 additions & 0 deletions tests/PathAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,30 @@ public static function it_can_validate_extension(): void

PathAssertions::assertExtension($extension, $directory.'/'.$filename.'.'.$extension);
}

/**
* Detect the OS of the PHP in use and return a path with opposite DIR seperator.
*
* When given the rand: true parameter it will sometimes provide mixed results.
*/
private static function os_agnostic_get_expected_path(bool $rand = false): string
{
// Intentionally set the "expected" path to opposite of what should work on the platform.
if (PHP_OS_FAMILY !== "Windows" || ($rand === true && 1 === self::randomInt(0, 4))) {
return dirname(__DIR__) . '\tests\Utils';
}

return dirname(__DIR__) . '/tests/Utils';
}

/**
* @test
* @dataProvider hundredTimes
*/
public static function it_can_validate_os_agnostic_paths(): void
{
$expected = static::os_agnostic_get_expected_path(true);
static::assertNotEquals($expected, realpath(dirname(__DIR__) . '/tests/Utils'));
PathAssertions::assertOsAgnosticPath($expected, realpath(dirname(__DIR__) . '/tests/Utils'));
}
}