diff --git a/composer.json b/composer.json index bdf1930..06c9ba7 100644 --- a/composer.json +++ b/composer.json @@ -31,12 +31,12 @@ "psr/simple-cache": "^3.0.0" }, "require-dev": { - "pestphp/pest": "^5.0.4", + "pestphp/pest": "^5.2.0", "pestphp/pest-dev-tools": "^5.0.0", "pestphp/pest-plugin-type-coverage": "^5.0.2" }, "conflict": { - "pestphp/pest": "<5.0.0" + "pestphp/pest": "<5.2.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 7448eb7..c339798 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,7 +5,7 @@ beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" - cacheResult="false" + recordTestRunHistory="false" colors="true" failOnRisky="true" failOnWarning="true" diff --git a/src/Event/Emitter.php b/src/Event/Emitter.php index 6c891a4..cb42ba7 100644 --- a/src/Event/Emitter.php +++ b/src/Event/Emitter.php @@ -32,9 +32,7 @@ class Emitter public static function instance(): self { - if (! isset(self::$instance)) { - self::$instance = new self; - } + self::$instance ??= new self; return self::$instance; } diff --git a/src/Event/Facade.php b/src/Event/Facade.php index a1b0d73..f6b640e 100644 --- a/src/Event/Facade.php +++ b/src/Event/Facade.php @@ -18,9 +18,7 @@ class Facade public static function instance(): self { - if (! isset(self::$instance)) { - self::$instance = new self; - } + self::$instance ??= new self; return self::$instance; } diff --git a/src/MutationTest.php b/src/MutationTest.php index 7bf0918..436d033 100644 --- a/src/MutationTest.php +++ b/src/MutationTest.php @@ -22,6 +22,13 @@ class MutationTest private ?float $finish = null; + /** + * The test classes covering this mutation. + * + * @var array + */ + private array $coveringTestClasses = []; + private Process $process; public function __construct(public readonly Mutation $mutation) {} @@ -49,12 +56,15 @@ public function start(array $coveredLines, Configuration $configuration, array $ { // TODO: we should pass the tests to run in another way, maybe via cache, mutation or env variable $filters = []; + $coveringTestClasses = []; foreach (range($this->mutation->startLine, $this->mutation->endLine) as $lineNumber) { foreach ($coveredLines[$this->mutation->file->getRealPath()][$lineNumber] ?? [] as $test) { if (preg_match('/\\\\([a-zA-Z0-9]*)::(__pest_evaluable_)?([^#]*)"?/', $test, $matches) !== 1) { continue; } + $coveringTestClasses[] = $this->testClass($test); + if ($matches[2] === '__pest_evaluable_') { $filters[] = $matches[1].'::(.*)'.str_replace(['__', '_'], ['.{1,2}', '.'], $matches[3]); } else { @@ -64,6 +74,8 @@ public function start(array $coveredLines, Configuration $configuration, array $ } $filters = array_unique($filters); + $this->coveringTestClasses = array_values(array_unique($coveringTestClasses)); + if ($filters === []) { $this->updateResult(MutationTestResult::Uncovered); @@ -84,13 +96,10 @@ public function start(array $coveredLines, Configuration $configuration, array $ $envs['LARAVEL_PARALLEL_TESTING'] = '1'; } - // remove coverage arguments from the original arguments - $filteredArguments = array_filter($originalArguments, fn (string $argument): bool => ! str_starts_with($argument, '--coverage')); - // TODO: filter arguments to remove unnecessary stuff (Teamcity, Coverage, etc.) $process = new Process( command: [ - ...$filteredArguments, + ...$this->subprocessArguments($originalArguments), '--bail', '--filter="'.implode('|', $filters).'"', ], @@ -107,6 +116,71 @@ public function start(array $coveredLines, Configuration $configuration, array $ return true; } + /** + * Removes the arguments that only make sense for the run as a whole. + * + * Coverage is not collected again, and the sharding options must not reach a + * mutation's own test run: `--shard` would split the tests covering the mutation + * a second time and run only a part of them, and `--update-shards` would record + * the timings of that partial run. + * + * @param array $arguments + * @return array + */ + private function subprocessArguments(array $arguments): array + { + $filtered = []; + $skipNext = false; + + foreach ($arguments as $argument) { + if ($skipNext) { + $skipNext = false; + + continue; + } + + if ($argument === '--shard') { + $skipNext = true; + + continue; + } + + if ($argument === '--update-shards' || str_starts_with($argument, '--shard=') || str_starts_with($argument, '--coverage')) { + continue; + } + + $filtered[] = $argument; + } + + return $filtered; + } + + /** + * Returns the test classes covering this mutation. + * + * @return array + */ + public function coveringTestClasses(): array + { + return $this->coveringTestClasses; + } + + /** + * Extracts the fully qualified class name from a code coverage test identifier. + * + * The filter above keeps only the last segment of the name, which is all a + * `--filter` pattern needs. Sharding matches against the fully qualified names + * `--list-tests` reports, so it needs the whole thing, minus Pest's `P\` prefix. + */ + private function testClass(string $test): string + { + $separator = strpos($test, '::'); + + $class = $separator === false ? $test : substr($test, 0, $separator); + + return preg_replace('/^P\\\\/', '', $class) ?? $class; + } + private function calculateTimeout(): int { /** @var TelemetryRepository $telemetryRepository */ diff --git a/src/Mutators/Abstract/AbstractFunctionCallUnwrapMutator.php b/src/Mutators/Abstract/AbstractFunctionCallUnwrapMutator.php index 455e28a..b8451e2 100644 --- a/src/Mutators/Abstract/AbstractFunctionCallUnwrapMutator.php +++ b/src/Mutators/Abstract/AbstractFunctionCallUnwrapMutator.php @@ -5,12 +5,12 @@ namespace Pest\Mutate\Mutators\Abstract; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Param; -use PhpParser\Node\VariadicPlaceholder; abstract class AbstractFunctionCallUnwrapMutator extends AbstractMutator { @@ -37,14 +37,16 @@ public static function mutate(Node $node): Node { assert($node instanceof FuncCall); - if ($node->args[0] instanceof VariadicPlaceholder) { + $argument = $node->args[0]; + + if (! $argument instanceof Arg) { return new ArrowFunction([ 'params' => [new Param(new Variable('value'))], 'expr' => new Variable('value'), ]); } - return $node->args[0]->value; + return $argument->value; } abstract public static function functionName(): string; diff --git a/src/Mutators/Removal/RemoveEarlyReturn.php b/src/Mutators/Removal/RemoveEarlyReturn.php index 024e199..d709f7e 100644 --- a/src/Mutators/Removal/RemoveEarlyReturn.php +++ b/src/Mutators/Removal/RemoveEarlyReturn.php @@ -37,14 +37,11 @@ public static function can(Node $node): bool return false; } - $classMethod = $node; - do { - $classMethod = $classMethod->getAttribute('parent'); + $classMethod = $node->getAttribute('parent'); - if ($classMethod instanceof ClassMethod) { - break; - } - } while ($classMethod !== null); + while ($classMethod instanceof Node && ! $classMethod instanceof ClassMethod) { + $classMethod = $classMethod->getAttribute('parent'); + } if (! $classMethod instanceof ClassMethod) { return false; diff --git a/src/Options/LogJsonOption.php b/src/Options/LogJsonOption.php new file mode 100644 index 0000000..6d30c1b --- /dev/null +++ b/src/Options/LogJsonOption.php @@ -0,0 +1,27 @@ +enable(); $this->ensurePrinterIsRegistered(); + // Mutation time does not correlate with test time, so sharded mutation runs get + // their own timings file rather than sharing the one regular runs write. + Shard::useTimingsFile('mutation-shards.json'); + $coverageRequired = array_filter($arguments, fn (string $argument): bool => str_starts_with($argument, '--coverage')) !== []; if ($coverageRequired) { $mutationTestRunner->doNotDisableCodeCoverage(); diff --git a/src/Repositories/ConfigurationRepository.php b/src/Repositories/ConfigurationRepository.php index f31e952..25d0184 100644 --- a/src/Repositories/ConfigurationRepository.php +++ b/src/Repositories/ConfigurationRepository.php @@ -57,9 +57,7 @@ public function globalConfiguration(?string $name = null): GlobalConfiguration { $name ??= $this->profile; - if (! isset($this->globalConfigurations[$name])) { - $this->globalConfigurations[$name] = new GlobalConfiguration; - } + $this->globalConfigurations[$name] ??= new GlobalConfiguration; return $this->globalConfigurations[$name]; } @@ -68,9 +66,7 @@ public function fakeTestConfiguration(?string $name = null): TestConfiguration { $name ??= $this->profile; - if (! isset($this->fakeTestConfigurations[$name])) { - $this->fakeTestConfigurations[$name] = new TestConfiguration; - } + $this->fakeTestConfigurations[$name] ??= new TestConfiguration; return $this->fakeTestConfigurations[$name]; } @@ -109,6 +105,7 @@ classes: $config['classes'] ?? [], mutationId: $config['mutation_id'] ?? null, retry: $config['retry'] ?? false, everything: $config['everything'] ?? false, + logJson: $config['log_json'] ?? null, ); } diff --git a/src/Repositories/MutationRepository.php b/src/Repositories/MutationRepository.php index 07fc90c..8878262 100644 --- a/src/Repositories/MutationRepository.php +++ b/src/Repositories/MutationRepository.php @@ -20,9 +20,7 @@ class MutationRepository public function add(Mutation $mutation): void { - if (! isset($this->tests[$mutation->file->getRealPath()])) { - $this->tests[$mutation->file->getRealPath()] = new MutationTestCollection($mutation->file); - } + $this->tests[$mutation->file->getRealPath()] ??= new MutationTestCollection($mutation->file); $test = new MutationTest($mutation); $this->tests[$mutation->file->getRealPath()]->add($test); @@ -94,6 +92,52 @@ public function slowest(): array return array_slice($allTests, 0, 10); } + /** + * Returns the shard units, one per mutated file. + * + * A file is the unit of mutation work: every mutation belongs to exactly one, and + * the tests covering it must run in the same shard for the result to be honest. + * Sharding test classes instead would regenerate a file's mutations in every shard + * holding one of its covering tests, and report the ones killed elsewhere as escaped. + * + * @return array}> + */ + public function units(string $rootPath): array + { + $units = []; + + foreach ($this->tests as $file => $testCollection) { + $time = 0.0; + $tests = []; + + foreach ($testCollection->tests() as $test) { + $time += $test->duration(); + + foreach ($test->coveringTestClasses() as $class) { + $tests[$class] = true; + } + } + + $units[self::relativePath($file, $rootPath)] = [ + 'time' => round($time, 4), + 'tests' => array_keys($tests), + ]; + } + + return $units; + } + + /** + * Makes a mutated file's path relative to the root, so the units survive being + * written on one machine and read on another. + */ + public static function relativePath(string $file, string $rootPath): string + { + $prefix = rtrim($rootPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; + + return str_starts_with($file, $prefix) ? substr($file, strlen($prefix)) : $file; + } + public function sortByEscapedFirst(): void { uasort($this->tests, fn (MutationTestCollection $a, MutationTestCollection $b): int => $b->hasLastRunEscapedMutation() <=> $a->hasLastRunEscapedMutation()); diff --git a/src/Support/Configuration/AbstractConfiguration.php b/src/Support/Configuration/AbstractConfiguration.php index e174b59..ce74b72 100644 --- a/src/Support/Configuration/AbstractConfiguration.php +++ b/src/Support/Configuration/AbstractConfiguration.php @@ -58,6 +58,8 @@ abstract class AbstractConfiguration implements ConfigurationContract private ?bool $everything = null; + private ?string $logJson = null; + /** * {@inheritDoc} */ @@ -144,6 +146,17 @@ public function profile(bool $profile = true): self return $this; } + /** + * Writes the results of this run to the given file, so that sharded runs can be + * added up into one report afterwards. + */ + public function logJson(string $path): self + { + $this->logJson = $path; + + return $this; + } + public function stopOnUntested(bool $stopOnUntested = true): self { $this->stopOnUntested = $stopOnUntested; @@ -194,7 +207,7 @@ public function everything(): self } /** - * @return array{covered_only?: bool, paths?: string[], paths_to_ignore?: string[], mutators?: class-string[], excluded_mutators?: class-string[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool} + * @return array{covered_only?: bool, paths?: string[], paths_to_ignore?: string[], mutators?: class-string[], excluded_mutators?: class-string[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool, log_json?: string} */ public function toArray(): array { @@ -215,6 +228,7 @@ public function toArray(): array 'mutation_id' => $this->mutationId, 'retry' => $this->retry, 'everything' => $this->everything, + 'log_json' => $this->logJson, ], fn (mixed $value): bool => ! is_null($value)); } diff --git a/src/Support/Configuration/CliConfiguration.php b/src/Support/Configuration/CliConfiguration.php index ef7052e..ee6e84c 100644 --- a/src/Support/Configuration/CliConfiguration.php +++ b/src/Support/Configuration/CliConfiguration.php @@ -13,6 +13,7 @@ use Pest\Mutate\Options\ExceptOption; use Pest\Mutate\Options\IgnoreMinScoreOnZeroMutationsOption; use Pest\Mutate\Options\IgnoreOption; +use Pest\Mutate\Options\LogJsonOption; use Pest\Mutate\Options\MinScoreOption; use Pest\Mutate\Options\MutateOption; use Pest\Mutate\Options\MutationIdOption; @@ -44,6 +45,7 @@ class CliConfiguration extends AbstractConfiguration ParallelOption::class, ProcessesOption::class, ProfileOption::class, + LogJsonOption::class, StopOnUntestedOption::class, StopOnUncoveredOption::class, BailOption::class, @@ -118,6 +120,10 @@ public function fromArguments(array $arguments): array $this->profile($input->getOption(ProfileOption::ARGUMENT) !== 'false'); } + if ($input->hasOption(LogJsonOption::ARGUMENT)) { + $this->logJson((string) $input->getOption(LogJsonOption::ARGUMENT)); // @phpstan-ignore-line + } + if ($_SERVER['COLLISION_PRINTER_PROFILE'] ?? false) { $this->profile(true); unset($_SERVER['COLLISION_PRINTER_PROFILE']); diff --git a/src/Support/Configuration/Configuration.php b/src/Support/Configuration/Configuration.php index 9e16b4f..badab2c 100644 --- a/src/Support/Configuration/Configuration.php +++ b/src/Support/Configuration/Configuration.php @@ -30,5 +30,6 @@ public function __construct( public readonly ?string $mutationId, public readonly bool $retry, public readonly bool $everything, + public readonly ?string $logJson, ) {} } diff --git a/src/Tester/MutationTestRunner.php b/src/Tester/MutationTestRunner.php index b451574..32fb610 100644 --- a/src/Tester/MutationTestRunner.php +++ b/src/Tester/MutationTestRunner.php @@ -12,12 +12,15 @@ use Pest\Mutate\MutationTest; use Pest\Mutate\Plugins\Mutate; use Pest\Mutate\Repositories\ConfigurationRepository; +use Pest\Mutate\Repositories\MutationRepository; use Pest\Mutate\Repositories\TelemetryRepository; use Pest\Mutate\Support\Configuration\Configuration; use Pest\Mutate\Support\FileFinder; use Pest\Mutate\Support\MutationGenerator; +use Pest\Plugins\Shard; use Pest\Support\Container; use Pest\Support\Coverage; +use Pest\TestSuite; use Psr\SimpleCache\CacheInterface; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Data\ProcessedCodeCoverageData; @@ -94,9 +97,10 @@ public function isCodeCoverageRequested(): bool public function run(): int { - Container::getInstance()->get(TelemetryRepository::class)->initialTestSuiteDuration( // @phpstan-ignore-line - microtime(true) - $this->startTime - ); + /** @var TelemetryRepository $telemetryRepository */ + $telemetryRepository = Container::getInstance()->get(TelemetryRepository::class); + + $telemetryRepository->initialTestSuiteDuration($this->initialTestSuiteDuration()); if (! Coverage::isAvailable() || ! file_exists($reportPath = Coverage::getPath())) { Container::getInstance()->get(Printer::class)->reportError('No coverage report found, aborting mutation testing.'); // @phpstan-ignore-line @@ -137,6 +141,12 @@ public function run(): int /** @var MutationGenerator $generator */ $generator = Container::getInstance()->get(MutationGenerator::class); + $rootPath = TestSuite::getInstance()->rootPath; + + // Every file this run considers becomes a shard unit, even when it yields no + // mutation, so that a sharded run can tell a file added since from one it knows. + $units = []; + foreach ($files as $file) { $linesToMutate = []; @@ -149,6 +159,16 @@ public function run(): int $linesToMutate = array_keys($coveredLines[$file->getRealPath()]); } + // The restriction is applied here rather than through `--path` or `--class`, + // because passing either of those lifts the `__pest_mutate_only` group and + // makes every shard run the whole suite instead of only the tests declaring + // `covers()`. + if (! Shard::owns($unit = MutationRepository::relativePath($file->getRealPath(), $rootPath))) { + continue; + } + + $units[$unit] = ['time' => 0.0, 'tests' => []]; + foreach ($generator->generate( file: $file, mutators: $this->getConfiguration()->mutators, @@ -186,6 +206,16 @@ classesToMutate: $this->getConfiguration()->everything ? [] : $this->getConfigur $mutationSuite->repository->saveResults(); + $this->writeJsonLog($mutationSuite); + + // A suite cut short by --bail or --stop-on-* holds partial durations, which would + // make for a badly balanced shards file, so it supplies no units and nothing is + // written. + Shard::useTimings( + $this->stop ? [] : [...$units, ...$mutationSuite->repository->units($rootPath)], + ['suite_time' => round($telemetryRepository->getInitialTestSuiteDuration(), 4)], + ); + Facade::instance()->emitter()->finishMutationSuite($mutationSuite); return $this->isMinScoreIsReached($mutationSuite) ? 0 : 1; @@ -219,6 +249,65 @@ private function coveredLines(ProcessedCodeCoverageData $coverageData): array return $coveredLines; } + /** + * Returns the duration a mutation's test run is measured against. + * + * Each mutation is given a timeout derived from this, so it has to describe the same + * amount of work whether or not the suite is sharded. A shard only runs part of the + * suite, so its own initial run is shorter, and mutations that are killed honestly on + * a full run would be cut off as timeouts instead. The reference duration recorded by + * the unsharded `--update-shards` run is therefore preferred whenever it is longer. + * + * Only ever called once, at the start of the mutation suite: it measures from the + * start of the process, so calling it again later would fold the mutation suite's own + * duration into the reference. + */ + private function initialTestSuiteDuration(): float + { + $measured = microtime(true) - $this->startTime; + + $reference = Shard::metadata()['suite_time'] ?? null; + + if (is_numeric($reference) && (float) $reference > $measured) { + return (float) $reference; + } + + return $measured; + } + + /** + * Writes this run's results, so that sharded runs can be added up afterwards. + * + * Shards own disjoint sets of mutations, so summing the counters of every shard + * reproduces the score of a single unsharded run exactly. + */ + private function writeJsonLog(MutationSuite $mutationSuite): void + { + $path = $this->getConfiguration()->logJson; + + if ($path === null) { + return; + } + + $repository = $mutationSuite->repository; + + $directory = dirname($path); + + if (! is_dir($directory)) { + mkdir($directory, 0755, true); + } + + file_put_contents($path, json_encode([ + 'tested' => $repository->tested(), + 'untested' => $repository->untested(), + 'timeout' => $repository->timedOut(), + 'uncovered' => $repository->uncovered(), + 'not_run' => $repository->notRun(), + 'total' => $repository->total(), + 'score' => round($repository->score(), 2), + ], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)."\n"); + } + private function getConfiguration(): Configuration { return Container::getInstance()->get(ConfigurationRepository::class)->mergedConfiguration(); // @phpstan-ignore-line diff --git a/tests/Unit/MutationGeneratorTest.php b/tests/Unit/MutationGeneratorTest.php index 79c67ed..be3c51e 100644 --- a/tests/Unit/MutationGeneratorTest.php +++ b/tests/Unit/MutationGeneratorTest.php @@ -29,7 +29,7 @@ it('generates mutations for the given file', function (): void { $mutations = $this->generator->generate( - $file = new SplFileInfo(dirname(__DIR__).'/Fixtures/Classes/AgeHelper.php', '', ''), + $file = new SplFileInfo(__DIR__.'/../Fixtures/Classes/AgeHelper.php', '', ''), [GreaterOrEqualToGreater::class, SmallerToSmallerOrEqual::class], [] ); @@ -57,7 +57,7 @@ it('ignores lines with no line coverage', function (): void { $mutations = $this->generator->generate( - new SplFileInfo(dirname(__DIR__).'/Fixtures/Classes/AgeHelper.php', '', ''), + new SplFileInfo(__DIR__.'/../Fixtures/Classes/AgeHelper.php', '', ''), [GreaterOrEqualToGreater::class], [1, 2] ); @@ -69,7 +69,7 @@ it('generates mutations for the given file if it has line coverage', function (): void { $mutations = $this->generator->generate( - new SplFileInfo(dirname(__DIR__).'/Fixtures/Classes/AgeHelper.php', '', ''), + new SplFileInfo(__DIR__.'/../Fixtures/Classes/AgeHelper.php', '', ''), [GreaterOrEqualToGreater::class], [10, 11] ); @@ -81,7 +81,7 @@ it('generates mutations for the given file if it contains the given class', function (array $classes, int $expectedCount): void { $mutations = $this->generator->generate( - file: new SplFileInfo(dirname(__DIR__).'/Fixtures/Classes/AgeHelper.php', '', ''), + file: new SplFileInfo(__DIR__.'/../Fixtures/Classes/AgeHelper.php', '', ''), mutators: [GreaterOrEqualToGreater::class], classesToMutate: $classes, ); @@ -105,7 +105,7 @@ classesToMutate: $classes, it('generates mutations for the given file if it contains the given trait', function (array $classes, int $expectedCount): void { $mutations = $this->generator->generate( - file: new SplFileInfo(dirname(__DIR__).'/Fixtures/Traits/SizeHelperTrait.php', '', ''), + file: new SplFileInfo(__DIR__.'/../Fixtures/Traits/SizeHelperTrait.php', '', ''), mutators: [SmallerToSmallerOrEqual::class], classesToMutate: $classes, ); diff --git a/tests/Unit/MutationTestTest.php b/tests/Unit/MutationTestTest.php new file mode 100644 index 0000000..04e30ed --- /dev/null +++ b/tests/Unit/MutationTestTest.php @@ -0,0 +1,37 @@ +getMethod('testClass'); + + expect($method->invoke($test, $identifier))->toBe($expected); +})->with([ + 'pest test' => ['P\Tests\Unit\FooTest::__pest_evaluable_it_works', 'Tests\Unit\FooTest'], + 'phpunit test' => ['Tests\Unit\FooTest::test_bar', 'Tests\Unit\FooTest'], + 'dataset' => ['P\Tests\Unit\FooTest::__pest_evaluable_it_works#0', 'Tests\Unit\FooTest'], + 'unnamespaced' => ['P\FooTest::test_bar', 'FooTest'], + 'no separator' => ['Tests\Unit\FooTest', 'Tests\Unit\FooTest'], +]); + +it('keeps the options of the whole run away from a mutation\'s own test run', function (): void { + $test = new MutationTest(new Mutation(new SplFileInfo(__FILE__, '', ''), 'id', 'SomeMutator', 1, 2, '', '')); + + $method = new ReflectionClass($test)->getMethod('subprocessArguments'); + + expect($method->invoke($test, [ + 'vendor/bin/pest', '--shard=1/4', '--shard', '2/4', '--update-shards', '--coverage-php=/tmp/c.php', '--compact', + ]))->toBe(['vendor/bin/pest', '--compact']); +}); + +it('has no covering test classes before it starts', function (): void { + $test = new MutationTest(new Mutation(new SplFileInfo(__FILE__, '', ''), 'id', 'SomeMutator', 1, 2, '', '')); + + expect($test->coveringTestClasses())->toBe([]); +}); diff --git a/tests/Unit/Repositories/MutationRepositoryTest.php b/tests/Unit/Repositories/MutationRepositoryTest.php new file mode 100644 index 0000000..a72aa00 --- /dev/null +++ b/tests/Unit/Repositories/MutationRepositoryTest.php @@ -0,0 +1,65 @@ + $coveringTestClasses + */ +$addMutation = function (MutationRepository $repository, string $id, float $duration, array $coveringTestClasses): void { + $file = new SplFileInfo(__FILE__, '', ''); + + $repository->add(new Mutation($file, $id, 'SomeMutator', 1, 2, '', '')); + + $tests = $repository->all()[$file->getRealPath()]->tests(); + $test = end($tests); + + assert($test instanceof MutationTest); + + $reflection = new ReflectionClass($test); + $reflection->getProperty('start')->setValue($test, 0.0); + $reflection->getProperty('finish')->setValue($test, $duration); + $reflection->getProperty('coveringTestClasses')->setValue($test, $coveringTestClasses); +}; + +describe('units', function () use ($addMutation): void { + it('groups the mutations of a file into one unit, with every test covering it', function () use ($addMutation): void { + $repository = new MutationRepository; + + $addMutation($repository, 'a', 2.0, ['Tests\\Unit\\FooTest', 'Tests\\Unit\\BarTest']); + $addMutation($repository, 'b', 0.5, ['Tests\\Unit\\FooTest']); + + expect($repository->units(dirname(__DIR__, 3)))->toBe([ + 'tests/Unit/Repositories/MutationRepositoryTest.php' => [ + 'time' => 2.5, + 'tests' => ['Tests\\Unit\\FooTest', 'Tests\\Unit\\BarTest'], + ], + ]); + }); + + it('keeps a file no test covers, so a shard still owns its mutations', function () use ($addMutation): void { + $repository = new MutationRepository; + + $addMutation($repository, 'a', 0.0, []); + + expect($repository->units(dirname(__DIR__, 3)))->toBe([ + 'tests/Unit/Repositories/MutationRepositoryTest.php' => ['time' => 0.0, 'tests' => []], + ]); + }); + + it('keeps an absolute path when the file is outside the root', function () use ($addMutation): void { + $repository = new MutationRepository; + + $addMutation($repository, 'a', 1.0, ['Tests\\Unit\\FooTest']); + + expect(array_keys($repository->units('/somewhere/else')))->toBe([__FILE__]); + }); + + it('returns nothing when no mutation ran', function (): void { + expect(new MutationRepository()->units('/'))->toBe([]); + }); +});