如何设置 Laravel Boilerplate phpunit 以在 Homestead 环境中提供代码覆盖率

How to setup Laravel Boilerplate phpunit to provide code coverage in Homestead environment

我有一个运行完美的全新 Laravel Boilerplate running on a Laravel Homestead 环境。 php -v 命令向我返回此信息

PHP 7.1.2-3+deb.sury.org~xenial+1 (cli) (built: Feb 22 2017 10:08:33) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.2-3+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.14.3~linux-x64-non_zts71, https://blackfire.io, by Blackfireio Inc.

我正在搜索如何设置 phpunit 的代码覆盖率以工作,发现我需要 xDebug 才能使其工作。在谷歌上搜索了一下,找到了 xon homestead 命令,它启用了 xDebug 扩展。所以我 运行 它,发现 xDebug 扩展已启用,如新的 php -v 所示:

PHP 7.1.2-3+deb.sury.org~xenial+1 (cli) (built: Feb 22 2017 10:08:33) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.2-3+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans
    with blackfire v1.14.3~linux-x64-non_zts71, https://blackfire.io, by Blackfireio Inc.

我还需要添加 php-code-coverage 与 composer require --dev phpunit/php-code-coverage:4.0.8 的依赖关系(特定版本,因为我在尝试安装最新版本时遇到了一些需求错误)。

然后我 运行 phpunit --coverage-clover=coverage.xml 得到了这个致命错误:

PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

PHP Fatal error:  Uncaught Error: Class 'Breadcrumbs' not found in /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access/User.php:3
Stack trace:
#0 /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access.php(3): require()
#1 /home/vagrant/servirme/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(1083): include_once('/home/vagrant/s...')
#2 /home/vagrant/servirme/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(256): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#3 /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestResult.php(650): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(AccessHelperTest))
#4 /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestCase.php(860): PHPUnit_Framework_TestResult->run(Object(AccessHelperTest))
#5 /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHP in /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access/User.php on line 3

Fatal error: Uncaught Error: Class 'Breadcrumbs' not found in /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access/User.php on line 3

Error: Class 'Breadcrumbs' not found in /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access/User.php on line 3

Call Stack:
    0.0005     350872   1. {main}() /home/vagrant/servirme/vendor/phpunit/phpunit/phpunit:0
    0.0736     562656   2. PHPUnit_TextUI_Command::main() /home/vagrant/servirme/vendor/phpunit/phpunit/phpunit:52
    0.0736     562768   3. PHPUnit_TextUI_Command->run() /home/vagrant/servirme/vendor/phpunit/phpunit/src/TextUI/Command.php:116
    0.3058    1886960   4. PHPUnit_TextUI_TestRunner->doRun() /home/vagrant/servirme/vendor/phpunit/phpunit/src/TextUI/Command.php:186
    0.5521    1993968   5. PHPUnit_Framework_TestSuite->run() /home/vagrant/servirme/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:517
    0.5558    1994968   6. PHPUnit_Framework_TestSuite->run() /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestSuite.php:722
    0.5598    1995536   7. PHPUnit_Framework_TestCase->run() /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestSuite.php:722
    0.5599    1995536   8. PHPUnit_Framework_TestResult->run() /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestCase.php:860
    0.5704    2003832   9. SebastianBergmann\CodeCoverage\CodeCoverage->start() /home/vagrant/servirme/vendor/phpunit/phpunit/src/Framework/TestResult.php:650
    0.5704    2003832  10. SebastianBergmann\CodeCoverage\CodeCoverage->initializeData() /home/vagrant/servirme/vendor/phpunit/php-code-coverage/src/CodeCoverage.php:256
    0.6267    2193768  11. include_once('/home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access.php') /home/vagrant/servirme/vendor/phpunit/php-code-coverage/src/CodeCoverage.php:1083
    0.6278    2194104  12. require('/home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access/User.php') /home/vagrant/servirme/app/Http/Breadcrumbs/Backend/Access.php:3

我感觉这与 Laravel Facades 有关,这让我认为 phpunit 不是 运行 我的自动加载文件,而是属性 bootstrap="bootstrap/autoload.php"默认存在于phpunit.xml个文件中。

不知道从哪里开始,不知道哪里出了问题。

编辑: 我的 Homestead 版本是 5.1.0

新的样板安装带有一个 phpunit.xml 文件。在其中,有一个带有此数据的过滤器标签

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">
            ./app
        </directory>
    </whitelist>
</filter>

我不是 PHPUnit 专家,所以我玩了一下 XML,发现 whitelist 标签导致它崩溃。我又玩了一会儿,发现如果我删除 processUncoveredFilesFromWhitelist="true" 属性,phpunit 命令能够 运行 并生成我需要的代码覆盖率。


请注意。

不幸的是,如果没有代码覆盖测试,过去 运行 需要约 1.5 分钟,而使用此技巧打开代码覆盖,测试需要约 11 分钟。

由于问题与生成代码覆盖率报告有关,我会将其标记为已接受,如果我发现任何可能对遇到与我相同问题的人有帮助的内容,我会保持更新此问题。