Skip to content

Commit

Permalink
Refactor profiles to configurations and ensure arguments are merge in…
Browse files Browse the repository at this point in the history
… the correct order.
  • Loading branch information
gehrisandro committed Nov 8, 2023
1 parent 33f8cdb commit 06f3e17
Show file tree
Hide file tree
Showing 25 changed files with 425 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Pest\Mutate\Contracts;

interface ProfileFactory
interface Configuration
{
/**
* @param array<int, string>|string ...$paths
Expand Down
8 changes: 1 addition & 7 deletions src/Contracts/MutationTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Pest\Mutate\Contracts;

use Pest\Mutate\Factories\ProfileFactory;

interface MutationTestRunner
{
public function enable(string $profile): void;
public function enable(): void;

public function isEnabled(): bool;

Expand All @@ -19,9 +17,5 @@ public function setOriginalArguments(array $arguments): void;

public function isCodeCoverageRequested(): bool;

public function getProfileFactory(): ProfileFactory;

public function getEnabledProfile(): ?string;

public function run(): void;
}
38 changes: 22 additions & 16 deletions src/Decorators/TestCallDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

namespace Pest\Mutate\Decorators;

use Pest\Mutate\Contracts\Configuration;
use Pest\Mutate\Contracts\MutationTestRunner;
use Pest\Mutate\Factories\ProfileFactory;
use Pest\Mutate\Profile;
use Pest\Mutate\Repositories\ConfigurationRepository;
use Pest\Mutate\Support\Configuration\TestConfiguration;
use Pest\Mutate\Tester\MutationTestRunnerFake;
use Pest\PendingCalls\TestCall;
use Pest\Plugins\Only;
use Pest\Support\Container;

// @codeCoverageIgnoreStart
class TestCallDecorator implements \Pest\Mutate\Contracts\ProfileFactory
class TestCallDecorator implements Configuration
{
private MutationTestRunner $testRunner;

private TestConfiguration $configuration;

public function __construct(private readonly TestCall $testCall)
{
}
Expand All @@ -31,16 +34,24 @@ public function __call(string $name, array $arguments): TestCall

public function mutate(string $profile = 'default'): self
{
if (! str_starts_with($profile, Profile::FAKE)) {
if (! str_starts_with($profile, ConfigurationRepository::FAKE)) {
Only::enable($this->testCall);

$this->testRunner = Container::getInstance() // @phpstan-ignore-line
->get(MutationTestRunner::class);

Container::getInstance()->get(ConfigurationRepository::class)->setProfile($profile); // @phpstan-ignore-line

$this->configuration = Container::getInstance()->get(ConfigurationRepository::class) // @phpstan-ignore-line
->testConfiguration;
} else {
$this->testRunner = new MutationTestRunnerFake();

$this->configuration = Container::getInstance()->get(ConfigurationRepository::class) // @phpstan-ignore-line
->fakeTestConfiguration($profile);
}

$this->testRunner->enable($profile); // @phpstan-ignore-line
$this->testRunner->enable(); // @phpstan-ignore-line

$this->coveredOnly();

Expand All @@ -49,14 +60,14 @@ public function mutate(string $profile = 'default'): self

public function coveredOnly(bool $coveredOnly = true): self
{
$this->_profileFactory()->coveredOnly($coveredOnly);
$this->configuration->coveredOnly($coveredOnly);

return $this;
}

public function min(float $minMSI): self
{
$this->_profileFactory()->min($minMSI);
$this->configuration->min($minMSI);

return $this;
}
Expand All @@ -66,7 +77,7 @@ public function min(float $minMSI): self
*/
public function path(array|string ...$paths): self
{
$this->_profileFactory()->path(...$paths);
$this->configuration->path(...$paths);

return $this;
}
Expand All @@ -76,14 +87,14 @@ public function path(array|string ...$paths): self
*/
public function mutator(string|array ...$mutators): self
{
$this->_profileFactory()->mutator(...$mutators);
$this->configuration->mutator(...$mutators);

return $this;
}

public function parallel(bool $parallel = true): self
{
$this->_profileFactory()->parallel($parallel);
$this->configuration->parallel($parallel);

return $this;
}
Expand All @@ -93,14 +104,9 @@ public function parallel(bool $parallel = true): self
*/
public function class(string|array ...$classes): self
{
$this->_profileFactory()->class(...$classes);
$this->configuration->class(...$classes);

return $this;
}

private function _profileFactory(): ProfileFactory
{
return $this->testRunner->getProfileFactory();
}
}
// @codeCoverageIgnoreEnd
8 changes: 5 additions & 3 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

use Pest\Mutate\Factories\ProfileFactory;
use Pest\Mutate\Repositories\ConfigurationRepository;
use Pest\Mutate\Support\Configuration\GlobalConfiguration;
use Pest\Support\Container;

// @codeCoverageIgnoreStart
if (! function_exists('mutate')) {
Expand All @@ -11,8 +13,8 @@
/**
* Returns a factory to configure the mutation testing profile.
*/
function mutate(string $profile = 'default'): ProfileFactory
function mutate(string $profile = 'default'): GlobalConfiguration
{
return new ProfileFactory($profile);
return Container::getInstance()->get(ConfigurationRepository::class)->globalConfiguration($profile); // @phpstan-ignore-line
}
}
5 changes: 3 additions & 2 deletions src/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Pest\Mutate\Event\Facade;
use Pest\Mutate\Plugins\Mutate;
use Pest\Mutate\Support\Configuration\Configuration;
use Pest\Mutate\Support\MutationTestResult;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
Expand All @@ -32,7 +33,7 @@ public function updateResult(MutationTestResult $result): void
* @param array<string, array<int, array<int, string>>> $coveredLines
* @param array<int, string> $originalArguments
*/
public function run(array $coveredLines, Profile $profile, array $originalArguments): void
public function run(array $coveredLines, Configuration $configuration, array $originalArguments): void
{
/** @var string $tmpfname */
$tmpfname = tempnam('/tmp', 'pest_mutation_');
Expand Down Expand Up @@ -62,7 +63,7 @@ public function run(array $coveredLines, Profile $profile, array $originalArgume
...$originalArguments,
'--bail',
'--filter="'.implode('|', $filters).'"',
$profile->parallel ? '--parallel' : '',
$configuration->parallel ? '--parallel' : '',
],
env: [
Mutate::ENV_MUTATION_TESTING => $this->mutation->file->getRealPath(),
Expand Down
2 changes: 1 addition & 1 deletion src/Options/CoveredOnlyOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static function match(string $argument): bool

public static function inputOption(): InputOption
{
return new InputOption('--covered-only', null, InputOption::VALUE_OPTIONAL, '');
return new InputOption(sprintf('--%s', self::ARGUMENT), null, InputOption::VALUE_OPTIONAL, '');
}
}
2 changes: 1 addition & 1 deletion src/Options/MutateOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static function match(string $argument): bool

public static function inputOption(): InputOption
{
return new InputOption('--mutate', null, InputOption::VALUE_OPTIONAL, '');
return new InputOption(sprintf('--%s', self::ARGUMENT), null, InputOption::VALUE_OPTIONAL, '');
}
}
95 changes: 10 additions & 85 deletions src/Plugins/Mutate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,10 @@
use Pest\Mutate\Boostrappers\BootPhpUnitSubscribers;
use Pest\Mutate\Boostrappers\BootSubscribers;
use Pest\Mutate\Contracts\MutationTestRunner;
use Pest\Mutate\Contracts\Printer;
use Pest\Mutate\Factories\ProfileFactory;
use Pest\Mutate\Options\ClassOption;
use Pest\Mutate\Options\CoveredOnlyOption;
use Pest\Mutate\Options\MinMsiOption;
use Pest\Mutate\Options\MutateOption;
use Pest\Mutate\Options\MutatorsOption;
use Pest\Mutate\Options\ParallelOption;
use Pest\Mutate\Options\PathsOption;
use Pest\Mutate\Profile;
use Pest\Mutate\Support\Printers\CompactPrinter;
use Pest\Mutate\Support\Printers\DefaultPrinter;
use Pest\Mutate\Repositories\ConfigurationRepository;
use Pest\Plugins\Concerns\HandleArguments;
use Pest\Plugins\Parallel;
use Pest\Support\Container;
use Pest\Support\Coverage;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
Expand All @@ -44,16 +29,6 @@ class Mutate implements Bootable, HandlesArguments

final public const ENV_MUTATION_FILE = 'PEST_MUTATION_FILE';

private const OPTIONS = [
MutateOption::class,
PathsOption::class,
MutatorsOption::class,
MinMsiOption::class,
CoveredOnlyOption::class,
ParallelOption::class,
ClassOption::class,
];

/**
* The Kernel bootstrappers.
*
Expand All @@ -68,8 +43,7 @@ class Mutate implements Bootable, HandlesArguments
* Creates a new Plugin instance.
*/
public function __construct(
private readonly Container $container,
private readonly OutputInterface $output
private readonly Container $container
) {
//
}
Expand Down Expand Up @@ -99,26 +73,17 @@ public function handleArguments(array $arguments): array
/** @var \Pest\Mutate\Tester\MutationTestRunner $mutationTestRunner */
$mutationTestRunner = Container::getInstance()->get(MutationTestRunner::class);

$filteredArguments = ['vendor/bin/pest'];
$inputOptions = [];
foreach ($arguments as $key => $argument) {
foreach (self::OPTIONS as $option) {
if ($option::match($argument)) {
$filteredArguments[] = $argument;
$inputOptions[] = $option::inputOption();

if ($option::remove()) {
unset($arguments[$key]);
}
}
}
}
if (array_filter($arguments, fn (string $argument): bool => str_starts_with($argument, '--mutate=') || $argument === '--mutate') === []) {
$mutationTestRunner->setOriginalArguments($arguments);

$originalArguments = $arguments;
return $arguments;
}

$inputDefinition = new InputDefinition($inputOptions);
$arguments = Container::getInstance()->get(ConfigurationRepository::class) // @phpstan-ignore-line
->cliConfiguration->fromArguments($arguments);

$input = new ArgvInput($filteredArguments, $inputDefinition);
$mutationTestRunner->enable();
$mutationTestRunner->setOriginalArguments($arguments);

// always enable php coverage report, but it will be disabled if not required
if (Coverage::isAvailable()) {
Expand All @@ -129,46 +94,6 @@ public function handleArguments(array $arguments): array
$arguments[] = '--coverage-php='.Coverage::getPath();
}

if (! $input->hasOption(MutateOption::ARGUMENT)) {
$mutationTestRunner->setOriginalArguments($originalArguments);

return $arguments;
}

$profileName = $input->getOption(MutateOption::ARGUMENT) ?? 'default';
$profileFactory = new ProfileFactory($profileName); // @phpstan-ignore-line

if (! str_starts_with((string) $profileName, Profile::FAKE)) { // @phpstan-ignore-line
$mutationTestRunner->enable($profileName); // @phpstan-ignore-line
$mutationTestRunner->setOriginalArguments($originalArguments);
}

if ($input->hasOption(PathsOption::ARGUMENT)) {
$profileFactory->path(explode(',', (string) $input->getOption(PathsOption::ARGUMENT))); // @phpstan-ignore-line
}

if ($input->hasOption(MutatorsOption::ARGUMENT)) {
$profileFactory->mutator(explode(',', (string) $input->getOption(MutatorsOption::ARGUMENT))); // @phpstan-ignore-line
}

if ($input->hasOption(MinMsiOption::ARGUMENT)) {
$profileFactory->min((float) $input->getOption(MinMsiOption::ARGUMENT)); // @phpstan-ignore-line
}

if ($input->hasOption(CoveredOnlyOption::ARGUMENT)) {
$profileFactory->coveredOnly($input->getOption(CoveredOnlyOption::ARGUMENT) !== 'false');
}

if ($input->hasOption(ParallelOption::ARGUMENT)) {
unset($arguments[array_search('--'.ParallelOption::ARGUMENT, $arguments, true)]);
$profileFactory->parallel();
Parallel::disable();
}

if ($input->hasOption(ClassOption::ARGUMENT)) {
$profileFactory->class(explode(',', (string) $input->getOption(ClassOption::ARGUMENT))); // @phpstan-ignore-line
}

return $arguments;
}
}
42 changes: 0 additions & 42 deletions src/Profile.php

This file was deleted.

Loading

0 comments on commit 06f3e17

Please sign in to comment.