TYPO3 constants redefined with @runInSeparateProcess
Running a unit test with @runInSeparateProcess fails with an error:
PHPUnit 5.7.25 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 203 ms, Memory: 8.00MB
There was 1 error:
1) Vendor\Package\Tests\Unit\MyTestCase::myTest
PHPUnit_Framework_Exception: PHP Fatal error: Uncaught RuntimeException: TYPO3_REQUESTTYPE has already been set, cannot be called multiple times in /.../vendor/typo3/cms/typo3/sysext/core/Classes/Core/Bootstrap.php:714
Stack trace:
#0 /.../vendor/nimut/testing-framework/src/TestingFramework/Bootstrap/Bootstrap.php(46): TYPO3\CMS\Core\Core\Bootstrap->setRequestType(6)
#1 /.../vendor/nimut/testing-framework/src/TestingFramework/Bootstrap/AbstractBootstrap.php(76): Nimut\TestingFramework\Bootstrap\Bootstrap->includeAndStartCoreBootstrap()
#2 /.../vendor/nimut/testing-framework/res/Configuration/UnitTestsBootstrap.php(35): Nimut\TestingFramework\Bootstrap\AbstractBootstrap->bootstrapUnitTestSystem()
#3 /.../vendor/nimut/testing-framework/res/Configuration/UnitTestsBootstrap.php(36): {closure}()
#4 -(463): require_once('/var/www/web/ty...')
#5 {main}
thrown in /.../vendor/typo3/cms/typo3/sysext/core/Classes/Core/Bootstrap.php on line 714
/.../vendor/phpunit/phpunit/phpunit:52
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Any idea how to fix this?
Could you fix this issue? I have the same problem since updating to typo3/cms 8.7
Unit tests are not meant to be running in an isolated process. I fear here is nothing we can do for you. What is your use case?
I wondered, why it works for the typo3/testing-framwork and had a closer look into it. It works there, because ->setRequestType is not called for unit test bootstrap.
Here is what phpunit does for isolated tests:
- gather all defined constants of current process and write them as code into a template file
- gather all included files of current process and write them as code into a template file
A new process is started and the generated file is evaluated.
First all constants are defined, then all files are included. Because of the UnitTestBootstrap.php file was among the included files it is included as well. However the TYPO3_REQUESTTYPE was already defined before.
With a bit of luck, it works for typo3/testing-framework because the bootstrap executed there does not re-define constants defined before, but in general such issues could occur there as well.
This would be pretty tough to fix thoroughly. But maybe we should indeed consider not setting request type for unit test bootstrap. no unit test should rely on this constant to be set anyway imho (in general no unit test should rely on global constants, but that is a different story).