duanpenpan5796 2017-09-21 11:56
浏览 249

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

I'm having a strange problem on 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:

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

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. 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

The reason for this is because it's not loading the Facade properly and is likely not even booting Laravel.

Has anyone got this to work before? If so how?

I am using a setUp() call, it's defined as such

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.

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.

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.

Am I missing something here? Thank you for your help. Let me know if I'm missing some details you need.

  • 写回答

1条回答 默认 最新

  • dougu1896 2017-09-21 13:51
    关注

    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)

    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.

    <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.

    Hope this helps someone in the future :)

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作