Invalid unserialization in \PHPUnit\Util\PHP\AbstractPhpProcess::processChildResult after @runInSeparateProcess
| Q | A |
|---|---|
| PHPUnit version | 8.5.8 |
| PHP version | 7.3.9 |
| Installation Method | PHAR |
Summary
After running a test with @runInSeparateProcess annotation (@preserveGlogalState disabled), an unserialization error happens.
a:4:{s:10:"testResult";N;s:13:"numAssertions";i:1;s:6:"result";O:28:"PHPUnit\Framework\TestResult":35:{s:36:" PHPUnit\Framework\TestResult passed";a:1:{s:14:"Test::testTest";a:2:{s:6:"result";N;s:4:"size";i:-1;}}s:36:" PHPUnit\Framework\TestResult errors";a:0:{}s:38:" PHPUnit\Framework\TestResult failures";a:0:{}s:38:" PHPUnit\Framework\TestResult warnings";a:0:{}s:44:" PHPUnit\Framework\TestResult notImplemented";a:0:{}s:35:" PHPUnit\Framework\TestResult risky";a:0:{}s:37:" PHPUnit\Framework\TestResult skipped";a:0:{}s:39:" PHPUnit\Framework\TestResult listeners";a:0:{}s:38:" PHPUnit\Framework\TestResult runTests";i:1;s:34:" PHPUnit\Framework\TestResult time";d:0.0036439895629882812;s:42:" PHPUnit\Framework\TestResult topTestSuite";N;s:42:" PHPUnit\Framework\TestResult codeCoverage";N;s:61:" PHPUnit\Framework\TestResult convertDeprecationsToExceptions";b:1;s:55:" PHPUnit\Framework\TestResult convertErrorsToExceptions";b:1;s:56:" PHPUnit\Framework\TestResult convertNoticesToExceptions";b:1;s:57:" PHPUnit\Framework\TestResult convertWarningsToExceptions";b:1;s:34:" PHPUnit\Framework\TestResult stop";b:0;s:41:" PHPUnit\Framework\TestResult stopOnError";b:0;s:43:" PHPUnit\Framework\TestResult stopOnFailure";b:0;s:43:" PHPUnit\Framework\TestResult stopOnWarning";b:0;s:69:" PHPUnit\Framework\TestResult beStrictAboutTestsThatDoNotTestAnything";b:1;s:60:" PHPUnit\Framework\TestResult beStrictAboutOutputDuringTests";b:0;s:61:" PHPUnit\Framework\TestResult beStrictAboutTodoAnnotatedTests";b:0;s:72:" PHPUnit\Framework\TestResult beStrictAboutResourceUsageDuringSmallTests";b:0;s:46:" PHPUnit\Framework\TestResult enforceTimeLimit";b:0;s:50:" PHPUnit\Framework\TestResult timeoutForSmallTests";i:1;s:51:" PHPUnit\Framework\TestResult timeoutForMediumTests";i:10;s:50:" PHPUnit\Framework\TestResult timeoutForLargeTests";i:60;s:41:" PHPUnit\Framework\TestResult stopOnRisky";b:0;s:46:" PHPUnit\Framework\TestResult stopOnIncomplete";b:0;s:43:" PHPUnit\Framework\TestResult stopOnSkipped";b:0;s:44:" PHPUnit\Framework\TestResult lastTestFailed";b:0;s:46:" PHPUnit\Framework\TestResult defaultTimeLimit";i:0;s:42:" PHPUnit\Framework\TestResult stopOnDefect";b:0;s:77:" PHPUnit\Framework\TestResult registerMockObjectsFromTestArgumentsRecursively";b:0;}s:6:"output";s:19:"#!/usr/bin/env php
";}
ErrorException: unserialize(): Error at offset 2278 of 2285 bytes
Current behavior
I've investigated the issue a little bit and found lines with the problem. So, in phpunit-8.5.8.phar/phpunit/Util/PHP/AbstractPhpProcess.php:216 there is a line:
$childResult = \unserialize(\str_replace("#!/usr/bin/env php\n", '', $stdout));
$stdout is a string, that is mentioned above (a:4:{s:10:"testResult";N; and so on ...). Please, take a look at the final part: s:19:"#!/usr/bin/env php\n";. After replacing with \str_replace("#!/usr/bin/env php\n", '', $stdout), this part of the $stdout becomes something like s:19:"";, which causes ErrorException.
I tried to implement a naive fix with replacing the problematic line of code with the code below and it seems to work. At least, the test finishes as expected without any errors
$childResult = \unserialize(\str_replace("s:19:\"#!/usr/bin/env php\n\";", "s:0:\"\";", $stdout));
Thank you for your report.
Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.
Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.
Hi, it might be related to composer or php version. I started experiencing this issue after upgrading composer from version 2.1.5 (php 8.0.9) to composer 2.2.1 (php 8.1.1). Reproducing test case https://github.com/jfilla/phpunit-issue-4443. Thank you for your hard work.
I cannot reproduce this.