简体   繁体   English

如何在TYPO3 v9单元测试中模拟,使用或覆盖Environment :: isCli()

[英]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. 我试图将TYPO3 v8系统更新为TYPO3 v9,但是在进行单元测试时,出现了一些错误。 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. 我已经在网上搜索了TYPO3文档(似乎缺少重要部分?),问了一些朋友并自己尝试了一些方法,但没有任何帮助。

$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. 我期望手动覆盖Environment类随附的静态函数:: isCli()。 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: 更新1:

After using @susis tip , I get the following error when appending the code: 使用@susis tip之后 ,附加代码时出现以下错误:

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. 附加信息:我的项目只是一个扩展文件夹,其自己的composer.json中包含TYPO3 v9源。 No web, no htdocs, just the extension folder. 没有网络,没有htdocs,只有扩展名文件夹。


Update 2: 更新2:

Here's a full gist of my test file. 这是我测试文件的完整内容


Update 3: 更新3:

Even the debugger isn't helping me in this case, see attached screenshot: xdebug phpstorm applicationcontext environment screenshot 在这种情况下,即使调试器也无法解决问题 ,请参见所附的屏幕截图: xdebug phpstorm applicationcontext环境屏幕截图


Update 4: 更新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: 我更新了要点 ,将环境变量添加到phpunit.xml文件中,并将parent::setUp()添加到setUp()方法的顶部,但是错误仍然相同:

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: 更新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. 由于我发现它们也不起作用,因此我更新了要点,并从phpunit.xml中删除了环境设置。 At this moment, the test is working but I'm still not sure if it's done the right way. 目前,测试正在进行中,但是我仍然不确定测试是否正确。 Thanks for your help! 谢谢你的帮助!

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. 这与在TYPO3 Core测试中完成的方法相同,并且允许您自定义完整的环境。 If you are using the TYPO3 testing framework / UnitTestCase base classes, you can use the property protected $backupEnvironment = true; 如果使用的是TYPO3测试框架/ UnitTestCase基类,则可以使用属性protected $backupEnvironment = true; to make sure the environment is reset after your test. 确保在测试后重置环境。

For an example, you can have a look at the ResourceCompressorIntegrationTest 例如,您可以查看ResourceCompressorIntegrationTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM