Skip to content

Commit

Permalink
regex update (& some cleanups)
Browse files Browse the repository at this point in the history
  • Loading branch information
raneomik committed Dec 1, 2023
1 parent bdf8422 commit b96e46b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/Browser/Test/BootstrappedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Browser/Test/LegacyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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], '\\:', '-_');
Expand Down
73 changes: 73 additions & 0 deletions tests/NormalizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the zenstruck/browser package.
*
* (c) Kevin Bond <[email protected]>
*
* 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);
}
}

0 comments on commit b96e46b

Please sign in to comment.