Files
pest/tests/Features/Tia/CoveragePiggyback.php
T
Tiago Santos 09a36c97f3 fix(tia): drop the coverage cache on structural drift (#1996)
* fix(tia): drop the coverage cache on structural drift

A coverage run cannot rebuild the graph, so a composer.lock change left the previous coverage.bin.gz in place and the next merge failed.

* style(tests): satisfy rector dry-run

* style: remove the source comment

Repository instructions prohibit source comments.

---------

Co-authored-by: Tiago Santos <tsantos@lynixt.pt>
2026-11-03 20:34:22 +05:30

158 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Support\Coverage;
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('a coverage report does not found a dependency graph', function (array $arguments): void {
$project = Project::make('master');
$project->pest('--tia', ...$arguments);
expect($project->graphExists())->toBeFalse();
})->with([
'pest coverage' => [['--coverage']],
'phpunit coverage report' => [['--coverage-text']],
'parallel' => [['--coverage', '--parallel', '--processes=2']],
])->skipOnWindows();
test('a plain run after a coverage run records the whole project scope', function (): void {
$project = Project::make('master');
$project->pest('--tia', '--coverage');
$project->pest('--tia');
$graph = $project->graph();
if ($graph === null) {
expect($project->graphExists())->toBeFalse();
return;
}
expect(array_keys($graph['edges']))->toEqualCanonicalizing(array_keys(Project::EDGES))
->and($graph['files'])->toContain('tests/Unit/CalculatorTest.php')
->and($graph['files'])->toContain('app/Calculator.php');
})->skipOnWindows();
test('xml-configured coverage does not prevent tia recording', function (): void {
$project = Project::make('master');
$project->write('phpunit.xml', <<<'XML_WRAP'
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
failOnRisky="true"
failOnWarning="false"
>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<clover outputFile="coverage/clover.xml" />
</report>
</coverage>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
XML_WRAP);
$result = $project->pest('--tia');
expect($result->exitCode)->toBe(0, $result->describe())
->and($project->graphExists())->toBeTrue()
->and(array_keys($project->graph()['edges']))->toEqualCanonicalizing(array_keys(Project::EDGES));
})->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
test('a coverage report leaves the edges of an existing graph alone', function (): void {
$project = Project::make('master');
$project->seed('master');
$project->pest('--tia', '--coverage');
$delta = $project->delta();
expect($delta->edgesMoved())->toBeFalse($delta->summary())
->and($delta->filesMoved())->toBeFalse($delta->summary())
->and($delta->removed())->toBe(0, $delta->summary())
->and($delta->added())->toBe(0, $delta->summary());
})->skipOnWindows();
test('a coverage run keeps the coverage cache when the fingerprint matches', function (): void {
$project = Project::make('master');
$project->seed('master');
$cache = $project->graphDir().DIRECTORY_SEPARATOR.'coverage.bin.gz';
file_put_contents($cache, 'stale-coverage');
$result = $project->pest('--tia', '--coverage-text');
expect($cache)->toBeFile($result->describe())
->and(file_get_contents($cache))->toBe('stale-coverage');
})->skipOnWindows();
test('a coverage run drops the coverage cache when composer.lock drifts', function (): void {
$project = Project::make('master');
$project->seed('master');
$cache = $project->graphDir().DIRECTORY_SEPARATOR.'coverage.bin.gz';
file_put_contents($cache, 'stale-coverage');
$project->write('composer.lock', (string) json_encode([
'content-hash' => 'drifted-lock',
'packages' => [],
'packages-dev' => [],
]));
$result = $project->pest('--tia', '--coverage-text');
expect($cache)->not->toBeFile($result->describe())
->and($project->graphExists())->toBeTrue($result->describe())
->and($result->output)->toContain('narrows the edges');
})->skipOnWindows();
test('a coverage report adds a new dependency to the edges of an existing graph', function (array $arguments): void {
$project = Project::make('master');
$project->seed('master');
$project->write('app/Greeter.php', <<<'PHP'
<?php
declare(strict_types=1);
namespace Fixture\App;
final class Greeter
{
public function greet(string $name): string
{
(new Calculator)->add(1, 1);
return sprintf('Hello, %s!', $name);
}
}
PHP);
$result = $project->pest('--tia', '--coverage', ...$arguments);
$graph = $project->graph();
$edges = array_map(fn (int $id): string => $graph['files'][$id], $graph['edges']['tests/Unit/GreeterTest.php']);
expect($result->exitCode)->toBe(0, $result->describe())
->and($edges)->toContain('app/Greeter.php')
->and($edges)->toContain('app/Calculator.php');
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');