Skip to content

Commit

Permalink
add --list-test-files option
Browse files Browse the repository at this point in the history
  • Loading branch information
joke2k authored and sebastianbergmann committed Jan 12, 2024
1 parent 63eb2ab commit fa87e27
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/TextUI/Application.php
Expand Up @@ -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;
Expand Down Expand Up @@ -417,6 +418,10 @@ private function executeCommandsThatRequireCliConfigurationAndTestSuite(CliConfi
),
);
}

if ($cliConfiguration->listTestFiles()) {
$this->execute(new ListTestFilesCommand($testSuite));
}
}

private function executeCommandsThatRequireCompleteConfiguration(Configuration $configuration, CliConfiguration $cliConfiguration): void
Expand Down
81 changes: 81 additions & 0 deletions src/TextUI/Command/Commands/ListTestFilesCommand.php
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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);
}
}
8 changes: 8 additions & 0 deletions src/TextUI/Configuration/Cli/Builder.php
Expand Up @@ -72,6 +72,7 @@
'include-path=',
'list-groups',
'list-suites',
'list-test-files',
'list-tests',
'list-tests-xml=',
'log-junit=',
Expand Down Expand Up @@ -213,6 +214,7 @@ public function fromParameters(array $parameters): Configuration
$junitLogfile = null;
$listGroups = false;
$listSuites = false;
$listTestFiles = false;
$listTests = false;
$listTestsXml = null;
$noCoverage = null;
Expand Down Expand Up @@ -447,6 +449,11 @@ public function fromParameters(array $parameters): Configuration

break;

case '--list-test-files':
$listTestFiles = true;

break;

case '--list-tests':
$listTests = true;

Expand Down Expand Up @@ -904,6 +911,7 @@ public function fromParameters(array $parameters): Configuration
$junitLogfile,
$listGroups,
$listSuites,
$listTestFiles,
$listTests,
$listTestsXml,
$noCoverage,
Expand Down
9 changes: 8 additions & 1 deletion src/TextUI/Configuration/Cli/Configuration.php
Expand Up @@ -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;
Expand Down Expand Up @@ -125,7 +126,7 @@
* @psalm-param list<non-empty-string> $arguments
* @psalm-param ?non-empty-list<non-empty-string> $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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/TextUI/Help.php
Expand Up @@ -56,6 +56,7 @@ final class Help
['arg' => '--exclude-group <name>', 'desc' => 'Exclude tests from the specified group(s)'],
['arg' => '--covers <name>', 'desc' => 'Only run tests that intend to cover <name>'],
['arg' => '--uses <name>', 'desc' => 'Only run tests that intend to use <name>'],
['arg' => '--list-test-files', 'desc' => 'List available test files'],
['arg' => '--list-tests', 'desc' => 'List available tests'],
['arg' => '--list-tests-xml <file>', 'desc' => 'List available tests in XML format'],
['arg' => '--filter <pattern>', 'desc' => 'Filter which tests to run'],
Expand Down
1 change: 1 addition & 0 deletions tests/end-to-end/_files/output-cli-help-color.txt
Expand Up @@ -32,6 +32,7 @@
--exclude-group <name>  Exclude tests from the specified group(s)
--covers <name>  Only run tests that intend to cover <name>
--uses <name>  Only run tests that intend to use <name>
--list-test-files  List available test files
--list-tests  List available tests
--list-tests-xml <file>  List available tests in XML format
--filter <pattern>  Filter which tests to run
Expand Down
1 change: 1 addition & 0 deletions tests/end-to-end/_files/output-cli-usage.txt
Expand Up @@ -28,6 +28,7 @@ Selection:
--exclude-group <name> Exclude tests from the specified group(s)
--covers <name> Only run tests that intend to cover <name>
--uses <name> Only run tests that intend to use <name>
--list-test-files List available test files
--list-tests List available tests
--list-tests-xml <file> List available tests in XML format
--filter <pattern> Filter which tests to run
Expand Down
15 changes: 15 additions & 0 deletions 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--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--list-test-files';
$_SERVER['argv'][] = __DIR__.'/list-phpt-test-files.phpt';

require_once __DIR__ . '/../../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test files:
- %send-to-end%slist-phpt-test-files.phpt
@@ -0,0 +1,16 @@
--TEST--
phpunit --list-test-files --exclude-group group ../../../_files/Metadata/Annotation/tests/GroupTest.php
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--list-test-files';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'group';
$_SERVER['argv'][] = __DIR__.'/../../../_files/Metadata/Annotation/tests/GroupTest.php';

require_once __DIR__ . '/../../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test files:
17 changes: 17 additions & 0 deletions 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--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--list-test-files';
$_SERVER['argv'][] = '--group';
$_SERVER['argv'][] = 'group';
$_SERVER['argv'][] = __DIR__.'/../../../_files/Metadata/Annotation/tests/';

require_once __DIR__ . '/../../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test files:
- %Annotation%sGroupTest.php
19 changes: 19 additions & 0 deletions 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--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--list-test-files';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__.'/../../_files/basic/configuration.basic.xml';

require_once __DIR__ . '/../../../bootstrap.php';
(new PHPUnit\TextUI\Application)->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

0 comments on commit fa87e27

Please sign in to comment.