From 20077c285a87c42e74736844d68c70902783ae0e Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 00:14:20 -0600 Subject: [PATCH 01/19] WIP proof of concept fix for multiple file extensions in test suffix --- src/Factories/TestCaseFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index f3fdf031..714a7cdc 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -179,7 +179,7 @@ final class TestCaseFactory // Limit to A-Z, a-z, 0-9, '_', '-'. $relativePath = (string) preg_replace('/[^A-Za-z0-9.\\\]/', '', $relativePath); - $classFQN = 'P\\' . $relativePath; + $classFQN = str_replace('.', '', 'P\\' . $relativePath) if (class_exists($classFQN)) { return $classFQN; } From c6a6f7e2ab979123c61ed03a1845e15fd143d56f Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 00:15:54 -0600 Subject: [PATCH 02/19] fix typo --- src/Factories/TestCaseFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 714a7cdc..5de5200c 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -179,7 +179,7 @@ final class TestCaseFactory // Limit to A-Z, a-z, 0-9, '_', '-'. $relativePath = (string) preg_replace('/[^A-Za-z0-9.\\\]/', '', $relativePath); - $classFQN = str_replace('.', '', 'P\\' . $relativePath) + $classFQN = str_replace('.', '', 'P\\' . $relativePath); if (class_exists($classFQN)) { return $classFQN; } From ea6308bfdfed67c6cbc51d1c67564ae06fb1e356 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 09:45:41 -0600 Subject: [PATCH 03/19] add tests for vaious file naming conventions resulting in various suffixes --- tests/PHPUnit/CustomSuffix/A Test With Spaces.php | 4 ++++ tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php | 4 ++++ tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php | 4 ++++ tests/PHPUnit/CustomSuffix/kebab-case-spec.php | 4 ++++ tests/PHPUnit/CustomSuffix/snake_case_spec.php | 4 ++++ 5 files changed, 20 insertions(+) create mode 100644 tests/PHPUnit/CustomSuffix/A Test With Spaces.php create mode 100644 tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php create mode 100644 tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php create mode 100644 tests/PHPUnit/CustomSuffix/kebab-case-spec.php create mode 100644 tests/PHPUnit/CustomSuffix/snake_case_spec.php diff --git a/tests/PHPUnit/CustomSuffix/A Test With Spaces.php b/tests/PHPUnit/CustomSuffix/A Test With Spaces.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomSuffix/A Test With Spaces.php @@ -0,0 +1,4 @@ +assertTrue(true); diff --git a/tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php b/tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php @@ -0,0 +1,4 @@ +assertTrue(true); diff --git a/tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php b/tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php @@ -0,0 +1,4 @@ +assertTrue(true); diff --git a/tests/PHPUnit/CustomSuffix/kebab-case-spec.php b/tests/PHPUnit/CustomSuffix/kebab-case-spec.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomSuffix/kebab-case-spec.php @@ -0,0 +1,4 @@ +assertTrue(true); diff --git a/tests/PHPUnit/CustomSuffix/snake_case_spec.php b/tests/PHPUnit/CustomSuffix/snake_case_spec.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomSuffix/snake_case_spec.php @@ -0,0 +1,4 @@ +assertTrue(true); From 2cee825f61282b0a008c7b2978c36a94b50e37c1 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:07:22 -0600 Subject: [PATCH 04/19] rename test sub directory --- .../{CustomSuffix => CustomAffixes}/A Test With Spaces.php | 0 .../AdditionalFileExtension.spec.php | 0 .../{CustomSuffix => CustomAffixes}/ManyExtensions.class.test.php | 0 .../PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" | 0 .../snake_case_spec.php => CustomAffixes/kebab-case-spec.php} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename tests/PHPUnit/{CustomSuffix => CustomAffixes}/A Test With Spaces.php (100%) rename tests/PHPUnit/{CustomSuffix => CustomAffixes}/AdditionalFileExtension.spec.php (100%) rename tests/PHPUnit/{CustomSuffix => CustomAffixes}/ManyExtensions.class.test.php (100%) rename tests/PHPUnit/CustomSuffix/kebab-case-spec.php => "tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" (100%) rename tests/PHPUnit/{CustomSuffix/snake_case_spec.php => CustomAffixes/kebab-case-spec.php} (100%) diff --git a/tests/PHPUnit/CustomSuffix/A Test With Spaces.php b/tests/PHPUnit/CustomAffixes/A Test With Spaces.php similarity index 100% rename from tests/PHPUnit/CustomSuffix/A Test With Spaces.php rename to tests/PHPUnit/CustomAffixes/A Test With Spaces.php diff --git a/tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php b/tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php similarity index 100% rename from tests/PHPUnit/CustomSuffix/AdditionalFileExtension.spec.php rename to tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php diff --git a/tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php b/tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php similarity index 100% rename from tests/PHPUnit/CustomSuffix/ManyExtensions.class.test.php rename to tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php diff --git a/tests/PHPUnit/CustomSuffix/kebab-case-spec.php "b/tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" similarity index 100% rename from tests/PHPUnit/CustomSuffix/kebab-case-spec.php rename to "tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" diff --git a/tests/PHPUnit/CustomSuffix/snake_case_spec.php b/tests/PHPUnit/CustomAffixes/kebab-case-spec.php similarity index 100% rename from tests/PHPUnit/CustomSuffix/snake_case_spec.php rename to tests/PHPUnit/CustomAffixes/kebab-case-spec.php From be906eb823cc7d50b052ad80a42d55527bf50d36 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:08:08 -0600 Subject: [PATCH 05/19] remove additional str_replace call in favour of adjusting the relative path regexp --- src/Factories/TestCaseFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 5de5200c..f4773ce6 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -177,9 +177,9 @@ final class TestCaseFactory // Strip out any %-encoded octets. $relativePath = (string) preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $relativePath); // Limit to A-Z, a-z, 0-9, '_', '-'. - $relativePath = (string) preg_replace('/[^A-Za-z0-9.\\\]/', '', $relativePath); + $relativePath = (string) preg_replace('/[^A-Za-z0-9\\\\]/', '', $relativePath); - $classFQN = str_replace('.', '', 'P\\' . $relativePath); + $classFQN = 'P\\' . $relativePath; if (class_exists($classFQN)) { return $classFQN; } From 40f2065575aad62629a3f65774ef11bbbce224be Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:12:09 -0600 Subject: [PATCH 06/19] catch parse errors and let the user know in a friendlier manner --- src/Factories/TestCaseFactory.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index f4773ce6..df1c0d2a 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest\Factories; use Closure; +use ParseError; use Pest\Concerns; use Pest\Contracts\HasPrintableTestCaseName; use Pest\Datasets; @@ -13,6 +14,7 @@ use Pest\Support\HigherOrderMessageCollection; use Pest\Support\NullClosure; use Pest\TestSuite; use PHPUnit\Framework\TestCase; +use RuntimeException; /** * @internal @@ -194,15 +196,19 @@ final class TestCaseFactory $namespace = implode('\\', $partsFQN); $baseClass = sprintf('\%s', $this->class); - eval(" - namespace $namespace; + try { + eval(" + namespace $namespace; - final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN { - $traitsCode + final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN { + $traitsCode - private static \$__filename = '$filename'; - } - "); + private static \$__filename = '$filename'; + } + "); + } catch (ParseError $caught) { + throw new RuntimeException(sprintf('Unable to create test case for test file at %s', $filename), 1, $caught); + } return $classFQN; } From 301ff155a4bc7e2592e5417deceffa86b21f9ecc Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:17:52 -0600 Subject: [PATCH 07/19] prevent parse errors by escaping the quote used for filename property --- src/Factories/TestCaseFactory.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index df1c0d2a..77f335d8 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -170,7 +170,8 @@ final class TestCaseFactory }, $filename); } - $filename = (string) realpath($filename); + $quote = '\''; + $filename = str_replace($quote, sprintf('\\%s', $quote), (string) realpath($filename)); $rootPath = TestSuite::getInstance()->rootPath; $relativePath = str_replace($rootPath . DIRECTORY_SEPARATOR, '', $filename); $relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . basename($relativePath, '.php'); @@ -203,7 +204,7 @@ final class TestCaseFactory final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN { $traitsCode - private static \$__filename = '$filename'; + private static \$__filename = $quote$filename$quote; } "); } catch (ParseError $caught) { From 721e047485d66401a1a09bfc729d366fef1f556c Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:18:28 -0600 Subject: [PATCH 08/19] add another test case with non-standard PHP test file name --- tests/PHPUnit/CustomAffixes/snake_case_spec.php | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/PHPUnit/CustomAffixes/snake_case_spec.php diff --git a/tests/PHPUnit/CustomAffixes/snake_case_spec.php b/tests/PHPUnit/CustomAffixes/snake_case_spec.php new file mode 100644 index 00000000..1fa89600 --- /dev/null +++ b/tests/PHPUnit/CustomAffixes/snake_case_spec.php @@ -0,0 +1,4 @@ +assertTrue(true); From a7e2856887ec2096c3743dc2122a86c77b72888f Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:19:00 -0600 Subject: [PATCH 09/19] add test for totally invalid PHP class name --- tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php diff --git a/tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php b/tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php new file mode 100644 index 00000000..accf5eee --- /dev/null +++ b/tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php @@ -0,0 +1,6 @@ +assertTrue(true); + + //!@#$%^&*()-_=+ From 13f09cc6629a140a34c5db768e9072a01d4e2907 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:19:40 -0600 Subject: [PATCH 10/19] add helper method to generate a simple random string --- src/Support/Str.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Support/Str.php b/src/Support/Str.php index aa135647..bdab0577 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -9,6 +9,25 @@ namespace Pest\Support; */ final class Str { + /** + * Pool of alpha-numeric characters for generating (unsafe) random strings + * from. + */ + private const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + + /** + * Create a (unsecure & non-cryptographically safe) random alpha-numeric + * string value. + * + * @param int $length The length of the resulting randomized string. + * @return string + * @see https://github.com/laravel/framework/blob/4.2/src/Illuminate/Support/Str.php#L240-L242 + */ + public static function random(int $length = 16): string + { + return substr(str_shuffle(str_repeat(self::POOL, 5)), 0, $length); + } + /** * Checks if the given `$target` starts with the given `$search`. */ From f6c19e469f8611e0b8e8cffa379ffc0c27c77c99 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:20:16 -0600 Subject: [PATCH 11/19] in the event of no class name, make one on the fly as an escape hatch --- src/Factories/TestCaseFactory.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 77f335d8..aeb6b83c 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -12,6 +12,7 @@ use Pest\Datasets; use Pest\Exceptions\ShouldNotHappen; use Pest\Support\HigherOrderMessageCollection; use Pest\Support\NullClosure; +use Pest\Support\Str; use Pest\TestSuite; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -197,6 +198,11 @@ final class TestCaseFactory $namespace = implode('\\', $partsFQN); $baseClass = sprintf('\%s', $this->class); + if (empty(trim($className))) { + $className = 'InvalidTestName' . Str::random(); + $classFQN .= $className; + } + try { eval(" namespace $namespace; From 5e0bfba7bfdd0af1fd194b4a7f056226023ac656 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:21:14 -0600 Subject: [PATCH 12/19] run linter --- src/Support/Str.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Support/Str.php b/src/Support/Str.php index bdab0577..7a07a105 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -19,8 +19,8 @@ final class Str * Create a (unsecure & non-cryptographically safe) random alpha-numeric * string value. * - * @param int $length The length of the resulting randomized string. - * @return string + * @param int $length the length of the resulting randomized string + * * @see https://github.com/laravel/framework/blob/4.2/src/Illuminate/Support/Str.php#L240-L242 */ public static function random(int $length = 16): string From bb57a5408949ba2b00f9bbc6868de1cf12c76353 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:40:08 -0600 Subject: [PATCH 13/19] simplify quote escape sequence handling --- src/Factories/TestCaseFactory.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index aeb6b83c..e4c0422c 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -171,8 +171,7 @@ final class TestCaseFactory }, $filename); } - $quote = '\''; - $filename = str_replace($quote, sprintf('\\%s', $quote), (string) realpath($filename)); + $filename = addslashes((string) realpath($filename)); $rootPath = TestSuite::getInstance()->rootPath; $relativePath = str_replace($rootPath . DIRECTORY_SEPARATOR, '', $filename); $relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . basename($relativePath, '.php'); @@ -180,6 +179,10 @@ final class TestCaseFactory // Strip out any %-encoded octets. $relativePath = (string) preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $relativePath); + // Remove escaped quote sequences + $relativePath = str_replace(array_map(function (string $quote): string { + return sprintf('\\%s', $quote); + }, ['\'', '"']), '', $relativePath); // Limit to A-Z, a-z, 0-9, '_', '-'. $relativePath = (string) preg_replace('/[^A-Za-z0-9\\\\]/', '', $relativePath); @@ -210,7 +213,7 @@ final class TestCaseFactory final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN { $traitsCode - private static \$__filename = $quote$filename$quote; + private static \$__filename = '$filename'; } "); } catch (ParseError $caught) { From cabd64df0003ed71f5be44225f7d446aeb1d9f1d Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:51:35 -0600 Subject: [PATCH 14/19] rename tests for windows, despite losing some coverage --- .../CustomAffixes/{!@#$%^&*()-_=+.php => @#$%^&()-_=+.php} | 0 .../PHPUnit/CustomAffixes/Test 'Case' With Quotes.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/PHPUnit/CustomAffixes/{!@#$%^&*()-_=+.php => @#$%^&()-_=+.php} (100%) rename "tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" => tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php (100%) diff --git a/tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php b/tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php similarity index 100% rename from tests/PHPUnit/CustomAffixes/!@#$%^&*()-_=+.php rename to tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php diff --git "a/tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" b/tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php similarity index 100% rename from "tests/PHPUnit/CustomAffixes/Test 'Case' \"With\" Quotes.php" rename to tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php From fe2fac37f8b0d1c641ec070501696f3f78ef49cf Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:53:30 -0600 Subject: [PATCH 15/19] address PHPStan warning about use of empty --- src/Factories/TestCaseFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index e4c0422c..502bb083 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -201,7 +201,7 @@ final class TestCaseFactory $namespace = implode('\\', $partsFQN); $baseClass = sprintf('\%s', $this->class); - if (empty(trim($className))) { + if ('' === trim($className)) { $className = 'InvalidTestName' . Str::random(); $classFQN .= $className; } From 9426d08aa2a025558d4817b1bd61be366b24b822 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 12:12:32 -0600 Subject: [PATCH 16/19] update snapshot for running tests with failed classnames --- tests/.snapshots/success.txt | 23 ++++++++++++++++++++++- tests/Visual/Success.php | 8 +++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 36136f81..cfe2a393 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -103,6 +103,27 @@ PASS Tests\Fixtures\ExampleTest ✓ it example 2 + PASS Tests\PHPUnit\CustomAffixes\InvalidTestName + ✓ this runs file names like "@#$%^&()-_=+.php" + + PASS Tests\PHPUnit\CustomAffixes\ATestWithSpaces + ✓ this runs file names like "A Test With Spaces.php" + + PASS Tests\PHPUnit\CustomAffixes\AdditionalFileExtensionspec + ✓ this runs file names like "AdditionalFileExtension.spec.php" + + PASS Tests\PHPUnit\CustomAffixes\ManyExtensionsclasstest + ✓ this runs file names like "ManyExtensions.class.test.php" + + PASS Tests\PHPUnit\CustomAffixes\TestCaseWithQuotes + ✓ this runs file names like "Test 'Case' With Quotes.php" + + PASS Tests\PHPUnit\CustomAffixes\kebabcasespec + ✓ this runs file names like "kebab-case-spec.php" + + PASS Tests\PHPUnit\CustomAffixes\snakecasespec + ✓ this runs file names like "snake_case_spec.php" + PASS Tests\PHPUnit\CustomTestCase\UsesPerDirectory ✓ closure was bound to CustomTestCase @@ -188,5 +209,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 7 skipped, 108 passed + Tests: 7 skipped, 115 passed \ No newline at end of file diff --git a/tests/Visual/Success.php b/tests/Visual/Success.php index 1323da5b..44aac2bc 100644 --- a/tests/Visual/Success.php +++ b/tests/Visual/Success.php @@ -13,7 +13,13 @@ test('visual snapshot of test suite on success', function () { $process->run(); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return preg_replace([ + '#\\x1b[[][^A-Za-z]*[A-Za-z]#', + '/(Tests\\\PHPUnit\\\CustomAffixes\\\InvalidTestName)([A-Za-z0-9]*)/' + ], [ + '', + '$1' + ], $process->getOutput()); }; if (getenv('REBUILD_SNAPSHOTS')) { From dd94a843b51767368f4bce284b84cc15862b721c Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 12:14:22 -0600 Subject: [PATCH 17/19] run linter & auto format --- tests/Visual/Success.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Visual/Success.php b/tests/Visual/Success.php index 44aac2bc..8c5e1d6f 100644 --- a/tests/Visual/Success.php +++ b/tests/Visual/Success.php @@ -15,10 +15,10 @@ test('visual snapshot of test suite on success', function () { return preg_replace([ '#\\x1b[[][^A-Za-z]*[A-Za-z]#', - '/(Tests\\\PHPUnit\\\CustomAffixes\\\InvalidTestName)([A-Za-z0-9]*)/' + '/(Tests\\\PHPUnit\\\CustomAffixes\\\InvalidTestName)([A-Za-z0-9]*)/', ], [ '', - '$1' + '$1', ], $process->getOutput()); }; From 9a0240bc7bff9dd079b19dd26acbd6a0865fc142 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 12:48:43 -0600 Subject: [PATCH 18/19] patch addslashes for windows paths --- src/Factories/TestCaseFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 502bb083..c09730a7 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -171,7 +171,7 @@ final class TestCaseFactory }, $filename); } - $filename = addslashes((string) realpath($filename)); + $filename = str_replace('\\\\', '\\', addslashes((string) realpath($filename))); $rootPath = TestSuite::getInstance()->rootPath; $relativePath = str_replace($rootPath . DIRECTORY_SEPARATOR, '', $filename); $relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . basename($relativePath, '.php'); From 19a1569fa8e9aca2ca6ec341b2e2aecb8c2ecd90 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 13:31:25 -0600 Subject: [PATCH 19/19] cleanup for self-review --- src/Factories/TestCaseFactory.php | 2 +- tests/.snapshots/success.txt | 14 +++++++------- tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php | 10 +++++++--- tests/PHPUnit/CustomAffixes/A Test With Spaces.php | 3 +-- .../CustomAffixes/AdditionalFileExtension.spec.php | 3 +-- .../CustomAffixes/ManyExtensions.class.test.php | 3 +-- .../CustomAffixes/Test 'Case' With Quotes.php | 3 +-- tests/PHPUnit/CustomAffixes/kebab-case-spec.php | 3 +-- tests/PHPUnit/CustomAffixes/snake_case_spec.php | 3 +-- 9 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index c09730a7..4d628e5a 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -179,7 +179,7 @@ final class TestCaseFactory // Strip out any %-encoded octets. $relativePath = (string) preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $relativePath); - // Remove escaped quote sequences + // Remove escaped quote sequences (maintain namespace) $relativePath = str_replace(array_map(function (string $quote): string { return sprintf('\\%s', $quote); }, ['\'', '"']), '', $relativePath); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index cfe2a393..f4db6877 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -104,25 +104,25 @@ ✓ it example 2 PASS Tests\PHPUnit\CustomAffixes\InvalidTestName - ✓ this runs file names like "@#$%^&()-_=+.php" + ✓ it runs file names like `@#$%^&()-_=+.php` PASS Tests\PHPUnit\CustomAffixes\ATestWithSpaces - ✓ this runs file names like "A Test With Spaces.php" + ✓ it runs file names like `A Test With Spaces.php` PASS Tests\PHPUnit\CustomAffixes\AdditionalFileExtensionspec - ✓ this runs file names like "AdditionalFileExtension.spec.php" + ✓ it runs file names like `AdditionalFileExtension.spec.php` PASS Tests\PHPUnit\CustomAffixes\ManyExtensionsclasstest - ✓ this runs file names like "ManyExtensions.class.test.php" + ✓ it runs file names like `ManyExtensions.class.test.php` PASS Tests\PHPUnit\CustomAffixes\TestCaseWithQuotes - ✓ this runs file names like "Test 'Case' With Quotes.php" + ✓ it runs file names like `Test 'Case' With Quotes.php` PASS Tests\PHPUnit\CustomAffixes\kebabcasespec - ✓ this runs file names like "kebab-case-spec.php" + ✓ it runs file names like `kebab-case-spec.php` PASS Tests\PHPUnit\CustomAffixes\snakecasespec - ✓ this runs file names like "snake_case_spec.php" + ✓ it runs file names like `snake_case_spec.php` PASS Tests\PHPUnit\CustomTestCase\UsesPerDirectory ✓ closure was bound to CustomTestCase diff --git a/tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php b/tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php index accf5eee..c829124d 100644 --- a/tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php +++ b/tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php @@ -1,6 +1,10 @@ assertTrue(true); +/* + * NOTE: To preserve cross-platform testing compatibility we cannot use ! * and + * other Windows reserved characters in this test's filename. + * + * See https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions + */ - //!@#$%^&*()-_=+ +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/A Test With Spaces.php b/tests/PHPUnit/CustomAffixes/A Test With Spaces.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/A Test With Spaces.php +++ b/tests/PHPUnit/CustomAffixes/A Test With Spaces.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php b/tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php +++ b/tests/PHPUnit/CustomAffixes/AdditionalFileExtension.spec.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php b/tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php +++ b/tests/PHPUnit/CustomAffixes/ManyExtensions.class.test.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php b/tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php +++ b/tests/PHPUnit/CustomAffixes/Test 'Case' With Quotes.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/kebab-case-spec.php b/tests/PHPUnit/CustomAffixes/kebab-case-spec.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/kebab-case-spec.php +++ b/tests/PHPUnit/CustomAffixes/kebab-case-spec.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true); diff --git a/tests/PHPUnit/CustomAffixes/snake_case_spec.php b/tests/PHPUnit/CustomAffixes/snake_case_spec.php index 1fa89600..28497a1e 100644 --- a/tests/PHPUnit/CustomAffixes/snake_case_spec.php +++ b/tests/PHPUnit/CustomAffixes/snake_case_spec.php @@ -1,4 +1,3 @@ assertTrue(true); +it(sprintf('runs file names like `%s`', basename(__FILE__)))->assertTrue(true);