Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 19, 2024
1 parent e0be652 commit 191477f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\Util\GlobalState;
use PHPUnit\Util\PHP\DefaultPhpJobRunner;
use PHPUnit\Util\PHP\PhpJob;
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
use PHPUnit\Util\PHP\PhpProcessException;
use ReflectionClass;
use SebastianBergmann\CodeCoverage\Exception as OriginalCodeCoverageException;
Expand Down Expand Up @@ -497,7 +497,7 @@ private function shouldErrorHandlerBeUsed(TestCase $test): bool
*/
private function runTestJob(string $code, Test $test, string $processResultFile): void
{
$_result = (new DefaultPhpJobRunner)->run(new PhpJob($code));
$_result = PhpJobRunnerRegistry::run(new PhpJob($code));

$processResult = '';

Expand Down
15 changes: 6 additions & 9 deletions src/Runner/PhptTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\Test;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\Util\PHP\DefaultPhpJobRunner;
use PHPUnit\Util\PHP\PhpJob;
use PHPUnit\Util\PHP\PhpJobRunner;
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
use SebastianBergmann\CodeCoverage\ReflectionException;
Expand All @@ -78,7 +77,6 @@ final class PhptTestCase implements Reorderable, SelfDescribing, Test
* @psalm-var non-empty-string
*/
private readonly string $filename;
private readonly PhpJobRunner $phpJobRunner;
private string $output = '';

/**
Expand All @@ -88,14 +86,13 @@ final class PhptTestCase implements Reorderable, SelfDescribing, Test
*
* @throws Exception
*/
public function __construct(string $filename, ?PhpJobRunner $phpJobRunner = null)
public function __construct(string $filename)
{
if (!is_file($filename)) {
throw new FileDoesNotExistException($filename);
}

$this->filename = $filename;
$this->phpJobRunner = $phpJobRunner ?: new DefaultPhpJobRunner;
$this->filename = $filename;
}

/**
Expand Down Expand Up @@ -186,7 +183,7 @@ public function run(): void
);
}

$jobResult = $this->phpJobRunner->run(
$jobResult = PhpJobRunnerRegistry::run(
new PhpJob(
$code,
$this->stringifyIni($phpSettings),
Expand Down Expand Up @@ -402,7 +399,7 @@ private function shouldTestBeSkipped(array $sections, array $settings): bool
return false;
}

$jobResult = $this->phpJobRunner->run(
$jobResult = PhpJobRunnerRegistry::run(
new PhpJob(
$this->render($sections['SKIPIF']),
$this->stringifyIni($settings),
Expand Down Expand Up @@ -435,7 +432,7 @@ private function runClean(array $sections, bool $collectCoverage): void
return;
}

$this->phpJobRunner->run(
PhpJobRunnerRegistry::run(
new PhpJob(
$this->render($sections['CLEAN']),
$this->settings($collectCoverage),
Expand Down
36 changes: 19 additions & 17 deletions src/Util/PHP/DefaultPhpJobRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use const PHP_SAPI;
use function array_keys;
use function array_merge;
use function assert;
use function fclose;
use function file_put_contents;
use function fwrite;
Expand All @@ -31,22 +32,22 @@
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class DefaultPhpJobRunner implements PhpJobRunner
final readonly class DefaultPhpJobRunner implements PhpJobRunner
{
private ?string $temporaryFile = null;

/**
* @psalm-return array{stdout: string, stderr: string}
*
* @throws PhpProcessException
*/
public function run(PhpJob $job): array
{
$temporaryFile = null;

if ($job->hasInput()) {
$this->temporaryFile = tempnam(sys_get_temp_dir(), 'phpunit_');
$temporaryFile = tempnam(sys_get_temp_dir(), 'phpunit_');

if ($this->temporaryFile === false ||
file_put_contents($this->temporaryFile, $job->code()) === false) {
if ($temporaryFile === false ||
file_put_contents($temporaryFile, $job->code()) === false) {
throw new PhpProcessException(
'Unable to write temporary file',
);
Expand All @@ -62,15 +63,19 @@ public function run(PhpJob $job): array
);
}

return $this->runProcess($job);
assert($temporaryFile !== '');

return $this->runProcess($job, $temporaryFile);
}

/**
* @psalm-param ?non-empty-string $temporaryFile
*
* @psalm-return array{stdout: string, stderr: string}
*
* @throws PhpProcessException
*/
private function runProcess(PhpJob $job): array
private function runProcess(PhpJob $job, ?string $temporaryFile): array
{
$environmentVariables = null;

Expand Down Expand Up @@ -102,7 +107,7 @@ private function runProcess(PhpJob $job): array
}

$process = proc_open(
$this->getCommand($job, $this->temporaryFile),
$this->buildCommand($job, $temporaryFile),
$pipeSpec,
$pipes,
null,
Expand Down Expand Up @@ -135,23 +140,20 @@ private function runProcess(PhpJob $job): array

proc_close($process);

if ($this->temporaryFile !== null) {
unlink($this->temporaryFile);
if ($temporaryFile !== null) {
unlink($temporaryFile);
}

return ['stdout' => $stdout, 'stderr' => $stderr];
}

/**
* Returns the command based into the configurations.
*
* @return string[]
* @psalm-return non-empty-list<string>
*/
private function getCommand(PhpJob $job, ?string $file = null): array
private function buildCommand(PhpJob $job, ?string $file): array
{
$runtime = new Runtime;
$command = [];
$command[] = PHP_BINARY;
$command = [PHP_BINARY];
$phpSettings = $job->phpSettings();

if ($runtime->hasPCOV()) {
Expand Down
35 changes: 35 additions & 0 deletions src/Util/PHP/PhpJobRunnerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\PHP;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class PhpJobRunnerRegistry
{
private static ?PhpJobRunner $runner = null;

/**
* @psalm-return array{stdout: string, stderr: string}
*/
public static function run(PhpJob $job): array
{
if (self::$runner === null) {
self::$runner = new DefaultPhpJobRunner;
}

return self::$runner->run($job);
}

public static function set(PhpJobRunner $runner): void
{
self::$runner = $runner;
}
}

0 comments on commit 191477f

Please sign in to comment.