Preserve existing PHPUnit configuration files (#2044)

Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Ikki Nishihara
2026-11-20 06:05:43 +09:00
committed by GitHub
parent a07f68bc54
commit d65b04a389
3 changed files with 122 additions and 14 deletions
+51 -13
View File
@@ -11,6 +11,7 @@ use Pest\Plugins\Concerns\HandleArguments;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\TextUI\CliArguments\Builder as CliConfigurationBuilder;
use PHPUnit\TextUI\CliArguments\XmlConfigurationFileFinder;
use RuntimeException;
/**
* @internal
@@ -27,6 +28,9 @@ final class Configuration implements HandlesArguments, Terminable
.DIRECTORY_SEPARATOR
.'resources/base-phpunit.xml';
/** @var array{path: string, dev: int, ino: int}|null */
private ?array $generatedConfiguration = null;
public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--configuration', $arguments) || $this->hasArgument('-c', $arguments) || $this->hasCustomConfigurationFile()) {
@@ -40,19 +44,50 @@ final class Configuration implements HandlesArguments, Terminable
private function fromGeneratedConfigurationFile(): string
{
$path = $this->getTempPhpunitXmlPath();
if (file_exists($path)) {
unlink($path);
$directory = getcwd();
if ($directory === false) {
throw new RuntimeException('Unable to determine the working directory.');
}
$doc = new DOMDocument;
$doc->load(self::BASE_PHPUNIT_FILE);
$contents = $doc->saveXML();
assert(is_string($contents));
assert(is_int(file_put_contents($path, $contents)));
foreach (['.pest.xml', '.pest-fallback.xml', null] as $filename) {
$filename ??= '.pest-'.bin2hex(random_bytes(8)).'.xml';
$path = $directory.DIRECTORY_SEPARATOR.$filename;
$file = @fopen($path, 'xb');
return $path;
if ($file === false) {
continue;
}
try {
$stat = @fstat($file);
if ($stat === false) {
throw new RuntimeException('Unable to identify the temporary PHPUnit configuration file.');
}
$this->generatedConfiguration = ['path' => $path, 'dev' => $stat['dev'], 'ino' => $stat['ino']];
$written = @fwrite($file, $contents);
} finally {
fclose($file);
}
if ($written !== strlen($contents)) {
$this->terminate();
throw new RuntimeException('Unable to write the temporary PHPUnit configuration file.');
}
return $path;
}
throw new RuntimeException('Unable to create a temporary PHPUnit configuration file.');
}
private function hasCustomConfigurationFile(): bool
@@ -63,17 +98,20 @@ final class Configuration implements HandlesArguments, Terminable
return is_string($configurationFile);
}
private function getTempPhpunitXmlPath(): string
{
return getcwd().'/.pest.xml';
}
public function terminate(): void
{
$path = $this->getTempPhpunitXmlPath();
$configuration = $this->generatedConfiguration;
$this->generatedConfiguration = null;
if (file_exists($path)) {
unlink($path);
if ($configuration === null) {
return;
}
clearstatcache();
$stat = @lstat($configuration['path']);
if ($stat !== false && $stat['dev'] === $configuration['dev'] && $stat['ino'] === $configuration['ino']) {
unlink($configuration['path']);
}
}
}
+9 -1
View File
@@ -73,6 +73,14 @@
↓ is marked as todo 3
↓ shouldBeMarkedAsTodo
PASS Tests\Features\Configuration
✓ preserves an existing .pest.xml file with dataset "explicit configuration"
✓ preserves an existing .pest.xml file with dataset "discovered configuration"
✓ preserves an existing .pest.xml file with dataset "generated configuration"
✓ removes the generated configuration when it finishes with dataset "sequential"
✓ removes the generated configuration when it finishes with dataset "parallel"
✓ preserves a replacement for the generated configuration
WARN Tests\Features\Coverage
✓ it has plugin
- it adds coverage if --coverage exist → Coverage is not available
@@ -2545,4 +2553,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1845 passed (4231 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1851 passed (4255 assertions)
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('preserves an existing .pest.xml file', function (array $arguments, bool $hasDefaultConfiguration): void {
$project = Project::withoutGit();
$configuration = $project->path('.pest.xml');
$contents = $arguments === [] ? 'An existing project file.' : (string) file_get_contents($project->path('phpunit.xml'));
$project->write('.pest.xml', $contents);
if (! $hasDefaultConfiguration) {
unlink($project->path('phpunit.xml'));
}
$result = $project->pest('tests/Unit/GreeterTest.php', '--cache-directory', $project->path('.phpunit.cache'), ...$arguments);
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->tally())->toContain('2 passed')
->and(glob($project->path('.pest*.xml')))->toHaveCount(1)
->and($configuration)->toBeFile()
->and(file_get_contents($configuration))->toBe($contents);
})->with([
'explicit configuration' => [['--configuration=.pest.xml'], false],
'discovered configuration' => [[], true],
'generated configuration' => [[], false],
])->group('integration');
test('removes the generated configuration when it finishes', function (array $arguments): void {
$project = Project::withoutGit();
unlink($project->path('phpunit.xml'));
$result = $project->pest('tests/Unit/GreeterTest.php', '--cache-directory', $project->path('.phpunit.cache'), ...$arguments);
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->tally())->toContain('2 passed')
->and(glob($project->path('.pest*.xml')))->toBe([]);
})->with(Project::SEQUENTIAL_AND_PARALLEL)->group('integration');
test('preserves a replacement for the generated configuration', function (): void {
$project = Project::withoutGit();
unlink($project->path('phpunit.xml'));
$project->write('replacement.xml', 'A replacement project file.');
$project->write('tests/Unit/ReplaceConfigurationTest.php', <<<'PHP'
<?php
test('replaces the configuration', function (): void {
expect(rename('replacement.xml', '.pest.xml'))->toBeTrue();
});
PHP);
$result = $project->pest('tests/Unit/ReplaceConfigurationTest.php', '--cache-directory', $project->path('.phpunit.cache'));
expect($result->exitCode)->toBe(0, $result->describe())
->and($project->path('.pest.xml'))->toBeFile()
->and(file_get_contents($project->path('.pest.xml')))->toBe('A replacement project file.');
})->group('integration');