Skip to content

Commit 94dc922

Browse files
Refactor
1 parent ab1bf62 commit 94dc922

File tree

5 files changed

+91
-55
lines changed

5 files changed

+91
-55
lines changed

src/Framework/TestRunner/SeparateProcessTestRunner.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@
1111

1212
use function assert;
1313
use function defined;
14-
use function file_get_contents;
1514
use function get_include_path;
1615
use function hrtime;
17-
use function is_file;
1816
use function serialize;
1917
use function sys_get_temp_dir;
2018
use function tempnam;
2119
use function unlink;
2220
use function unserialize;
2321
use function var_export;
24-
use PHPUnit\Event\Facade;
2522
use PHPUnit\Event\NoPreviousThrowableException;
2623
use PHPUnit\Runner\CodeCoverage;
27-
use PHPUnit\TestRunner\TestResult\PassedTests;
2824
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
2925
use PHPUnit\Util\GlobalState;
3026
use PHPUnit\Util\PHP\Job;
3127
use PHPUnit\Util\PHP\JobRunnerRegistry;
32-
use PHPUnit\Util\PHP\PhpProcessException;
3328
use ReflectionClass;
3429
use SebastianBergmann\Template\InvalidArgumentException;
3530
use SebastianBergmann\Template\Template;
@@ -144,46 +139,11 @@ public function run(TestCase $test, bool $runEntireClass, bool $preserveGlobalSt
144139

145140
assert($code !== '');
146141

147-
$this->runTestJob($code, $test, $processResultFile);
142+
JobRunnerRegistry::runTestJob(new Job($code), $processResultFile, $test);
148143

149144
@unlink($serializedConfiguration);
150145
}
151146

152-
/**
153-
* @param non-empty-string $code
154-
*
155-
* @throws Exception
156-
* @throws NoPreviousThrowableException
157-
* @throws PhpProcessException
158-
*/
159-
private function runTestJob(string $code, Test $test, string $processResultFile): void
160-
{
161-
$result = JobRunnerRegistry::run(new Job($code));
162-
163-
$processResult = '';
164-
165-
if (is_file($processResultFile)) {
166-
$processResult = file_get_contents($processResultFile);
167-
168-
assert($processResult !== false);
169-
170-
@unlink($processResultFile);
171-
}
172-
173-
$processor = new ChildProcessResultProcessor(
174-
Facade::instance(),
175-
Facade::emitter(),
176-
PassedTests::instance(),
177-
CodeCoverage::instance(),
178-
);
179-
180-
$processor->process(
181-
$test,
182-
$processResult,
183-
$result->stderr(),
184-
);
185-
}
186-
187147
/**
188148
* @throws ProcessIsolationException
189149
*/

src/Util/PHP/DefaultJobRunner.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function assert;
1717
use function fclose;
1818
use function file_put_contents;
19+
use function function_exists;
1920
use function fwrite;
2021
use function ini_get_all;
2122
use function is_array;
@@ -37,7 +38,7 @@
3738
*
3839
* @internal This class is not covered by the backward compatibility promise for PHPUnit
3940
*/
40-
final readonly class DefaultJobRunner implements JobRunner
41+
final readonly class DefaultJobRunner extends JobRunner
4142
{
4243
/**
4344
* @throws PhpProcessException
@@ -178,6 +179,8 @@ private function buildCommand(Job $job, ?string $file): array
178179
),
179180
);
180181
} elseif ($runtime->hasXdebug()) {
182+
assert(function_exists('xdebug_is_debugger_active'));
183+
181184
$xdebugSettings = ini_get_all('xdebug');
182185

183186
assert($xdebugSettings !== false);
@@ -189,11 +192,8 @@ private function buildCommand(Job $job, ?string $file): array
189192
),
190193
);
191194

192-
// disable xdebug if not required to reduce xdebug performance overhead in subprocesses
193-
if (
194-
!CodeCoverage::instance()->isActive() &&
195-
xdebug_is_debugger_active() === false
196-
) {
195+
if (!CodeCoverage::instance()->isActive() &&
196+
xdebug_is_debugger_active() === false) {
197197
$phpSettings['xdebug.mode'] = 'xdebug.mode=off';
198198
}
199199
}

src/Util/PHP/JobRunner.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,50 @@
99
*/
1010
namespace PHPUnit\Util\PHP;
1111

12+
use function assert;
13+
use function file_get_contents;
14+
use function is_file;
15+
use function unlink;
16+
use PHPUnit\Framework\ChildProcessResultProcessor;
17+
use PHPUnit\Framework\Test;
18+
1219
/**
1320
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1421
*
15-
* @internal This interface is not covered by the backward compatibility promise for PHPUnit
22+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
1623
*/
17-
interface JobRunner
24+
abstract readonly class JobRunner
1825
{
19-
public function run(Job $job): Result;
26+
private ChildProcessResultProcessor $processor;
27+
28+
public function __construct(ChildProcessResultProcessor $processor)
29+
{
30+
$this->processor = $processor;
31+
}
32+
33+
/**
34+
* @param non-empty-string $processResultFile
35+
*/
36+
final public function runTestJob(Job $job, string $processResultFile, Test $test): void
37+
{
38+
$result = $this->run($job);
39+
40+
$processResult = '';
41+
42+
if (is_file($processResultFile)) {
43+
$processResult = file_get_contents($processResultFile);
44+
45+
assert($processResult !== false);
46+
47+
@unlink($processResultFile);
48+
}
49+
50+
$this->processor->process(
51+
$test,
52+
$processResult,
53+
$result->stderr(),
54+
);
55+
}
56+
57+
abstract public function run(Job $job): Result;
2058
}

src/Util/PHP/JobRunnerRegistry.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
namespace PHPUnit\Util\PHP;
1111

12+
use PHPUnit\Event\Facade;
13+
use PHPUnit\Framework\ChildProcessResultProcessor;
14+
use PHPUnit\Framework\Test;
15+
use PHPUnit\Runner\CodeCoverage;
16+
use PHPUnit\TestRunner\TestResult\PassedTests;
17+
1218
/**
1319
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1420
*
@@ -20,15 +26,35 @@ final class JobRunnerRegistry
2026

2127
public static function run(Job $job): Result
2228
{
23-
if (self::$runner === null) {
24-
self::$runner = new DefaultJobRunner;
25-
}
29+
return self::runner()->run($job);
30+
}
2631

27-
return self::$runner->run($job);
32+
/**
33+
* @param non-empty-string $processResultFile
34+
*/
35+
public static function runTestJob(Job $job, string $processResultFile, Test $test): void
36+
{
37+
self::runner()->runTestJob($job, $processResultFile, $test);
2838
}
2939

3040
public static function set(JobRunner $runner): void
3141
{
3242
self::$runner = $runner;
3343
}
44+
45+
private static function runner(): JobRunner
46+
{
47+
if (self::$runner === null) {
48+
self::$runner = new DefaultJobRunner(
49+
new ChildProcessResultProcessor(
50+
Facade::instance(),
51+
Facade::emitter(),
52+
PassedTests::instance(),
53+
CodeCoverage::instance(),
54+
),
55+
);
56+
}
57+
58+
return self::$runner;
59+
}
3460
}

tests/unit/Util/PHP/DefaultJobRunnerTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
use const PHP_VERSION;
1313
use function version_compare;
1414
use Generator;
15+
use PHPUnit\Event\Emitter;
16+
use PHPUnit\Event\Facade;
1517
use PHPUnit\Framework\Attributes\CoversClass;
1618
use PHPUnit\Framework\Attributes\DataProvider;
1719
use PHPUnit\Framework\Attributes\Small;
1820
use PHPUnit\Framework\Attributes\UsesClass;
21+
use PHPUnit\Framework\ChildProcessResultProcessor;
1922
use PHPUnit\Framework\TestCase;
23+
use PHPUnit\Runner\CodeCoverage;
24+
use PHPUnit\TestRunner\TestResult\PassedTests;
2025

2126
#[CoversClass(DefaultJobRunner::class)]
2227
#[UsesClass(Job::class)]
@@ -114,7 +119,14 @@ public static function provider(): Generator
114119
#[DataProvider('provider')]
115120
public function testRunsJobInSeparateProcess(Result $expected, Job $job): void
116121
{
117-
$jobRunner = new DefaultJobRunner;
122+
$jobRunner = new DefaultJobRunner(
123+
new ChildProcessResultProcessor(
124+
new Facade,
125+
$this->createStub(Emitter::class),
126+
new PassedTests,
127+
new CodeCoverage,
128+
),
129+
);
118130

119131
$result = $jobRunner->run($job);
120132

0 commit comments

Comments
 (0)