Skip to content

Commit 3291172

Browse files
The child processes used for process isolation now use temporary files to communicate their result to the parent process
As of PHP 8.3, specifically as of php/php-src#11169, it is no longer possible to capture direct writes to standard output. Such direct writes to standard output interfere with the previous approach where the child process would print its result to standard output from where the parent process would read it. Co-authored-by: Sebastian Bergmann <[email protected]> Co-authored-by: Arne Blankerts <[email protected]>
1 parent 33bbe51 commit 3291172

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

ChangeLog-8.5.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 8.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [8.5.34] - 2023-MM-DD
6+
7+
### Changed
8+
9+
* The child processes used for process isolation now use temporary files to communicate their result to the parent process
10+
511
## [8.5.33] - 2023-02-27
612

713
### Fixed
@@ -268,6 +274,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
268274
* [#3967](https://github.com/sebastianbergmann/phpunit/issues/3967): Cannot double interface that extends interface that extends `\Throwable`
269275
* [#3968](https://github.com/sebastianbergmann/phpunit/pull/3968): Test class run in a separate PHP process are passing when `exit` called inside
270276

277+
[8.5.34]: https://github.com/sebastianbergmann/phpunit/compare/8.5.33...8.5
271278
[8.5.33]: https://github.com/sebastianbergmann/phpunit/compare/8.5.32...8.5.33
272279
[8.5.32]: https://github.com/sebastianbergmann/phpunit/compare/8.5.31...8.5.32
273280
[8.5.31]: https://github.com/sebastianbergmann/phpunit/compare/8.5.30...8.5.31

src/Framework/TestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
use function strlen;
6060
use function strpos;
6161
use function substr;
62+
use function sys_get_temp_dir;
63+
use function tempnam;
6264
use function trim;
6365
use function var_export;
6466
use DeepCopy\DeepCopy;
@@ -801,6 +803,7 @@ public function run(TestResult $result = null): TestResult
801803
$codeCoverageFilter = "'." . $codeCoverageFilter . ".'";
802804

803805
$configurationFilePath = $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] ?? '';
806+
$processResultFile = tempnam(sys_get_temp_dir(), 'phpunit_');
804807

805808
$var = [
806809
'composerAutoload' => $composerAutoload,
@@ -824,6 +827,7 @@ public function run(TestResult $result = null): TestResult
824827
'codeCoverageFilter' => $codeCoverageFilter,
825828
'configurationFilePath' => $configurationFilePath,
826829
'name' => $this->getName(false),
830+
'processResultFile' => $processResultFile,
827831
];
828832

829833
if (!$runEntireClass) {
@@ -833,7 +837,7 @@ public function run(TestResult $result = null): TestResult
833837
$template->setVar($var);
834838

835839
$php = AbstractPhpProcess::factory();
836-
$php->runTestJob($template->render(), $this, $result);
840+
$php->runTestJob($template->render(), $this, $result, $processResultFile);
837841
} else {
838842
$result->run($this);
839843
}

src/Util/PHP/AbstractPhpProcess.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use function array_merge;
1616
use function assert;
1717
use function escapeshellarg;
18+
use function file_exists;
19+
use function file_get_contents;
1820
use function ini_get_all;
1921
use function restore_error_handler;
2022
use function set_error_handler;
@@ -24,6 +26,7 @@
2426
use function strrpos;
2527
use function substr;
2628
use function trim;
29+
use function unlink;
2730
use function unserialize;
2831
use __PHP_Incomplete_Class;
2932
use ErrorException;
@@ -174,16 +177,23 @@ public function getTimeout(): int
174177
*
175178
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
176179
*/
177-
public function runTestJob(string $job, Test $test, TestResult $result): void
180+
public function runTestJob(string $job, Test $test, TestResult $result, string $processResultFile): void
178181
{
179182
$result->startTest($test);
180183

181-
$_result = $this->runJob($job);
184+
$processResult = '';
185+
$_result = $this->runJob($job);
186+
187+
if (file_exists($processResultFile)) {
188+
$processResult = file_get_contents($processResultFile);
189+
190+
@unlink($processResultFile);
191+
}
182192

183193
$this->processChildResult(
184194
$test,
185195
$result,
186-
$_result['stdout'],
196+
$processResult,
187197
$_result['stderr']
188198
);
189199
}

src/Util/PHP/Template/TestCaseClass.tpl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ function __phpunit_run_isolated_test()
7070
}
7171
}
7272

73-
print serialize(
74-
[
75-
'testResult' => $test->getResult(),
76-
'numAssertions' => $test->getNumAssertions(),
77-
'result' => $result,
78-
'output' => $output
79-
]
73+
file_put_contents(
74+
'{processResultFile}',
75+
serialize(
76+
[
77+
'testResult' => $test->getResult(),
78+
'numAssertions' => $test->getNumAssertions(),
79+
'result' => $result,
80+
'output' => $output
81+
]
82+
)
8083
);
8184
}
8285

src/Util/PHP/Template/TestCaseMethod.tpl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ function __phpunit_run_isolated_test()
7373
}
7474
}
7575

76-
print serialize(
77-
[
78-
'testResult' => $test->getResult(),
79-
'numAssertions' => $test->getNumAssertions(),
80-
'result' => $result,
81-
'output' => $output
82-
]
76+
file_put_contents(
77+
'{processResultFile}',
78+
serialize(
79+
[
80+
'testResult' => $test->getResult(),
81+
'numAssertions' => $test->getNumAssertions(),
82+
'result' => $result,
83+
'output' => $output
84+
]
85+
)
8386
);
8487
}
8588

0 commit comments

Comments
 (0)