I need to do various automated tests related to a platform's bootstrap process. This means running a number of times a startup procedure that messes with global state (particularly, loading classes and class static initialization), and possibly with different configurations. The PHPUnit @runTestsInSeparateProcesses is an ugly hack ( I'd have implemented it using http://php.net/manual/ro/function.pcntl-fork.php ), but it kinda works.
However, with Codeception I get, for instance:
[InjectionException] Service di is not defined and can't be accessed from a test
https://github.com/Codeception/Codeception/issues/1556
Would there be any way to achieve some kind of process isolation with Codeception?
I have been able to get this functionality by writing a unit test function like this, works like a charm:
$pipe=tempnam('/tmp','runTest');
file_put_contents($pipe,serialize(new \Codeception\Exception\Fail('Separate process did not run cleanly')));
if(!pcntl_fork()){
try {
/* testing code goes here; it will run in the forked process */
} catch (\Exception $e) {
file_put_contents($pipe,serialize($e));
die();
}
file_put_contents($pipe,'');
die();
} else {
pcntl_wait($status);
$se=file_get_contents($pipe);
unlink($pipe);
if($se){
$exception=unserialize($se);
throw $exception;
}
}
Since it works inside Codeception, I think adding such behavior via an annotation comment PHPUnit-style should not be too difficult.
If you find it useful, I managed to make a functional process isolation wrapper for the unit tests:
https://github.com/exteon/WithIsolation
To make it even more perfect, it would need:
$testResult = $this->{$this->name}(...\array_values($testArguments)); in \PHPUnit\Framework\TestCase::runTest()
Most helpful comment
If you find it useful, I managed to make a functional process isolation wrapper for the unit tests:
https://github.com/exteon/WithIsolation
To make it even more perfect, it would need:
$testResult = $this->{$this->name}(...\array_values($testArguments));in\PHPUnit\Framework\TestCase::runTest()