Skip to content

Commit

Permalink
#199 Add support for filtering tests by suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaelecarelle committed Sep 24, 2024
1 parent bd06bb5 commit e39ac8d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<code><![CDATA[$input->getOption('testsuite')]]></code>
<code><![CDATA[$input->getOption('exclude-testsuite')]]></code>
<code><![CDATA[$input->getOption('sort')]]></code>
<code><![CDATA[$input->getOption('test-suffix')]]></code>
</MixedArgument>
</file>
<file src="src/Coverage/CoverageFetcher.php">
Expand Down
1 change: 1 addition & 0 deletions src/Command/ParallelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function configure(): void
$this->addOption('pass-through', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Inject options to be passed directly to the underlying PHPUnit processes');
$this->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Only run tests from the specified test suite');
$this->addOption('exclude-testsuite', null, InputOption::VALUE_REQUIRED, 'Exclude test suite from running');
$this->addOption('test-suffix', null, InputOption::VALUE_REQUIRED, 'Only run tests from the specified suffix');
$this->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Choose in which order to execute the test classes (values: "random" only for now)');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ private function configureServices(ContainerBuilder $container): void
$container->autowire(Filter::class)
->setArgument('$testSuiteFilter', '%paraunit.testsuite%')
->setArgument('$stringFilter', '%paraunit.string_filter%')
->setArgument('$excludeTestSuiteFilter', '%paraunit.exclude_testsuite%');
->setArgument('$excludeTestSuiteFilter', '%paraunit.exclude_testsuite%')
->setArgument('$testSuffix', '%paraunit.test_suffix%');

$container->autowire(RetryParser::class)
->setArgument('$maxRetryCount', '%paraunit.max_retry_count%');
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/ParallelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, In
$containerBuilder->setParameter('paraunit.pass_through', $input->getOption('pass-through'));
$containerBuilder->setParameter('paraunit.sort_order', $input->getOption('sort'));
$containerBuilder->setParameter('paraunit.exclude_testsuite', $input->getOption('exclude-testsuite'));
$containerBuilder->setParameter('paraunit.test_suffix', $input->getOption('test-suffix'));

if ($input->getOption('debug')) {
$this->enableDebugMode($containerBuilder);
Expand Down
11 changes: 8 additions & 3 deletions src/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function __construct(
private readonly PHPUnitConfig $configFile,
private readonly ?string $testSuiteFilter = null,
private readonly ?string $stringFilter = null,
private readonly ?string $excludeTestSuiteFilter = null
private readonly ?string $excludeTestSuiteFilter = null,
private readonly ?string $testSuffix = null
) {
/** @psalm-suppress InternalClass */
$this->xmlLoader = new Loader();
Expand Down Expand Up @@ -64,7 +65,7 @@ public function filterTestFiles(): array
}
}

return $this->filterByString($aggregatedFiles, $this->stringFilter);
return $this->filterByString($aggregatedFiles, $this->stringFilter, $this->testSuffix);
}

private function testSuitePassFilter(\DOMElement $testSuiteNode, string $testSuiteFilter = null): bool
Expand Down Expand Up @@ -175,12 +176,16 @@ private function getDOMNodeAttribute(
*
* @return string[]
*/
private function filterByString(array $aggregatedFiles, ?string $stringFilter): array
private function filterByString(array $aggregatedFiles, ?string $stringFilter, ?string $suffix): array
{
if ($stringFilter !== null) {
$aggregatedFiles = array_filter($aggregatedFiles, fn($value): bool => stripos($value, $stringFilter) !== false);
}

if ($suffix !== null) {
$aggregatedFiles = array_filter($aggregatedFiles, fn($value): bool => str_ends_with($value, (string) $this->testSuffix));
}

return array_values($aggregatedFiles);
}

Expand Down
1 change: 0 additions & 1 deletion tests/Functional/Configuration/PassThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private function getPossibleFutureOptions(): array
{
return [
'--exclude-filter',
'--test-suffix',
'--columns',
'--display-incomplete',
'--display-skipped',
Expand Down
33 changes: 33 additions & 0 deletions tests/Functional/Filter/SuffixFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tests\Functional\Filter;

use Paraunit\Filter\Filter;
use Tests\BaseFunctionalTestCase;

class SuffixFilterTest extends BaseFunctionalTestCase
{
protected function setup(): void
{
$this->setOption('configuration', $this->getStubPath() . DIRECTORY_SEPARATOR . 'phpunit_with_2_testsuites.xml');
$this->setOption('test-suffix', 'GreenTestStub.php');

parent::setup();
}

public function testFilterTestBySuffixFiles(): void
{
/** @var Filter $filter */
$filter = $this->getService(Filter::class);

$files = $filter->filterTestFiles();

$this->assertCount(1, $files);

$fileExploded = explode('/', $files[0]);

$this->assertSame('ThreeGreenTestStub.php', end($fileExploded));
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Configuration/CoverageConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function testBuildContainerWithCoverageSettings(string $inputOption, stri
'pass-through',
'sort',
'exclude-testsuite',
'test-suffix',
];

foreach ($options as $optionName) {
Expand Down Expand Up @@ -207,6 +208,7 @@ public function testBuildContainerWithColoredTextToConsoleCoverage(): void
'pass-through',
'sort',
'exclude-testsuite',
'test-suffix',
];

foreach ($options as $optionName) {
Expand Down

0 comments on commit e39ac8d

Please sign in to comment.