Skip to content

Implement TestCase->expectProcessExit($exitCode) #6275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ProcessExitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

final class ProcessExitTest extends TestCase
{
#[RunInSeparateProcess]
#[DataProvider("provideExitCodes")]
public function testOne(?int $expectedExit, int $actualExitCode): void
{
if ($expectedExit !== null) {
$this->expectProcessExit($expectedExit);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a PHPUnit error/warning when expectProcessExit is called in a test, which does not use process-isolation

}

exit($actualExitCode);
}

static public function provideExitCodes():iterable {
yield [null, 0];
yield [null, 1];

yield [0, 0];
yield [0, 1];

yield [1, 1];
yield [1, 0];
}
}
11 changes: 11 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
private bool $outputRetrievedForAssertion = false;
private bool $doesNotPerformAssertions = false;
private bool $expectErrorLog = false;
private ?int $expectProcessExit = null;

/**
* @var list<Comparator>
Expand Down Expand Up @@ -1035,6 +1036,16 @@ final protected function expectOutputString(string $expectedString): void
$this->outputExpectedString = $expectedString;
}

final protected function expectProcessExit(int $exitCode): void
{
$this->expectProcessExit = $exitCode;
}

final public function getExpectedProcessExitCode(): ?int
{
return $this->expectProcessExit;
}

final protected function expectErrorLog(): void
{
$this->expectErrorLog = true;
Expand Down
12 changes: 11 additions & 1 deletion src/Framework/TestRunner/ChildProcessResultProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$this->codeCoverage = $codeCoverage;
}

public function process(Test $test, string $serializedProcessResult, string $stderr): void
public function process(Test $test, string $serializedProcessResult, string $stderr, int $exitCode): void
{
if ($stderr !== '') {
$exception = new Exception(trim($stderr));
Expand Down Expand Up @@ -74,6 +74,16 @@
return;
}

if ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === true) {
assert($test instanceof TestCase);

$test->assertSame($childResult->expectedProcessExit, $exitCode, 'Process exit-code expectation failed');
} elseif ($childResult->expectedProcessExit !== null && $childResult->testCalledExit === false) {
$test->fail('Process expected exit() to be called but test did not call it');

Check failure on line 82 in src/Framework/TestRunner/ChildProcessResultProcessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to an undefined method PHPUnit\Framework\Test::fail().
} elseif ($childResult->expectedProcessExit === null && $childResult->testCalledExit === false) {
$test->fail('Process called exit() but the test did not expect it');

Check failure on line 84 in src/Framework/TestRunner/ChildProcessResultProcessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to an undefined method PHPUnit\Framework\Test::fail().
}
Comment on lines +77 to +85
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need something here which allows us to combine parent-process level assertions with the child process results into a overall result


$this->eventFacade->forward($childResult->events);
$this->passedTests->import($childResult->passedTests);

Expand Down
36 changes: 20 additions & 16 deletions src/Framework/TestRunner/templates/class.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ function __phpunit_run_isolated_test()
$test->setInIsolation(true);

ob_end_clean();
$output = '';
$testCalledExit = true;
register_shutdown_function(function() use ($test, $output, $dispatcher, $testCalledExit) {
file_put_contents(
'{processResultFile}',
serialize(
(object)[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'testCalledExit' => $testCalledExit,
'expectedProcessExit' => $test->getExpectedProcessExitCode(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance(),
]
)
);
});

$test->run();

$output = '';

$testCalledExit = false;
if (!$test->expectsOutput()) {
$output = $test->output();
}
Expand All @@ -98,20 +116,6 @@ function __phpunit_run_isolated_test()
@rewind(STDOUT);
}
}

file_put_contents(
'{processResultFile}',
serialize(
(object)[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
)
);
}

function __phpunit_error_handler($errno, $errstr, $errfile, $errline)
Expand Down
36 changes: 20 additions & 16 deletions src/Framework/TestRunner/templates/method.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ function __phpunit_run_isolated_test()
$test->setInIsolation(true);

ob_end_clean();
$output = '';
$testCalledExit = true;
register_shutdown_function(function() use ($test, $output, $dispatcher, $testCalledExit) {
file_put_contents(
'{processResultFile}',
serialize(
(object)[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'testCalledExit' => $testCalledExit,
'expectedProcessExit' => $test->getExpectedProcessExitCode(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance(),
]
)
);
});

$test->run();

$output = '';

$testCalledExit = false;
if (!$test->expectsOutput()) {
$output = $test->output();
}
Expand All @@ -98,20 +116,6 @@ function __phpunit_run_isolated_test()
@rewind(STDOUT);
}
}

file_put_contents(
'{processResultFile}',
serialize(
(object)[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
)
);
}

function __phpunit_error_handler($errno, $errstr, $errfile, $errline)
Expand Down
8 changes: 7 additions & 1 deletion src/Util/PHP/DefaultJobRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ private function runProcess(Job $job, ?string $temporaryFile): Result
fclose($pipes[2]);
}

$exitCode = 0;
$processStatus = proc_get_status($process);
if ($processStatus['running'] === false) {
$exitCode = $processStatus['exitcode'];
}

proc_close($process);

if ($temporaryFile !== null) {
Expand All @@ -156,7 +162,7 @@ private function runProcess(Job $job, ?string $temporaryFile): Result
assert($stdout !== false);
assert($stderr !== false);

return new Result($stdout, $stderr);
return new Result($stdout, $stderr, $exitCode);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Util/PHP/JobRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final public function runTestJob(Job $job, string $processResultFile, Test $test
$test,
$processResult,
$result->stderr(),
$result->exitCode(),
);

EventFacade::emitter()->childProcessFinished($result->stdout(), $result->stderr());
Expand Down
9 changes: 8 additions & 1 deletion src/Util/PHP/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
{
private string $stdout;
private string $stderr;
private int $exitCode;

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

public function stdout(): string
Expand All @@ -36,4 +38,9 @@ public function stderr(): string
{
return $this->stderr;
}

public function exitCode(): int
{
return $this->exitCode;
}
}
Loading