From b6bcafee3eb8d21ab423050ab87809221c33e689 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 18 Feb 2024 11:35:31 +0100 Subject: [PATCH] Closes #5704 --- ChangeLog-10.5.md | 1 + src/TextUI/Configuration/Cli/Builder.php | 31 +++++++++++++++ .../event/duplicated-cli-options.phpt | 39 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/end-to-end/event/duplicated-cli-options.phpt diff --git a/ChangeLog-10.5.md b/ChangeLog-10.5.md index f4b58731d05..d29ad2ccc7a 100644 --- a/ChangeLog-10.5.md +++ b/ChangeLog-10.5.md @@ -6,6 +6,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi ### Fixed +* [#5704](https://github.com/sebastianbergmann/phpunit/issues/5704#issuecomment-1951105254): No warning when CLI options are used multiple times * [#5707](https://github.com/sebastianbergmann/phpunit/issues/5707): `--fail-on-empty-test-suite` CLI option is not documented in `--help` output * Resource usage information is printed when the `--debug` CLI option is used diff --git a/src/TextUI/Configuration/Cli/Builder.php b/src/TextUI/Configuration/Cli/Builder.php index 0f6cb18a52b..db82c9f570d 100644 --- a/src/TextUI/Configuration/Cli/Builder.php +++ b/src/TextUI/Configuration/Cli/Builder.php @@ -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; @@ -127,6 +128,11 @@ final class Builder ]; private const SHORT_OPTIONS = 'd:c:h'; + /** + * @psalm-var array + */ + private array $processed = []; + /** * @throws Exception */ @@ -838,6 +844,8 @@ public function fromParameters(array $parameters): Configuration break; } + + $this->markProcessed($option[0]); } if (empty($iniSettings)) { @@ -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, + ), + ); + } + } } diff --git a/tests/end-to-end/event/duplicated-cli-options.phpt b/tests/end-to-end/event/duplicated-cli-options.phpt new file mode 100644 index 00000000000..78ad1ccee32 --- /dev/null +++ b/tests/end-to-end/event/duplicated-cli-options.phpt @@ -0,0 +1,39 @@ +--TEST-- +The right events are emitted in the right order when duplicated CLI options are used +--FILE-- +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)