diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b67ff04..8ec5f09 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/README.md b/README.md index efb8a3f..4bf4724 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/PathAssertions.php b/src/PathAssertions.php index fdf1261..f0e3913 100644 --- a/src/PathAssertions.php +++ b/src/PathAssertions.php @@ -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); + } } diff --git a/tests/PathAssertionsTest.php b/tests/PathAssertionsTest.php index f08a060..1b5cf31 100644 --- a/tests/PathAssertionsTest.php +++ b/tests/PathAssertionsTest.php @@ -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')); + } }