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 191477f commit 3a3b102
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .psalm/baseline.xml
Expand Up @@ -1011,7 +1011,7 @@
<code><![CDATA[self::$directories]]></code>
</InvalidPropertyAssignmentValue>
</file>
<file src="src/Util/PHP/DefaultPhpJobRunner.php">
<file src="src/Util/PHP/DefaultJobRunner.php">
<DocblockTypeContradiction>
<code><![CDATA[is_array($value)]]></code>
</DocblockTypeContradiction>
Expand Down
8 changes: 4 additions & 4 deletions src/Framework/TestRunner.php
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\PhpJob;
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
use PHPUnit\Util\PHP\Job;
use PHPUnit\Util\PHP\JobRunnerRegistry;
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 = PhpJobRunnerRegistry::run(new PhpJob($code));
$result = JobRunnerRegistry::run(new Job($code));

$processResult = '';

Expand All @@ -510,7 +510,7 @@ private function runTestJob(string $code, Test $test, string $processResultFile)
$this->processChildResult(
$test,
$processResult,
$_result['stderr'],
$result->stderr(),
);
}

Expand Down
22 changes: 11 additions & 11 deletions src/Runner/PhptTestCase.php
Expand Up @@ -55,8 +55,8 @@
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\Test;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\Util\PHP\PhpJob;
use PHPUnit\Util\PHP\PhpJobRunnerRegistry;
use PHPUnit\Util\PHP\Job;
use PHPUnit\Util\PHP\JobRunnerRegistry;
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
use SebastianBergmann\CodeCoverage\ReflectionException;
Expand Down Expand Up @@ -183,8 +183,8 @@ public function run(): void
);
}

$jobResult = PhpJobRunnerRegistry::run(
new PhpJob(
$jobResult = JobRunnerRegistry::run(
new Job(
$code,
$this->stringifyIni($phpSettings),
$environmentVariables,
Expand All @@ -194,7 +194,7 @@ public function run(): void
),
);

$this->output = $jobResult['stdout'] ?? '';
$this->output = $jobResult->stdout();

if (CodeCoverage::instance()->isActive()) {
$coverage = $this->cleanupForCoverage();
Expand Down Expand Up @@ -399,17 +399,17 @@ private function shouldTestBeSkipped(array $sections, array $settings): bool
return false;
}

$jobResult = PhpJobRunnerRegistry::run(
new PhpJob(
$jobResult = JobRunnerRegistry::run(
new Job(
$this->render($sections['SKIPIF']),
$this->stringifyIni($settings),
),
);

if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
if (!strncasecmp('skip', ltrim($jobResult->stdout()), 4)) {
$message = '';

if (preg_match('/^\s*skip\s*(.+)\s*/i', $jobResult['stdout'], $skipMatch)) {
if (preg_match('/^\s*skip\s*(.+)\s*/i', $jobResult->stdout(), $skipMatch)) {
$message = substr($skipMatch[1], 2);
}

Expand All @@ -432,8 +432,8 @@ private function runClean(array $sections, bool $collectCoverage): void
return;
}

PhpJobRunnerRegistry::run(
new PhpJob(
JobRunnerRegistry::run(
new Job(
$this->render($sections['CLEAN']),
$this->settings($collectCoverage),
),
Expand Down
Expand Up @@ -32,14 +32,12 @@
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class DefaultPhpJobRunner implements PhpJobRunner
final readonly class DefaultJobRunner implements JobRunner
{
/**
* @psalm-return array{stdout: string, stderr: string}
*
* @throws PhpProcessException
*/
public function run(PhpJob $job): array
public function run(Job $job): Result
{
$temporaryFile = null;

Expand All @@ -53,7 +51,7 @@ public function run(PhpJob $job): array
);
}

$job = new PhpJob(
$job = new Job(
$job->input(),
$job->phpSettings(),
$job->environmentVariables(),
Expand All @@ -71,11 +69,9 @@ public function run(PhpJob $job): array
/**
* @psalm-param ?non-empty-string $temporaryFile
*
* @psalm-return array{stdout: string, stderr: string}
*
* @throws PhpProcessException
*/
private function runProcess(PhpJob $job, ?string $temporaryFile): array
private function runProcess(Job $job, ?string $temporaryFile): Result
{
$environmentVariables = null;

Expand Down Expand Up @@ -144,13 +140,13 @@ private function runProcess(PhpJob $job, ?string $temporaryFile): array
unlink($temporaryFile);
}

return ['stdout' => $stdout, 'stderr' => $stderr];
return new Result($stdout, $stderr);
}

/**
* @psalm-return non-empty-list<string>
*/
private function buildCommand(PhpJob $job, ?string $file): array
private function buildCommand(Job $job, ?string $file): array
{
$runtime = new Runtime;
$command = [PHP_BINARY];
Expand Down
2 changes: 1 addition & 1 deletion src/Util/PHP/PhpJob.php → src/Util/PHP/Job.php
Expand Up @@ -14,7 +14,7 @@
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class PhpJob
final readonly class Job
{
/**
* @psalm-var non-empty-string
Expand Down
7 changes: 2 additions & 5 deletions src/Util/PHP/PhpJobRunner.php → src/Util/PHP/JobRunner.php
Expand Up @@ -12,10 +12,7 @@
/**
* @internal This interface is not covered by the backward compatibility promise for PHPUnit
*/
interface PhpJobRunner
interface JobRunner
{
/**
* @psalm-return array{stdout: string, stderr: string}
*/
public function run(PhpJob $job): array;
public function run(Job $job): Result;
}
Expand Up @@ -12,23 +12,20 @@
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class PhpJobRunnerRegistry
final class JobRunnerRegistry
{
private static ?PhpJobRunner $runner = null;
private static ?JobRunner $runner = null;

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

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

public static function set(PhpJobRunner $runner): void
public static function set(JobRunner $runner): void
{
self::$runner = $runner;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Util/PHP/Result.php
@@ -0,0 +1,37 @@
<?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;

/**
* @psalm-immutable
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class Result
{
private string $stdout;
private string $stderr;

public function __construct(string $stdout, string $stderr)
{
$this->stdout = $stdout;
$this->stderr = $stderr;
}

public function stdout(): string
{
return $this->stdout;
}

public function stderr(): string
{
return $this->stderr;
}
}
16 changes: 8 additions & 8 deletions tests/unit/Util/PHP/PhpJobTest.php
Expand Up @@ -13,15 +13,15 @@
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;

#[CoversClass(PhpJob::class)]
#[CoversClass(Job::class)]
#[Small]
final class PhpJobTest extends TestCase
{
public function testHasCode(): void
{
$code = 'the-code';

$job = new PhpJob(
$job = new Job(
$code,
[],
[],
Expand All @@ -41,7 +41,7 @@ public function testMayHavePhpSettings(): void
{
$phpSettings = ['foo' => 'bar'];

$job = new PhpJob(
$job = new Job(
'the-code',
$phpSettings,
[],
Expand All @@ -61,7 +61,7 @@ public function testMayHaveEnvironmentVariables(): void
{
$environmentVariables = ['foo' => 'bar'];

$job = new PhpJob(
$job = new Job(
'the-code',
[],
$environmentVariables,
Expand All @@ -81,7 +81,7 @@ public function testMayHaveArguments(): void
{
$arguments = ['foo', 'bar'];

$job = new PhpJob(
$job = new Job(
'the-code',
[],
[],
Expand All @@ -102,7 +102,7 @@ public function testMayHaveInput(): void
{
$input = 'the-input';

$job = new PhpJob(
$job = new Job(
'the-code',
[],
[],
Expand All @@ -120,7 +120,7 @@ public function testMayHaveInput(): void

public function testMayNotHaveInput(): void
{
$job = new PhpJob(
$job = new Job(
'the-code',
[],
[],
Expand All @@ -138,7 +138,7 @@ public function testMayNotHaveInput(): void

public function testMayRedirectErrors(): void
{
$job = new PhpJob(
$job = new Job(
'the-code',
[],
[],
Expand Down

0 comments on commit 3a3b102

Please sign in to comment.