From 645ab7beac1ddeef95435863bb1f0619701e9188 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 22:21:50 -0400 Subject: [PATCH 1/6] Run tests on multiple OSes --- .github/workflows/run-tests.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From d1036217835a5404b8637b7fee0bd45db2a10bc1 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 22:43:33 -0400 Subject: [PATCH 2/6] add agnostic path helpers --- src/PathAssertions.php | 19 +++++++++++++++++++ tests/PathAssertionsTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) 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..ccf50ca 100644 --- a/tests/PathAssertionsTest.php +++ b/tests/PathAssertionsTest.php @@ -57,4 +57,39 @@ public static function it_can_validate_extension(): void PathAssertions::assertExtension($extension, $directory.'/'.$filename.'.'.$extension); } + + private static function get_expected_path(): string + { + // Intentionally set the "expected" path to opposite of what should work on the platform. + if (PHP_OS_FAMILY !== "Windows") { + return dirname(__DIR__) . '\tests\Utils'; + } + + return dirname(__DIR__) . '/tests/Utils'; + } + + private static function get_actual_path(): string + { + return realpath(dirname(__DIR__) . '/tests/Utils'); + } + + /** + * @test + * @dataProvider hundredTimes + */ + public static function it_can_validate_os_agnostic_paths(): void + { + $expected = static::get_expected_path(); + PathAssertions::assertOsAgnosticPath($expected, static::get_actual_path()); + } + + /** + * @test + * @dataProvider hundredTimes + */ + public static function it_can_validate_os_gnostic_paths_fail(): void + { + $expected = static::get_expected_path(); + static::assertNotEquals($expected, static::get_actual_path()); + } } From 57b4b0277f18cbf12a5f2c8fdeda683ee99d68e5 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 22:56:01 -0400 Subject: [PATCH 3/6] Adjust tests --- tests/PathAssertionsTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/PathAssertionsTest.php b/tests/PathAssertionsTest.php index ccf50ca..efa5dd1 100644 --- a/tests/PathAssertionsTest.php +++ b/tests/PathAssertionsTest.php @@ -58,10 +58,15 @@ public static function it_can_validate_extension(): void PathAssertions::assertExtension($extension, $directory.'/'.$filename.'.'.$extension); } - private static function get_expected_path(): string + /** + * 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 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") { + if (PHP_OS_FAMILY !== "Windows" || ($rand === true && 1 === self::randomInt(0, 4))) { return dirname(__DIR__) . '\tests\Utils'; } @@ -79,7 +84,7 @@ private static function get_actual_path(): string */ public static function it_can_validate_os_agnostic_paths(): void { - $expected = static::get_expected_path(); + $expected = static::get_expected_path(true); PathAssertions::assertOsAgnosticPath($expected, static::get_actual_path()); } From b3c9668f849b6aa964f20a662578a8f99a9d689d Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 22:59:55 -0400 Subject: [PATCH 4/6] refactor tests --- tests/PathAssertionsTest.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/PathAssertionsTest.php b/tests/PathAssertionsTest.php index efa5dd1..bfc1723 100644 --- a/tests/PathAssertionsTest.php +++ b/tests/PathAssertionsTest.php @@ -63,7 +63,7 @@ public static function it_can_validate_extension(): void * * When given the rand: true parameter it will sometimes provide mixed results. */ - private static function get_expected_path(bool $rand = false): string + 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))) { @@ -73,19 +73,14 @@ private static function get_expected_path(bool $rand = false): string return dirname(__DIR__) . '/tests/Utils'; } - private static function get_actual_path(): string - { - return realpath(dirname(__DIR__) . '/tests/Utils'); - } - /** * @test * @dataProvider hundredTimes */ public static function it_can_validate_os_agnostic_paths(): void { - $expected = static::get_expected_path(true); - PathAssertions::assertOsAgnosticPath($expected, static::get_actual_path()); + $expected = static::os_agnostic_get_expected_path(true); + PathAssertions::assertOsAgnosticPath($expected, realpath(dirname(__DIR__) . '/tests/Utils')); } /** @@ -94,7 +89,7 @@ public static function it_can_validate_os_agnostic_paths(): void */ public static function it_can_validate_os_gnostic_paths_fail(): void { - $expected = static::get_expected_path(); - static::assertNotEquals($expected, static::get_actual_path()); + $expected = static::os_agnostic_get_expected_path(); + static::assertNotEquals($expected, realpath(dirname(__DIR__) . '/tests/Utils')); } } From 8b31c33f2dbd5a768f67a166b323a0e256489664 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 23:01:24 -0400 Subject: [PATCH 5/6] duh... --- tests/PathAssertionsTest.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/PathAssertionsTest.php b/tests/PathAssertionsTest.php index bfc1723..1b5cf31 100644 --- a/tests/PathAssertionsTest.php +++ b/tests/PathAssertionsTest.php @@ -80,16 +80,7 @@ private static function os_agnostic_get_expected_path(bool $rand = false): strin public static function it_can_validate_os_agnostic_paths(): void { $expected = static::os_agnostic_get_expected_path(true); - PathAssertions::assertOsAgnosticPath($expected, realpath(dirname(__DIR__) . '/tests/Utils')); - } - - /** - * @test - * @dataProvider hundredTimes - */ - public static function it_can_validate_os_gnostic_paths_fail(): void - { - $expected = static::os_agnostic_get_expected_path(); static::assertNotEquals($expected, realpath(dirname(__DIR__) . '/tests/Utils')); + PathAssertions::assertOsAgnosticPath($expected, realpath(dirname(__DIR__) . '/tests/Utils')); } } From 79920d07a54e21b445b4e7d6cd15ead66e9d2c8e Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 3 Jun 2022 23:08:55 -0400 Subject: [PATCH 6/6] Add example to readme file --- README.md | 1 + 1 file changed, 1 insertion(+) 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