'Phpunit @runInSeparateProcess ErrorException: unserialize(): Error at offset 0 of 3 bytes

I am writing a test to check the output(printing the result) of a function, that is changing headers and printing the result.

To simplify it look like this:

function output(){
  //Defining some headers
  //...

  //Printing my result
  echo 'my result';

  exit();
}

In my tests i did something like



class KernelTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */
    public function testOutput(){
       $this->expectOutputString("my result");
       output();
    }
}

But when a run the tests i've got this error:

There was 1 error:

1) KernelTest::testOutput
PHPUnit\Framework\Exception: my result

Caused by
ErrorException: unserialize(): Error at offset 0 of 9 bytes in /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:289
Stack trace:
#0 [internal function]: PHPUnit\Util\PHP\AbstractPhpProcess::PHPUnit\Util\PHP\{closure}(8, 'unserialize(): ...', '/Users/gboutte/D...', 289)
#1 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php(289): unserialize('my result')
#2 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php(187): PHPUnit\Util\PHP\AbstractPhpProcess->processChildResult(Object(KernelTest), Object(PHPUnit\Framework\TestResult), 'my result', '')
#3 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Framework/TestCase.php(901): PHPUnit\Util\PHP\AbstractPhpProcess->runTestJob('<?php\nuse PHPUn...', Object(KernelTest), Object(PHPUnit\Framework\TestResult))
#4 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(678): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult))
#5 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(678): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#6 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(678): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#7 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(670): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#8 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/TextUI/Command.php(143): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\Framework\TestSuite), Array, Array, true)
#9 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/src/TextUI/Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#10 /Users/gboutte/Documents/my-project/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#11 {main}
--

I can reproduce the error with this code:


class KernelTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */
    public function testOutput(){
       $this->expectOutputString("my result");
       echo "my result";
       exit();
    }
}

I added @runInSeparateProcess and @preserveGlobalState disabled in the annotation because in this stackoverflow post that looked similar, their solution was theses annotations.

In the phpunit annotation documentation i also read that:

PHPUnit will attempt to preserve the global state from the parent process by serializing all globals in the parent process and unserializing them in the child process. This can cause problems if the parent process contains globals that are not serializable. To fix this, you can prevent PHPUnit from preserving global state with the @preserveGlobalState annotation.

It look similar to my problem but doesn't fix it.



Solution 1:[1]

By removing the exit() there is no errors. That's not the perfect solution, but it works

Solution 2:[2]

For me, i had processIsolation attribute in my PHPUnit config file equal to true and somehow this configuration caused the problem, when i removed the attribute problem solved, similarly you can set it false to fix the issue like bellow.

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
         processIsolation="false"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gregory Boutte
Solution 2 adnan ahmady