简体   繁体   English

PhpStorm中的PHPUnit代码覆盖率不会引导Laravel

[英]PHPUnit code coverage in PhpStorm does not bootstrap Laravel

I'm having a strange problem on Laravel 5.5. 我在Laravel 5.5上遇到了一个奇怪的问题。 I'm using PhpStorm and am trying to use it to make a phpunit call on my Vagrant host to PHPUnit like this: 我正在使用PhpStorm,并尝试使用它在我的Vagrant主机上对PHPUnit进行phpunit调用,如下所示:

vagrant:///Users/mymachine/Webdev/project/usr/bin/php -dxdebug.coverage_enable=1 /home/vagrant/project/vendor/phpunit/phpunit/phpunit --coverage-clover /home/vagrant/.phpstorm_helpers/project_AdminLoginTest_coverage --bootstrap /home/vagrant/project/bootstrap/app.php --configuration /home/vagrant/project/phpunit.xml Tests\\Integration\\Authentication\\AdminLoginTest /home/vagrant/project/tests/Integration/Authentication/AdminLoginTest.php --teamcity vagrant:/// Users / mymachine / Webdev / project / usr / bin / php -dxdebug.coverage_enable = 1 / home / vagrant / project / vendor / phpunit / phpunit / phpunit --coverage-clover /home/vagrant/.phpstorm_helpers / project_AdminLoginTest_coverage --bootstrap /home/vagrant/project/bootstrap/app.php --configuration /home/vagrant/project/phpunit.xml测试\\ Integration \\ Authentication \\ AdminLoginTest / home / vagrant / project / tests / Integration / Authentication / AdminLoginTest.php --teamcity

I've defined my default configuration file to be the phpunit.xml file that comes with Laravel by default, and running tests normally without coverage works just fine. 我已经将默认配置文件定义为默认情况下与Laravel一起提供的phpunit.xml文件,并且正常运行测试而没有覆盖范围也可以。 It's whenever I try to make use of code coverage that it starts failing with the following error: 每当我尝试利用代码覆盖率时,它就会因以下错误而开始失败:

Uncaught Error: Class 'Route' not found in /home/vagrant/project/app/routes.php:5 未捕获的错误:在/home/vagrant/project/app/routes.php:5中找不到类“ Route”

The reason for this is because it's not loading the Facade properly and is likely not even booting Laravel. 这样做的原因是因为它无法正确加载Facade,甚至可能无法启动Laravel。

Has anyone got this to work before? 有人有这个工作吗? If so how? 如果可以,怎么办?

I am using a setUp() call, it's defined as such 我正在使用setUp()调用,它的定义如下

protected function setUp()
{
    parent::setUp(); // Must run first, Laravel is set up using this parent call //
    /** @var AdminModel $user */
    $this->user = $this->createAdmin();
}

I do run setUp() from the parent first as that is supposed to call createApplication() which should take care of booting Laravel if I'm not mistaken. 我确实先从父级运行setUp() ,因为它应该调用createApplication() ,如果我没有记错的话, 应该负责启动Laravel。

I've set up Xdebug on my Vagrant machine on both FPM and CLI as well, and PhpStorm successfully reports that it can detect Xdebug 2.5.5 on my Vagrant box via CLI , so I'm ruling that out as a possibility unless I need to install something extra for the Coverage. 我也在FPM和CLI的Vagrant机器上都设置了Xdebug ,PhpStorm成功报告它可以通过CLI在我的Vagrant盒中检测到Xdebug 2.5.5 ,因此除非有必要,否则我将其排除在外为Coverage安装一些额外的东西。

My phpunit.xml also sets up the bootstrap/app.php file in it's bootstrap definition, which is the Laravel default, so that should work fine and the fact that it works without the coverage confuses me. 我的phpunit.xml还在其bootstrap定义中设置了bootstrap/app.php文件,这是Laravel的默认设置,因此应该可以正常工作,而且它在没有覆盖的情况下工作的事实使我感到困惑。

Am I missing something here? 我在这里想念什么吗? Thank you for your help. 谢谢您的帮助。 Let me know if I'm missing some details you need. 如果我缺少您需要的一些详细信息,请告诉我。

After much trial and error i figured it out, so, the problem is that my PHPUnit code coverage in this case is including the routes.php and PHPUnit's XDebug used for the code coverage feature fails to understand the Facade usage in this file (guess it's booted too early? would appreciate clarification if anyone knows why this is) 经过大量的试验和错误后,我发现了问题所在,所以问题在于我的PHPUnit代码覆盖范围包括routes.php和用于代码覆盖功能的PHPUnit的XDebug无法理解此文件中的Facade用法(猜测是太早启动了吗?如果有人知道这是为什么,将不胜感激)

Note that this is only an issue because i have a custom routes.php file inside my App directory, the way to make this work is to just tell PHPUnit to exclude this specific routes file from the Whitelist that Laravel defines. 请注意,这只是一个问题,因为我的App目录中有一个自定义的 route.php文件,完成此工作的方法是告诉PHPUnit从Laravel定义的白名单中排除该特定的路由文件。

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app/API</directory>
        <exclude>
            <file>./app/API/routes.php</file>
        </exclude>
    </whitelist>
</filter>

For me it looks like that, after you exclude that file the Code Coverage should work fine provided that you have XDebug installed, the other Facades are loaded just fine, so i'm guessing the one in routes.php doesn't because that's loaded earlier in the lifecycle of Laravel. 对我来说,在排除该文件后,如果您已安装XDebug,则代码覆盖范围应能正常工作,其他Facades也将正常加载,因此我猜测routes.php中的routes.php不会加载该代码,因为在Laravel生命周期的早期。

Hope this helps someone in the future :) 希望这对以后的人有所帮助:)

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

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