Skip to content

Commit

Permalink
Add support for PHPUnit 11.1 --group and --exclude-group to be used
Browse files Browse the repository at this point in the history
multiple times
  • Loading branch information
Slamdunk committed Jun 12, 2024
1 parent 6340430 commit 3178df8
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 29 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"jean85/pretty-package-versions": "^2.0.6",
"phpunit/php-code-coverage": "^10.1.14 || ^11.0.3",
"phpunit/php-file-iterator": "^4.1.0 || ^5.0.0",
"phpunit/php-timer": "^6.0.0 || ^7.0.0",
"phpunit/phpunit": "^10.5.20 || ^11.1.3",
"phpunit/php-timer": "^6.0.0. || ^7.0.0",
"phpunit/phpunit": "^10.5.20 || ^11.2.1",
"sebastian/environment": "^6.1.0 || ^7.1.0",
"symfony/console": "^6.4.7 || ^7.1.0",
"symfony/process": "^6.4.7 || ^7.1.0"
"symfony/console": "^6.4.8 || ^7.1.1",
"symfony/process": "^6.4.8 || ^7.1.1"
},
"require-dev": {
"ext-pcov": "*",
Expand All @@ -57,7 +57,7 @@
"phpstan/phpstan-phpunit": "^1.4.0",
"phpstan/phpstan-strict-rules": "^1.6.0",
"squizlabs/php_codesniffer": "^3.10.1",
"symfony/filesystem": "^6.4.3 || ^7.1.0"
"symfony/filesystem": "^6.4.8 || ^7.1.1"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ parameters:
path: src/Options.php

-
message: "#^Parameter \\#7 \\$phpunitOptions of class ParaTest\\\\Options constructor expects array\\<non\\-empty\\-string, non\\-empty\\-string\\|true\\>, array\\<'bootstrap'\\|'cache\\-directory'\\|'configuration'\\|'coverage\\-filter'\\|'disallow\\-test\\-output'\\|'dont\\-report\\-useless…'\\|'exclude\\-group'\\|'fail\\-on\\-incomplete'\\|'fail\\-on\\-risky'\\|'fail\\-on\\-skipped'\\|'fail\\-on\\-warning'\\|'filter'\\|'group'\\|'no\\-configuration'\\|'order\\-by'\\|'process\\-isolation'\\|'random\\-order\\-seed'\\|'stop\\-on\\-defect'\\|'stop\\-on\\-error'\\|'stop\\-on\\-incomplete'\\|'stop\\-on\\-risky'\\|'stop\\-on\\-skipped'\\|'stop\\-on\\-warning'\\|'strict\\-coverage'\\|'strict\\-global\\-state', non\\-empty\\-array\\|float\\|int\\<min, \\-1\\>\\|int\\<1, max\\>\\|non\\-falsy\\-string\\|true\\> given\\.$#"
count: 1
path: src/Options.php

-
message: "#^Part \\$value \\(array\\|float\\|int\\|string\\) of encapsed string cannot be cast to string\\.$#"
message: "#^Parameter \\#7 \\$phpunitOptions of class ParaTest\\\\Options constructor expects array\\<non\\-empty\\-string, array\\<int, non\\-empty\\-string\\>\\|non\\-empty\\-string\\|true\\>, array\\<'bootstrap'\\|'cache\\-directory'\\|'configuration'\\|'coverage\\-filter'\\|'disallow\\-test\\-output'\\|'dont\\-report\\-useless…'\\|'exclude\\-group'\\|'fail\\-on\\-incomplete'\\|'fail\\-on\\-risky'\\|'fail\\-on\\-skipped'\\|'fail\\-on\\-warning'\\|'filter'\\|'group'\\|'no\\-configuration'\\|'order\\-by'\\|'process\\-isolation'\\|'random\\-order\\-seed'\\|'stop\\-on\\-defect'\\|'stop\\-on\\-error'\\|'stop\\-on\\-incomplete'\\|'stop\\-on\\-risky'\\|'stop\\-on\\-skipped'\\|'stop\\-on\\-warning'\\|'strict\\-coverage'\\|'strict\\-global\\-state', non\\-empty\\-array\\|float\\|int\\<min, \\-1\\>\\|int\\<1, max\\>\\|non\\-falsy\\-string\\|true\\> given\\.$#"
count: 1
path: src/Options.php

Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
failOnEmptyTestSuite="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
cacheDirectory=".build-cache/.phpunit">
<source>
<include>
Expand Down
24 changes: 15 additions & 9 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ final class Options
public readonly bool $needsTeamcity;

/**
* @param non-empty-string $phpunit
* @param non-empty-string $cwd
* @param list<non-empty-string>|null $passthruPhp
* @param array<non-empty-string, non-empty-string|true> $phpunitOptions
* @param non-empty-string $runner
* @param non-empty-string $tmpDir
* @param non-empty-string $phpunit
* @param non-empty-string $cwd
* @param list<non-empty-string>|null $passthruPhp
* @param array<non-empty-string, non-empty-string|true|list<non-empty-string>> $phpunitOptions
* @param non-empty-string $runner
* @param non-empty-string $tmpDir
*/
private function __construct(
public readonly Configuration $configuration,
Expand Down Expand Up @@ -172,7 +172,13 @@ public static function fromConsoleInput(InputInterface $input, string $cwd): sel
continue;
}

$phpunitArgv[] = "--{$key}={$value}";
if (! is_array($value)) {
$value = [$value];
}

foreach ($value as $innerValue) {
$phpunitArgv[] = "--{$key}={$innerValue}";
}
}

if (($path = $input->getArgument('path')) !== null) {
Expand Down Expand Up @@ -306,13 +312,13 @@ public static function setInputDefinition(InputDefinition $inputDefinition): voi
new InputOption(
'group',
null,
InputOption::VALUE_REQUIRED,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'@see PHPUnit guide, chapter: ' . $chapter,
),
new InputOption(
'exclude-group',
null,
InputOption::VALUE_REQUIRED,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'@see PHPUnit guide, chapter: ' . $chapter,
),
new InputOption(
Expand Down
12 changes: 10 additions & 2 deletions src/WrapperRunner/WrapperWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function file_get_contents;
use function filesize;
use function implode;
use function is_array;
use function is_string;
use function serialize;
use function sprintf;
Expand Down Expand Up @@ -112,12 +113,19 @@ public function __construct(
continue;
}

$phpunitArguments[] = "--{$key}";
if ($value === true) {
$phpunitArguments[] = "--{$key}";
continue;
}

$phpunitArguments[] = $value;
if (! is_array($value)) {
$value = [$value];
}

foreach ($value as $innerValue) {
$phpunitArguments[] = "--{$key}";
$phpunitArguments[] = $innerValue;
}
}

$phpunitArguments[] = '--do-not-cache-result';
Expand Down
7 changes: 4 additions & 3 deletions test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ abstract class TestBase extends TestCase
{
/** @var class-string<RunnerInterface> */
protected string $runnerClass = WrapperRunner::class;
/** @var array<string, string|bool|int|null> */
/** @var array<non-empty-string, non-empty-string|list<non-empty-string>|bool|int|null> */
protected array $bareOptions = [];
/** @var non-empty-string */
protected string $tmpDir;

final protected function setUp(): void
Expand All @@ -39,8 +40,8 @@ protected function setUpTest(): void
}

/**
* @param array<string, string|bool|int|null> $argv
* @param non-empty-string|null $cwd
* @param array<non-empty-string, non-empty-string|list<non-empty-string>|bool|int|null> $argv
* @param non-empty-string|null $cwd
*/
final protected function createOptionsFromArgv(array $argv, ?string $cwd = null): Options
{
Expand Down
8 changes: 4 additions & 4 deletions test/Unit/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
final class OptionsTest extends TestBase
{
private Options $options;
/** @var array<string, string> */
/** @var array<non-empty-string, non-empty-string|list<non-empty-string>> */
private array $unfiltered;

public function setUpTest(): void
{
$this->unfiltered = [
'--processes' => '5',
'--group' => 'group1',
'--exclude-group' => 'group2',
'--group' => ['group1'],
'--exclude-group' => ['group2'],
'--bootstrap' => '/path/to/bootstrap',
'path' => '/path/to/tests',
];
Expand All @@ -38,7 +38,7 @@ public function setUpTest(): void

public function testFilteredOptionsShouldContainExtraneousOptions(): void
{
self::assertEquals('group1', $this->options->phpunitOptions['group']);
self::assertEquals(['group1'], $this->options->phpunitOptions['group']);
self::assertEquals('/path/to/bootstrap', $this->options->phpunitOptions['bootstrap']);
}

Expand Down
27 changes: 27 additions & 0 deletions test/Unit/WrapperRunner/WrapperRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,33 @@ public function testLongDataProviderShouldNotFillUnexpectedOutputFile(): void
self::assertEquals(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

#[Group('github')]
#[CoversNothing]
public function testCliOptionsThatCouldBeUsedMultipleTimes(): void
{
$this->bareOptions['--configuration'] = $this->fixture('github' . DIRECTORY_SEPARATOR . 'GH857' . DIRECTORY_SEPARATOR . 'phpunit.xml');
$this->bareOptions['--group'] = [
'one',
'two',
];

$runnerResult = $this->runRunner();

$expectedOutput = <<<'EOF'
Processes: %s
Runtime: PHP %s
Configuration: %s
.. 2 / 2 (100%)
Time: %s, Memory: %s MB
OK%a
EOF;
self::assertStringMatchesFormat($expectedOutput, $runnerResult->output);
self::assertEquals(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

/**
* ### WARNING ###
*
Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/github/GH857/MultipleGroupsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\github\GH857;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/** @internal */
final class MultipleGroupsTest extends TestCase
{
#[Group('one')]
public function testOne(): void
{
self::assertTrue(true);
}

#[Group('two')]
public function testTwo(): void
{
self::assertTrue(true);
}

#[Group('three')]
public function testThree(): void
{
self::assertTrue(true);
}
}
7 changes: 7 additions & 0 deletions test/fixtures/github/GH857/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit>
<testsuites>
<testsuite name="Github issue">
<file>MultipleGroupsTest.php</file>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 3178df8

Please sign in to comment.