Skip to content

Commit 191477f

Browse files
Refactor
1 parent e0be652 commit 191477f

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

src/Framework/TestRunner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
use PHPUnit\TextUI\Configuration\Configuration;
4343
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
4444
use PHPUnit\Util\GlobalState;
45-
use PHPUnit\Util\PHP\DefaultPhpJobRunner;
4645
use PHPUnit\Util\PHP\PhpJob;
46+
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
4747
use PHPUnit\Util\PHP\PhpProcessException;
4848
use ReflectionClass;
4949
use SebastianBergmann\CodeCoverage\Exception as OriginalCodeCoverageException;
@@ -497,7 +497,7 @@ private function shouldErrorHandlerBeUsed(TestCase $test): bool
497497
*/
498498
private function runTestJob(string $code, Test $test, string $processResultFile): void
499499
{
500-
$_result = (new DefaultPhpJobRunner)->run(new PhpJob($code));
500+
$_result = PhpJobRunnerRegistry::run(new PhpJob($code));
501501

502502
$processResult = '';
503503

src/Runner/PhptTestCase.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@
5555
use PHPUnit\Framework\SelfDescribing;
5656
use PHPUnit\Framework\Test;
5757
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
58-
use PHPUnit\Util\PHP\DefaultPhpJobRunner;
5958
use PHPUnit\Util\PHP\PhpJob;
60-
use PHPUnit\Util\PHP\PhpJobRunner;
59+
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
6160
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
6261
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
6362
use SebastianBergmann\CodeCoverage\ReflectionException;
@@ -78,7 +77,6 @@ final class PhptTestCase implements Reorderable, SelfDescribing, Test
7877
* @psalm-var non-empty-string
7978
*/
8079
private readonly string $filename;
81-
private readonly PhpJobRunner $phpJobRunner;
8280
private string $output = '';
8381

8482
/**
@@ -88,14 +86,13 @@ final class PhptTestCase implements Reorderable, SelfDescribing, Test
8886
*
8987
* @throws Exception
9088
*/
91-
public function __construct(string $filename, ?PhpJobRunner $phpJobRunner = null)
89+
public function __construct(string $filename)
9290
{
9391
if (!is_file($filename)) {
9492
throw new FileDoesNotExistException($filename);
9593
}
9694

97-
$this->filename = $filename;
98-
$this->phpJobRunner = $phpJobRunner ?: new DefaultPhpJobRunner;
95+
$this->filename = $filename;
9996
}
10097

10198
/**
@@ -186,7 +183,7 @@ public function run(): void
186183
);
187184
}
188185

189-
$jobResult = $this->phpJobRunner->run(
186+
$jobResult = PhpJobRunnerRegistry::run(
190187
new PhpJob(
191188
$code,
192189
$this->stringifyIni($phpSettings),
@@ -402,7 +399,7 @@ private function shouldTestBeSkipped(array $sections, array $settings): bool
402399
return false;
403400
}
404401

405-
$jobResult = $this->phpJobRunner->run(
402+
$jobResult = PhpJobRunnerRegistry::run(
406403
new PhpJob(
407404
$this->render($sections['SKIPIF']),
408405
$this->stringifyIni($settings),
@@ -435,7 +432,7 @@ private function runClean(array $sections, bool $collectCoverage): void
435432
return;
436433
}
437434

438-
$this->phpJobRunner->run(
435+
PhpJobRunnerRegistry::run(
439436
new PhpJob(
440437
$this->render($sections['CLEAN']),
441438
$this->settings($collectCoverage),

src/Util/PHP/DefaultPhpJobRunner.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use const PHP_SAPI;
1414
use function array_keys;
1515
use function array_merge;
16+
use function assert;
1617
use function fclose;
1718
use function file_put_contents;
1819
use function fwrite;
@@ -31,22 +32,22 @@
3132
/**
3233
* @internal This class is not covered by the backward compatibility promise for PHPUnit
3334
*/
34-
final class DefaultPhpJobRunner implements PhpJobRunner
35+
final readonly class DefaultPhpJobRunner implements PhpJobRunner
3536
{
36-
private ?string $temporaryFile = null;
37-
3837
/**
3938
* @psalm-return array{stdout: string, stderr: string}
4039
*
4140
* @throws PhpProcessException
4241
*/
4342
public function run(PhpJob $job): array
4443
{
44+
$temporaryFile = null;
45+
4546
if ($job->hasInput()) {
46-
$this->temporaryFile = tempnam(sys_get_temp_dir(), 'phpunit_');
47+
$temporaryFile = tempnam(sys_get_temp_dir(), 'phpunit_');
4748

48-
if ($this->temporaryFile === false ||
49-
file_put_contents($this->temporaryFile, $job->code()) === false) {
49+
if ($temporaryFile === false ||
50+
file_put_contents($temporaryFile, $job->code()) === false) {
5051
throw new PhpProcessException(
5152
'Unable to write temporary file',
5253
);
@@ -62,15 +63,19 @@ public function run(PhpJob $job): array
6263
);
6364
}
6465

65-
return $this->runProcess($job);
66+
assert($temporaryFile !== '');
67+
68+
return $this->runProcess($job, $temporaryFile);
6669
}
6770

6871
/**
72+
* @psalm-param ?non-empty-string $temporaryFile
73+
*
6974
* @psalm-return array{stdout: string, stderr: string}
7075
*
7176
* @throws PhpProcessException
7277
*/
73-
private function runProcess(PhpJob $job): array
78+
private function runProcess(PhpJob $job, ?string $temporaryFile): array
7479
{
7580
$environmentVariables = null;
7681

@@ -102,7 +107,7 @@ private function runProcess(PhpJob $job): array
102107
}
103108

104109
$process = proc_open(
105-
$this->getCommand($job, $this->temporaryFile),
110+
$this->buildCommand($job, $temporaryFile),
106111
$pipeSpec,
107112
$pipes,
108113
null,
@@ -135,23 +140,20 @@ private function runProcess(PhpJob $job): array
135140

136141
proc_close($process);
137142

138-
if ($this->temporaryFile !== null) {
139-
unlink($this->temporaryFile);
143+
if ($temporaryFile !== null) {
144+
unlink($temporaryFile);
140145
}
141146

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

145150
/**
146-
* Returns the command based into the configurations.
147-
*
148-
* @return string[]
151+
* @psalm-return non-empty-list<string>
149152
*/
150-
private function getCommand(PhpJob $job, ?string $file = null): array
153+
private function buildCommand(PhpJob $job, ?string $file): array
151154
{
152155
$runtime = new Runtime;
153-
$command = [];
154-
$command[] = PHP_BINARY;
156+
$command = [PHP_BINARY];
155157
$phpSettings = $job->phpSettings();
156158

157159
if ($runtime->hasPCOV()) {

src/Util/PHP/PhpJobRunnerRegistry.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\PHP;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
14+
*/
15+
final class PhpJobRunnerRegistry
16+
{
17+
private static ?PhpJobRunner $runner = null;
18+
19+
/**
20+
* @psalm-return array{stdout: string, stderr: string}
21+
*/
22+
public static function run(PhpJob $job): array
23+
{
24+
if (self::$runner === null) {
25+
self::$runner = new DefaultPhpJobRunner;
26+
}
27+
28+
return self::$runner->run($job);
29+
}
30+
31+
public static function set(PhpJobRunner $runner): void
32+
{
33+
self::$runner = $runner;
34+
}
35+
}

0 commit comments

Comments
 (0)