Search code examples
laravelphpunitcode-coveragehomesteadphp-code-coverage

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


I have a fresh Laravel Boilerplate running on a Laravel Homestead environment which runs perfect. The php -v command was returning me this info

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.

I was searching how to setup phpunit's code coverage to work, and found that I need xDebug to make it work. Googled a bit more and found the xon homestead command, which enable xDebug extension. So I ran it, and found the xDebug extension was enabled, as a new php -v shows:

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.

Also I needed to add php-code-coverage dependency with composer require --dev phpunit/php-code-coverage:4.0.8 (specific version, because I was getting some requirements errors while trying to install newest one).

Then I ran phpunit --coverage-clover=coverage.xml and got this fatal error:

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

I got a feeling this is related to Laravel Facades, and this lead me to think phpunit isn't running my autoload file, but the attribute bootstrap="bootstrap/autoload.php" exists in phpunit.xml file by default.

Don't know where to go from here, out of ideas of what is wrong.

EDIT: My Homestead version is 5.1.0


Solution

  • The fresh Boilerplate install comes with a phpunit.xml file. Inside it, there is a filter tag with this data

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

    I'm not a PHPUnit expert, so I played a little with the XML, and found that the whitelist tag was causing it to break. I played a little more with it, and found that if I remove the processUncoveredFilesFromWhitelist="true" attribute, phpunit command is able to run and generate code coverage I needed.


    Just a note.

    Unfortunately, without code coverage tests used to take ~1.5 minute to run, and with code coverage turned on with this trick, tests are taking ~11 minutes.

    As the question is related to generate the code coverage report, I'll mark this as accepted, and will keep this question updated if I find anything that might help someone with same problem as me.