Skip to content

Commit

Permalink
Closes #5704
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 18, 2024
1 parent 356fdfd commit b6bcafe
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog-10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions 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 Down Expand Up @@ -127,6 +128,11 @@ final class Builder
];
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 b6bcafe

Please sign in to comment.