From 1296afe8f5b171eb1d7d39776491e484869acc5c Mon Sep 17 00:00:00 2001 From: joke2k Date: Thu, 4 Jan 2024 01:40:13 +0000 Subject: [PATCH] add --list-test-files option --- src/TextUI/Application.php | 5 ++ .../Command/Commands/ListTestFilesCommand.php | 81 +++++++++++++++++++ src/TextUI/Configuration/Cli/Builder.php | 8 ++ .../Configuration/Cli/Configuration.php | 9 ++- src/TextUI/Help.php | 1 + .../_files/output-cli-help-color.txt | 1 + tests/end-to-end/_files/output-cli-usage.txt | 1 + .../list-file-tests/list-phpt-test-files.phpt | 15 ++++ .../list-test-files-exclude-group.phpt | 16 ++++ .../list-test-files-group.phpt | 17 ++++ .../cli/list-file-tests/list-test-files.phpt | 19 +++++ 11 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/TextUI/Command/Commands/ListTestFilesCommand.php create mode 100644 tests/end-to-end/cli/list-file-tests/list-phpt-test-files.phpt create mode 100644 tests/end-to-end/cli/list-file-tests/list-test-files-exclude-group.phpt create mode 100644 tests/end-to-end/cli/list-file-tests/list-test-files-group.phpt create mode 100644 tests/end-to-end/cli/list-file-tests/list-test-files.phpt diff --git a/src/TextUI/Application.php b/src/TextUI/Application.php index 913e7f60996..11a6b6bfcb3 100644 --- a/src/TextUI/Application.php +++ b/src/TextUI/Application.php @@ -52,6 +52,7 @@ use PHPUnit\TextUI\Command\AtLeastVersionCommand; use PHPUnit\TextUI\Command\GenerateConfigurationCommand; use PHPUnit\TextUI\Command\ListGroupsCommand; +use PHPUnit\TextUI\Command\ListTestFilesCommand; use PHPUnit\TextUI\Command\ListTestsAsTextCommand; use PHPUnit\TextUI\Command\ListTestsAsXmlCommand; use PHPUnit\TextUI\Command\ListTestSuitesCommand; @@ -417,6 +418,10 @@ private function executeCommandsThatRequireCliConfigurationAndTestSuite(CliConfi ), ); } + + if ($cliConfiguration->listTestFiles()) { + $this->execute(new ListTestFilesCommand($testSuite)); + } } private function executeCommandsThatRequireCompleteConfiguration(Configuration $configuration, CliConfiguration $cliConfiguration): void diff --git a/src/TextUI/Command/Commands/ListTestFilesCommand.php b/src/TextUI/Command/Commands/ListTestFilesCommand.php new file mode 100644 index 00000000000..dd8eb598e7d --- /dev/null +++ b/src/TextUI/Command/Commands/ListTestFilesCommand.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TextUI\Command; + +use function array_intersect; +use function array_unique; +use function sprintf; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestSuite; +use PHPUnit\Runner\PhptTestCase; +use PHPUnit\TextUI\Configuration\Registry; +use RecursiveIteratorIterator; +use ReflectionClass; +use ReflectionException; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final readonly class ListTestFilesCommand implements Command +{ + private readonly TestSuite $suite; + + public function __construct(TestSuite $suite) + { + $this->suite = $suite; + } + + /** + * @throws ReflectionException + */ + public function execute(): Result + { + $configuration = Registry::get(); + + $buffer = 'Available test files:' . PHP_EOL; + + $results = []; + + foreach (new RecursiveIteratorIterator($this->suite) as $test) { + if ($test instanceof TestCase) { + $name = (new ReflectionClass($test))->getFileName(); + + // @codeCoverageIgnoreStart + if ($name === false) { + continue; + } + // @codeCoverageIgnoreEnd + + if ($configuration->hasGroups() && empty(array_intersect($configuration->groups(), $test->groups()))) { + continue; + } + + if ($configuration->hasExcludeGroups() && !empty(array_intersect($configuration->excludeGroups(), $test->groups()))) { + continue; + } + } elseif ($test instanceof PhptTestCase) { + $name = $test->getName(); + } else { + continue; + } + + $results[] = $name; + } + + foreach (array_unique($results) as $result) { + $buffer .= sprintf( + ' - %s' . PHP_EOL, + $result, + ); + } + + return Result::from($buffer); + } +} diff --git a/src/TextUI/Configuration/Cli/Builder.php b/src/TextUI/Configuration/Cli/Builder.php index b621b788647..52936ae6206 100644 --- a/src/TextUI/Configuration/Cli/Builder.php +++ b/src/TextUI/Configuration/Cli/Builder.php @@ -72,6 +72,7 @@ 'include-path=', 'list-groups', 'list-suites', + 'list-test-files', 'list-tests', 'list-tests-xml=', 'log-junit=', @@ -213,6 +214,7 @@ public function fromParameters(array $parameters): Configuration $junitLogfile = null; $listGroups = false; $listSuites = false; + $listTestFiles = false; $listTests = false; $listTestsXml = null; $noCoverage = null; @@ -447,6 +449,11 @@ public function fromParameters(array $parameters): Configuration break; + case '--list-test-files': + $listTestFiles = true; + + break; + case '--list-tests': $listTests = true; @@ -904,6 +911,7 @@ public function fromParameters(array $parameters): Configuration $junitLogfile, $listGroups, $listSuites, + $listTestFiles, $listTests, $listTestsXml, $noCoverage, diff --git a/src/TextUI/Configuration/Cli/Configuration.php b/src/TextUI/Configuration/Cli/Configuration.php index bbc0266adb7..5c738e2497c 100644 --- a/src/TextUI/Configuration/Cli/Configuration.php +++ b/src/TextUI/Configuration/Cli/Configuration.php @@ -82,6 +82,7 @@ private ?string $junitLogfile; private bool $listGroups; private bool $listSuites; + private bool $listTestFiles; private bool $listTests; private ?string $listTestsXml; private ?bool $noCoverage; @@ -125,7 +126,7 @@ * @psalm-param list $arguments * @psalm-param ?non-empty-list $testSuffixes */ - public function __construct(array $arguments, ?string $atLeastVersion, ?bool $backupGlobals, ?bool $backupStaticProperties, ?bool $beStrictAboutChangesToGlobalState, ?string $bootstrap, ?string $cacheDirectory, ?bool $cacheResult, bool $checkVersion, ?string $colors, null|int|string $columns, ?string $configurationFile, ?string $coverageClover, ?string $coverageCobertura, ?string $coverageCrap4J, ?string $coverageHtml, ?string $coveragePhp, ?string $coverageText, ?bool $coverageTextShowUncoveredFiles, ?bool $coverageTextShowOnlySummary, ?string $coverageXml, ?bool $pathCoverage, bool $warmCoverageCache, ?int $defaultTimeLimit, ?bool $disableCodeCoverageIgnore, ?bool $disallowTestOutput, ?bool $enforceTimeLimit, ?array $excludeGroups, ?int $executionOrder, ?int $executionOrderDefects, ?bool $failOnDeprecation, ?bool $failOnEmptyTestSuite, ?bool $failOnIncomplete, ?bool $failOnNotice, ?bool $failOnRisky, ?bool $failOnSkipped, ?bool $failOnWarning, ?bool $stopOnDefect, ?bool $stopOnDeprecation, ?bool $stopOnError, ?bool $stopOnFailure, ?bool $stopOnIncomplete, ?bool $stopOnNotice, ?bool $stopOnRisky, ?bool $stopOnSkipped, ?bool $stopOnWarning, ?string $filter, ?string $excludeFilter, ?string $generateBaseline, ?string $useBaseline, bool $ignoreBaseline, bool $generateConfiguration, bool $migrateConfiguration, ?array $groups, ?array $testsCovering, ?array $testsUsing, bool $help, ?string $includePath, ?array $iniSettings, ?string $junitLogfile, bool $listGroups, bool $listSuites, bool $listTests, ?string $listTestsXml, ?bool $noCoverage, ?bool $noExtensions, ?bool $noOutput, ?bool $noProgress, ?bool $noResults, ?bool $noLogging, ?bool $processIsolation, ?int $randomOrderSeed, ?bool $reportUselessTests, ?bool $resolveDependencies, ?bool $reverseList, ?bool $stderr, ?bool $strictCoverage, ?string $teamcityLogfile, ?string $testdoxHtmlFile, ?string $testdoxTextFile, ?array $testSuffixes, ?string $testSuite, ?string $excludeTestSuite, bool $useDefaultConfiguration, ?bool $displayDetailsOnIncompleteTests, ?bool $displayDetailsOnSkippedTests, ?bool $displayDetailsOnTestsThatTriggerDeprecations, ?bool $displayDetailsOnTestsThatTriggerErrors, ?bool $displayDetailsOnTestsThatTriggerNotices, ?bool $displayDetailsOnTestsThatTriggerWarnings, bool $version, ?array $coverageFilter, ?string $logEventsText, ?string $logEventsVerboseText, ?bool $printerTeamCity, ?bool $printerTestDox, bool $debug) + public function __construct(array $arguments, ?string $atLeastVersion, ?bool $backupGlobals, ?bool $backupStaticProperties, ?bool $beStrictAboutChangesToGlobalState, ?string $bootstrap, ?string $cacheDirectory, ?bool $cacheResult, bool $checkVersion, ?string $colors, null|int|string $columns, ?string $configurationFile, ?string $coverageClover, ?string $coverageCobertura, ?string $coverageCrap4J, ?string $coverageHtml, ?string $coveragePhp, ?string $coverageText, ?bool $coverageTextShowUncoveredFiles, ?bool $coverageTextShowOnlySummary, ?string $coverageXml, ?bool $pathCoverage, bool $warmCoverageCache, ?int $defaultTimeLimit, ?bool $disableCodeCoverageIgnore, ?bool $disallowTestOutput, ?bool $enforceTimeLimit, ?array $excludeGroups, ?int $executionOrder, ?int $executionOrderDefects, ?bool $failOnDeprecation, ?bool $failOnEmptyTestSuite, ?bool $failOnIncomplete, ?bool $failOnNotice, ?bool $failOnRisky, ?bool $failOnSkipped, ?bool $failOnWarning, ?bool $stopOnDefect, ?bool $stopOnDeprecation, ?bool $stopOnError, ?bool $stopOnFailure, ?bool $stopOnIncomplete, ?bool $stopOnNotice, ?bool $stopOnRisky, ?bool $stopOnSkipped, ?bool $stopOnWarning, ?string $filter, ?string $excludeFilter, ?string $generateBaseline, ?string $useBaseline, bool $ignoreBaseline, bool $generateConfiguration, bool $migrateConfiguration, ?array $groups, ?array $testsCovering, ?array $testsUsing, bool $help, ?string $includePath, ?array $iniSettings, ?string $junitLogfile, bool $listGroups, bool $listSuites, bool $listTestFiles, bool $listTests, ?string $listTestsXml, ?bool $noCoverage, ?bool $noExtensions, ?bool $noOutput, ?bool $noProgress, ?bool $noResults, ?bool $noLogging, ?bool $processIsolation, ?int $randomOrderSeed, ?bool $reportUselessTests, ?bool $resolveDependencies, ?bool $reverseList, ?bool $stderr, ?bool $strictCoverage, ?string $teamcityLogfile, ?string $testdoxHtmlFile, ?string $testdoxTextFile, ?array $testSuffixes, ?string $testSuite, ?string $excludeTestSuite, bool $useDefaultConfiguration, ?bool $displayDetailsOnIncompleteTests, ?bool $displayDetailsOnSkippedTests, ?bool $displayDetailsOnTestsThatTriggerDeprecations, ?bool $displayDetailsOnTestsThatTriggerErrors, ?bool $displayDetailsOnTestsThatTriggerNotices, ?bool $displayDetailsOnTestsThatTriggerWarnings, bool $version, ?array $coverageFilter, ?string $logEventsText, ?string $logEventsVerboseText, ?bool $printerTeamCity, ?bool $printerTestDox, bool $debug) { $this->arguments = $arguments; $this->atLeastVersion = $atLeastVersion; @@ -190,6 +191,7 @@ public function __construct(array $arguments, ?string $atLeastVersion, ?bool $ba $this->junitLogfile = $junitLogfile; $this->listGroups = $listGroups; $this->listSuites = $listSuites; + $this->listTestFiles = $listTestFiles; $this->listTests = $listTests; $this->listTestsXml = $listTestsXml; $this->noCoverage = $noCoverage; @@ -1354,6 +1356,11 @@ public function listSuites(): bool return $this->listSuites; } + public function listTestFiles(): bool + { + return $this->listTestFiles; + } + public function listTests(): bool { return $this->listTests; diff --git a/src/TextUI/Help.php b/src/TextUI/Help.php index 3fb26c2343a..90f4043c892 100644 --- a/src/TextUI/Help.php +++ b/src/TextUI/Help.php @@ -56,6 +56,7 @@ final class Help ['arg' => '--exclude-group ', 'desc' => 'Exclude tests from the specified group(s)'], ['arg' => '--covers ', 'desc' => 'Only run tests that intend to cover '], ['arg' => '--uses ', 'desc' => 'Only run tests that intend to use '], + ['arg' => '--list-test-files', 'desc' => 'List available test files'], ['arg' => '--list-tests', 'desc' => 'List available tests'], ['arg' => '--list-tests-xml ', 'desc' => 'List available tests in XML format'], ['arg' => '--filter ', 'desc' => 'Filter which tests to run'], diff --git a/tests/end-to-end/_files/output-cli-help-color.txt b/tests/end-to-end/_files/output-cli-help-color.txt index 66505e13148..42b388146b1 100644 --- a/tests/end-to-end/_files/output-cli-help-color.txt +++ b/tests/end-to-end/_files/output-cli-help-color.txt @@ -32,6 +32,7 @@ --exclude-group   Exclude tests from the specified group(s) --covers   Only run tests that intend to cover --uses   Only run tests that intend to use + --list-test-files  List available test files --list-tests  List available tests --list-tests-xml   List available tests in XML format --filter   Filter which tests to run diff --git a/tests/end-to-end/_files/output-cli-usage.txt b/tests/end-to-end/_files/output-cli-usage.txt index 395f220c715..abb513cb13e 100644 --- a/tests/end-to-end/_files/output-cli-usage.txt +++ b/tests/end-to-end/_files/output-cli-usage.txt @@ -28,6 +28,7 @@ Selection: --exclude-group Exclude tests from the specified group(s) --covers Only run tests that intend to cover --uses Only run tests that intend to use + --list-test-files List available test files --list-tests List available tests --list-tests-xml List available tests in XML format --filter Filter which tests to run diff --git a/tests/end-to-end/cli/list-file-tests/list-phpt-test-files.phpt b/tests/end-to-end/cli/list-file-tests/list-phpt-test-files.phpt new file mode 100644 index 00000000000..f0ff205f181 --- /dev/null +++ b/tests/end-to-end/cli/list-file-tests/list-phpt-test-files.phpt @@ -0,0 +1,15 @@ +--TEST-- +phpunit --list-test-files list-phpt-test-files.phpt +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Available test files: + - %send-to-end%slist-phpt-test-files.phpt diff --git a/tests/end-to-end/cli/list-file-tests/list-test-files-exclude-group.phpt b/tests/end-to-end/cli/list-file-tests/list-test-files-exclude-group.phpt new file mode 100644 index 00000000000..a351751e7ce --- /dev/null +++ b/tests/end-to-end/cli/list-file-tests/list-test-files-exclude-group.phpt @@ -0,0 +1,16 @@ +--TEST-- +phpunit --list-test-files --exclude-group group ../../../_files/Metadata/Annotation/tests/GroupTest.php +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Available test files: diff --git a/tests/end-to-end/cli/list-file-tests/list-test-files-group.phpt b/tests/end-to-end/cli/list-file-tests/list-test-files-group.phpt new file mode 100644 index 00000000000..6f71169d3ca --- /dev/null +++ b/tests/end-to-end/cli/list-file-tests/list-test-files-group.phpt @@ -0,0 +1,17 @@ +--TEST-- +phpunit --list-test-files --group group ../../../_files/Metadata/Annotation/tests/ +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Available test files: + - %Annotation%sGroupTest.php diff --git a/tests/end-to-end/cli/list-file-tests/list-test-files.phpt b/tests/end-to-end/cli/list-file-tests/list-test-files.phpt new file mode 100644 index 00000000000..ce51dd7768e --- /dev/null +++ b/tests/end-to-end/cli/list-file-tests/list-test-files.phpt @@ -0,0 +1,19 @@ +--TEST-- +phpunit --list-test-files --configuration ../../../_files/basic/configuration.basic.xml +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Available test files: + - %send-to-end%sSetUpBeforeClassTest.php + - %send-to-end%sSetUpTest.php + - %send-to-end%sStatusTest.php + - %send-to-end%sTearDownAfterClassTest.php