Skip to content

Commit

Permalink
Merge branch '11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 18, 2024
2 parents d38e409 + 08d42ea commit 0f800f8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/TextUI/Configuration/Cli/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function is_file;
use function is_numeric;
use function sprintf;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\Util\Filesystem;
use SebastianBergmann\CliParser\Exception as CliParserException;
Expand All @@ -24,7 +25,7 @@
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class Builder
final class Builder
{
private const LONG_OPTIONS = [
'atleast-version=',
Expand Down Expand Up @@ -127,6 +128,11 @@
];
private const SHORT_OPTIONS = 'd:c:h';

/**
* @psalm-var array<string, non-negative-int>
*/
private array $processed = [];

/**
* @throws Exception
*/
Expand Down Expand Up @@ -838,6 +844,8 @@ public function fromParameters(array $parameters): Configuration

break;
}

$this->markProcessed($option[0]);
}

if (empty($iniSettings)) {
Expand Down Expand Up @@ -949,4 +957,27 @@ public function fromParameters(array $parameters): Configuration
$debug,
);
}

/**
* @psalm-param non-empty-string $option
*/
private function markProcessed(string $option): void
{
if (!isset($this->processed[$option])) {
$this->processed[$option] = 1;

return;
}

$this->processed[$option]++;

if ($this->processed[$option] === 2) {
EventFacade::emitter()->testRunnerTriggeredWarning(
sprintf(
'Option %s cannot be used more than once',
$option,
),
);
}
}
}
39 changes: 39 additions & 0 deletions tests/end-to-end/event/duplicated-cli-options.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
The right events are emitted in the right order when duplicated CLI options are used
--FILE--
<?php declare(strict_types=1);
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-text';
$_SERVER['argv'][] = $traceFile;
$_SERVER['argv'][] = '--filter';
$_SERVER['argv'][] = 'foo';
$_SERVER['argv'][] = '--filter';
$_SERVER['argv'][] = 'bar';
$_SERVER['argv'][] = '--filter';
$_SERVER['argv'][] = 'baz';
$_SERVER['argv'][] = __DIR__ . '/_files/SuccessTest.php';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);

print file_get_contents($traceFile);

unlink($traceFile);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Triggered Warning (Option --filter cannot be used more than once)
Test Runner Configured
Test Suite Loaded (1 test)
Event Facade Sealed
Test Runner Started
Test Suite Sorted
Test Suite Filtered (0 tests)
Test Runner Execution Started (0 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 1)

0 comments on commit 0f800f8

Please sign in to comment.