JLNNN
JLNNN

Reputation: 3

How to mock, use or override Environment::isCli() in TYPO3 v9 unit tests

I'm trying to get a TYPO3 v8 system updated to TYPO3 v9, but when it comes to unit-testing, I got some errors. I was able to fix some of them on my own but this one here's a very difficult one for me, because unit-testing is somewhat new to me in general.

I already searched the web, the TYPO3 documentation (which seems like the important parts are missing?), asked some friends and tried some things on my own, but nothing helped.

$this->environmentMock = $this->createMock(Environment::class);
$this->environmentMock->expects($this->once())
    ->method("::isCli")
    ->will($this->returnValue(TRUE));

I'm expecting to manually override the static function ::isCli() that comes with the Environment class. If that's not possible, is there any other "workaround", like setting a protected variable or something like that?

Currently this is my error message:

Trying to configure method "::isCli" which cannot be configured because it does not exist, has not been specified, is final, or is static

Thanks in advance!


Update 1:

After using @susis tip, I get the following error when appending the code:

TypeError: Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned

Additional information: My project is just an extension folder with TYPO3 v9 sources required in its own composer.json. No web, no htdocs, just the extension folder.


Update 2:

Here's a full gist of my test file.


Update 3:

Even the debugger isn't helping me in this case, see attached screenshot: xdebug phpstorm applicationcontext environment screenshot


Update 4:

I updated the gist, added the environment vars to the phpunit.xml file and added parent::setUp() to the top of the setUp() method but the error is still the same:

TypeError : Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned
 /Users/xyz/my_redirect/public/typo3/sysext/core/Classes/Core/Environment.php:97
 /Users/xyz/my_redirect/Tests/Unit/Hooks/RequestHandlerHookTest.php:41


Update 5:

I updated the gist and removed the environment settings from the phpunit.xml due to what I've seen that they didn't work either. At this moment, the test is working but I'm still not sure if it's done the right way. Thanks for your help!

Upvotes: 0

Views: 541

Answers (1)

Susi
Susi

Reputation: 718

You can initialize the Environment you want in your tests, for example with:

Environment::initialize(
        Environment::getContext(),
        true,
        false,
        Environment::getProjectPath(),
        Environment::getPublicPath(),
        Environment::getVarPath(),
        Environment::getConfigPath(),
        Environment::getBackendPath() . '/index.php',
        Environment::isWindows() ? 'WINDOWS' : 'UNIX'
    );

This is the same way as it is done in TYPO3 Core tests and allows you to customize the complete environment. If you are using the TYPO3 testing framework / UnitTestCase base classes, you can use the property protected $backupEnvironment = true; to make sure the environment is reset after your test.

For an example, you can have a look at the ResourceCompressorIntegrationTest

Upvotes: 2

Related Questions