From b96e46b81fe2c154e6c6e6ef41219cff946b753c Mon Sep 17 00:00:00 2001 From: marek Date: Sat, 2 Dec 2023 00:25:37 +0100 Subject: [PATCH] regex update (& some cleanups) --- src/Browser/Test/BootstrappedExtension.php | 4 +- src/Browser/Test/LegacyExtension.php | 2 +- tests/NormalizationTest.php | 73 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 tests/NormalizationTest.php diff --git a/src/Browser/Test/BootstrappedExtension.php b/src/Browser/Test/BootstrappedExtension.php index 44f6f14..6d35643 100644 --- a/src/Browser/Test/BootstrappedExtension.php +++ b/src/Browser/Test/BootstrappedExtension.php @@ -12,7 +12,6 @@ namespace Zenstruck\Browser\Test; use PHPUnit\Event\Code\Test; -use PHPUnit\Event\Code\TestMethod; use PHPUnit\Event\Test\Errored; use PHPUnit\Event\Test\ErroredSubscriber; use PHPUnit\Event\Test\Failed; @@ -25,13 +24,12 @@ use PHPUnit\Event\TestRunner\FinishedSubscriber as TestRunnerFinishedSubscriber; use PHPUnit\Event\TestRunner\Started as TestRunnerStartedEvent; use PHPUnit\Event\TestRunner\StartedSubscriber as TestRunnerStartedSubscriber; -use PHPUnit\Runner\Extension\Extension; use PHPUnit\Runner\Extension\Facade; use PHPUnit\Runner\Extension\ParameterCollection; use PHPUnit\TextUI\Configuration\Configuration; use Zenstruck\Browser; -class BootstrappedExtension implements Extension +class BootstrappedExtension { public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void { diff --git a/src/Browser/Test/LegacyExtension.php b/src/Browser/Test/LegacyExtension.php index e501362..e2aa712 100644 --- a/src/Browser/Test/LegacyExtension.php +++ b/src/Browser/Test/LegacyExtension.php @@ -115,7 +115,7 @@ private static function normalizeTestName(string $name): string { // Try to match for a numeric data set index. If it didn't, match for a string one. if (!\preg_match('#^([\w:\\\]+)(.+\#(\d+))#', $name, $matches)) { - \preg_match('#^([\w:\\\]+)(.+"([^"]+)")#', $name, $matches); + \preg_match('#^([\w:\\\]+)(.+"([^"]+)")?#', $name, $matches); } $normalized = \strtr($matches[1], '\\:', '-_'); diff --git a/tests/NormalizationTest.php b/tests/NormalizationTest.php new file mode 100644 index 0000000..00c70e2 --- /dev/null +++ b/tests/NormalizationTest.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Browser\Tests; + +use PHPUnit\Framework\TestCase; +use Zenstruck\Browser; +use Zenstruck\Browser\Test\LegacyExtension; + +final class NormalizationTest extends TestCase +{ + public static function namesProvider(): \Generator + { + $baseTemplate = 'error_' . __METHOD__; + + yield 'test name without datasets' => [ + 'test name' => __METHOD__, + 'expected output' => \strtr($baseTemplate, '\\:', '-_') . '__0', + ]; + + $datasetTemplate = $baseTemplate . '__data-set-%s__0'; + + $alphaTemplate = sprintf($datasetTemplate, 'test-set', ''); + $alphaOutput = \strtr($alphaTemplate, '\\:', '-_'); + + $numericTemplate = sprintf($datasetTemplate, '0', ''); + $numericOutput = \strtr($numericTemplate, '\\:', '-_'); + + yield 'phpunit 10 alpha' => [ + 'test name' => __METHOD__ . ' with data set "test set"', + 'expected output' => $alphaOutput, + ]; + yield 'phpunit 10 numeric' => [ + 'test name' => __METHOD__ . ' with data set #0', + 'expected output' => $numericOutput, + ]; + yield 'legacy alpha' => [ + 'test name' => __METHOD__ . ' with data set "test set" (test set)', + 'expected output' => $alphaOutput, + ]; + yield 'legacy numeric' => [ + 'test name' => __METHOD__ . ' with data set #0 (test set)', + 'expected output' => $numericOutput, + ]; + } + + /** + * @test + * @dataProvider namesProvider + */ + public function can_use_extension(string $testName, string $normalizedName): void + { + $browser = $this->createMock(Browser::class); + $browser + ->expects(self::once()) + ->method('saveCurrentState') + ->with($normalizedName); + + $extension = new LegacyExtension(); + $extension->executeBeforeFirstTest(); + $extension->executeBeforeTest($testName); + $extension::registerBrowser($browser); + $extension->executeAfterTestError($testName, '', 0); + } +}