Skip to content

Commit

Permalink
Merge pull request #195 from facile-it/self-document-supported-options
Browse files Browse the repository at this point in the history
Drop direct support of PHPUnit options, migrate to `--pass-through`and self-test it
  • Loading branch information
Jean85 authored Mar 3, 2023
2 parents 619606b + 2b44888 commit 0810462
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 232 deletions.
14 changes: 13 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" backupGlobals="false" cacheResult="false" colors="true" bootstrap="vendor/autoload.php" failOnWarning="true" failOnRisky="true" timeoutForSmallTests="1" timeoutForMediumTests="5" timeoutForLargeTests="10" beStrictAboutOutputDuringTests="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
backupGlobals="false"
cacheResult="false"
colors="true"
bootstrap="vendor/autoload.php"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnWarning="true"
failOnRisky="true"
timeoutForSmallTests="1"
timeoutForMediumTests="5"
timeoutForLargeTests="10"
beStrictAboutOutputDuringTests="true"
>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="intl.default_locale" value="en"/>
Expand Down
89 changes: 1 addition & 88 deletions src/Command/ParallelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Paraunit\Configuration\ParallelConfiguration;
use Paraunit\Configuration\PHPUnitConfig;
use Paraunit\Configuration\PHPUnitOption;
use Paraunit\Runner\Runner;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\FormatterHelper;
Expand All @@ -19,52 +18,8 @@

class ParallelCommand extends Command
{
/** @var PHPUnitOption[] */
private readonly array $phpunitOptions;

public function __construct(protected ParallelConfiguration $configuration)
{
$this->phpunitOptions = [
new PHPUnitOption('whitelist'),
new PHPUnitOption('disable-coverage-ignore', false),
new PHPUnitOption('no-coverage', false),

new PHPUnitOption('filter'),
new PHPUnitOption('testsuite'),
new PHPUnitOption('group'),
new PHPUnitOption('exclude-group'),
new PHPUnitOption('test-suffix'),

new PHPUnitOption('dont-report-useless-tests', false),
new PHPUnitOption('strict-coverage', false),
new PHPUnitOption('strict-global-state', false),
new PHPUnitOption('disallow-test-output', false),
new PHPUnitOption('disallow-resource-usage', false),
new PHPUnitOption('enforce-time-limit', false),
new PHPUnitOption('disallow-todo-tests', false),

new PHPUnitOption('fail-on-warning', false),
new PHPUnitOption('fail-on-risky', false),

new PHPUnitOption('process-isolation', false),
new PHPUnitOption('globals-backup', false),
new PHPUnitOption('static-backup', false),

new PHPUnitOption('loader'),
new PHPUnitOption('repeat'),
new PHPUnitOption('printer'),

new PHPUnitOption('do-not-cache-result', false),

new PHPUnitOption('prepend'),
new PHPUnitOption('bootstrap'),
new PHPUnitOption('no-configuration', false),
new PHPUnitOption('no-logging', false),
new PHPUnitOption('no-extensions', false),
new PHPUnitOption('include-path'),
new PHPUnitOption('stderr', false),
];

parent::__construct();
}

Expand All @@ -79,15 +34,7 @@ protected function configure(): void
$this->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
$this->addOption('logo', null, InputOption::VALUE_NONE, 'Print the Shark logo at the top');
$this->addOption('pass-through', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Inject options to be passed directly to the underlying PHPUnit processes');

foreach ($this->phpunitOptions as $option) {
$this->addOption(
$option->getName(),
$option->getShortName(),
$option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
);
}
$this->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Only run tests from the specified test suite');
}

/**
Expand All @@ -101,7 +48,6 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$config = $container->get(PHPUnitConfig::class);
$this->checkForExtension($config, $input, $output);
$this->assertExtensionIsInstalled($config, $input);
$this->addPHPUnitOptions($config, $input);

/** @var Runner $runner */
$runner = $container->get(Runner::class);
Expand Down Expand Up @@ -144,39 +90,6 @@ private function checkForExtension(PHPUnitConfig $config, InputInterface $input,
}
}

private function addPHPUnitOptions(PHPUnitConfig $config, InputInterface $input): PHPUnitConfig
{
foreach ($this->phpunitOptions as $option) {
$cliOption = $input->getOption($option->getName());

if (\is_bool($cliOption)) {
$cliOption = null;
}

if (null !== $cliOption && ! \is_string($cliOption)) {
throw new \InvalidArgumentException('Invalid option format for CLI option ' . $option->getName() . ': ' . gettype($cliOption));
}

if ($this->setOptionValue($option, $cliOption)) {
$config->addPhpunitOption($option);
}
}

return $config;
}

private function setOptionValue(PHPUnitOption $option, ?string $cliOption): bool
{
if (! $cliOption) {
return false;
}
if ($option->hasValue()) {
$option->setValue($cliOption);
}

return true;
}

protected function assertExtensionIsInstalled(PHPUnitConfig $config, InputInterface $input): void
{
if (! $config->isParaunitExtensionRegistered()) {
Expand Down
22 changes: 0 additions & 22 deletions src/Configuration/PHPUnitConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class PHPUnitConfig

private readonly string $configFilename;

/** @var PHPUnitOption[] */
private array $phpunitOptions = [];

private readonly Loader $xmlLoader;

/**
Expand Down Expand Up @@ -117,25 +114,6 @@ public function getBaseDirectory(): string
return dirname($this->configFilename);
}

public function addPhpunitOption(PHPUnitOption $option): void
{
$name = $option->getName();
$this->phpunitOptions[$name] = $option;
}

/**
* @return PHPUnitOption[]
*/
public function getPhpunitOptions(): array
{
return $this->phpunitOptions;
}

public function getPhpunitOption(string $name): ?PHPUnitOption
{
return $this->phpunitOptions[$name] ?? null;
}

/**
* @throws \InvalidArgumentException
*/
Expand Down
46 changes: 0 additions & 46 deletions src/Configuration/PHPUnitOption.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/Configuration/PassThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

class PassThrough
{
private const DISALLOWED_OPTIONS = [
'--no-configuration',
'--no-extensions',
'--no-logging',
'--coverage-php',
'--teamcity',
'--testdox',
'--atleast-version',
'--check-version',
'--generate-configuration',
'--migrate-configuration',
'--list-suites',
'--list-groups',
'--list-tests',
'--list-tests-xml',
];

/** @var list<string> */
public readonly array $options;

Expand All @@ -14,6 +31,12 @@ class PassThrough
*/
public function __construct(?array $options = [])
{
foreach ($options ?? [] as $option) {
if (in_array($option, self::DISALLOWED_OPTIONS)) {
throw new \InvalidArgumentException('Invalid passed-through option: ' . $option);
}
}

$this->options = array_values($options ?? []);
}
}
15 changes: 0 additions & 15 deletions src/Process/CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Paraunit\Configuration\ChunkSize;
use Paraunit\Configuration\PHPUnitBinFile;
use Paraunit\Configuration\PHPUnitConfig;
use Paraunit\Configuration\PHPUnitOption;

class CommandLine
{
Expand Down Expand Up @@ -38,23 +37,9 @@ public function getOptions(PHPUnitConfig $config): array
$options[] = '--configuration=' . $config->getFileFullPath();
}

foreach ($config->getPhpunitOptions() as $phpunitOption) {
$options[] = $this->buildPhpunitOptionString($phpunitOption);
}

return $options;
}

private function buildPhpunitOptionString(PHPUnitOption $option): string
{
$optionString = '--' . $option->getName();
if ($option->hasValue()) {
$optionString .= '=' . $option->getValue();
}

return $optionString;
}

/**
* @return string[]
*/
Expand Down
7 changes: 6 additions & 1 deletion tests/BaseIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ protected function getParameter(string $parameterName): bool|int|float|string
throw new \RuntimeException('Container not ready');
}

protected function loadContainer(): void
/**
* @param list<string> $passThrough
*/
protected function loadContainer(array $passThrough = []): void
{
$input = $this->prophesize(InputInterface::class);
$input->getArgument('stringFilter')
Expand All @@ -151,6 +154,8 @@ protected function loadContainer(): void
->willReturn(1);
$input->getOption('logo')
->willReturn(false);
$input->getOption('pass-through')
->willReturn($passThrough);
$input->getOption(Argument::cetera())
->willReturn(null);
$input->hasParameterOption(Argument::cetera())
Expand Down
27 changes: 23 additions & 4 deletions tests/Functional/Command/ParallelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,33 @@ public function testExecutionWithLogo(): void
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'--logo' => $configurationPath,
'--filter' => 'doNotExecuteAnyTestSoItsFaster',
'--configuration' => $configurationPath,
'--logo' => true,
'stringFilter' => 'doNotExecuteAnyTestSoItsFaster',
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('BBBBbBBBBBBB', $output, 'Shark logo missing');
}

public function testRegressionExecutionWithStringFilter(): void
{
$configurationPath = $this->getConfigForStubs();
$application = new Application();
$application->add(new ParallelCommand(new ParallelConfiguration()));

$command = $application->find('run');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'--configuration' => $configurationPath,
'stringFilter' => 'doNotExecuteAnyTestSoItsFaster',
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString(' 0 tests', $output, 'Filter is not working');
}

public function testExecutionWithDebugEnabled(): void
{
$configurationPath = $this->getConfigForStubs();
Expand Down Expand Up @@ -197,7 +216,7 @@ public function testExecutionWithParametersWithoutValue(): void
'command' => $command->getName(),
'--configuration' => $configurationPath,
'stringFilter' => 'green',
'--dont-report-useless-tests' => true,
'--pass-through' => ['--dont-report-useless-tests'],
]);

$this->assertSame(0, $exitCode);
Expand All @@ -212,7 +231,7 @@ public function testExecutionWithoutConfiguration(): void
$commandTester = new CommandTester($command);
$exitCode = $commandTester->execute([
'command' => $command->getName(),
'--filter' => 'do_not_execute_anything',
'--pass-through' => ['--filter=do_not_execute_anything'],
]);

$output = $commandTester->getDisplay();
Expand Down
Loading

0 comments on commit 0810462

Please sign in to comment.