Laravel 5: PHPUnit and no code coverage driver available
Asked Answered
L

9

82

I would like to use PHPUnit to create code coverage reports. I have tried a lot of installation setups found on the web. But nothing seems to work out.

I use the latest version of Laravel 5 (>5.2) and PHPUnit v. 5.0.10. Further, I use MAMP on Mac OS X 10.9.5 running PHP 7.

When I run PHPUnit that is integrated in my Laravel distribution, I receive the following error.

$ vendor/bin/phpunit -v
PHPUnit 5.0.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.0
Configuration: /Applications/MAMP/htdocs/myProject/phpunit.xml
Error:         No code coverage driver is available`

My composer file looks like:

"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "5.0.*",
    "phpunit/php-code-coverage": "^3",
    "symfony/css-selector": "2.8.*|3.0.*",
    "symfony/dom-crawler": "2.8.*|3.0.*"
},

I have also tried the following command:

/Applications/MAMP/bin/php/php7.0.0/bin/phpdbg -qrr ../../../htdocs/myProject/vendor/bin/phpunit -v

This seems to set up the code coverage driver well, but it ends up in an exception:

$ /Applications/MAMP/bin/php/php7.0.0/bin/phpdbg -qrr ../../../htdocs/myProject/vendor/bin/phpunit -v
PHPUnit 5.0.10 by Sebastian Bergmann and contributors.

Runtime:       PHPDBG 7.0.0
Configuration: /Applications/MAMP/htdocs/myProject/phpunit.xml

[PHP Fatal error:  Uncaught ErrorException: include(/Applications/MAMP/htdocs/myProject/app/Exceptions/Handler.php): failed to open stream: Too many open files in /Applications/MAMP/htdocs/myProject/vendor/composer/ClassLoader.php:412
Stack trace:
...

The phpunit.xml looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <logging>
      <log type="coverage-html" target="./tests/codeCoverage" charset="UTF-8"/>
    </logging>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Is it possible to use PHPUnit that comes with the Laravel framework together with code coverage? How should I set it up and use it?

Thanks a lot for your help.

Lapoint answered 5/3, 2016 at 7:43 Comment(0)
S
90

It seems like you are missing the Xdebug extension. If you're using homebrew you can install it like:

brew install php70-xdebug

After that, don't forget to edit your php.ini file to enable the extension.

php -i | grep xdebug

After checking that xdebug is enabled you should be able to do code coverage

Shadchan answered 15/3, 2016 at 12:55 Comment(6)
I used homebrew to install xdebug. How do I enable the extension in php.ini? I enabled the following line zend_extension="/Applications/MAMP/bin/php/php7.0.0/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so". The path is valid. I know it is not the path to xdebug that has been installed by homebrew. But where did homebrew install xdebug to? I can't find it on my system.Lapoint
Use php -i | grep php.ini to see where your loaded configuration php.ini file is. After you have found it, simply add at the end of the file the line you provided above. In my case homebrew installed the extension in zend_extension="/usr/local/opt/php70-xdebug/xdebug.so"Shadchan
It works now. I used the xdebug app that has been installed by homebrew. I actually edited the wrong php.ini file that is located in /Applications/MAMP/conf/php7.0.0. After editing the correct php.ini file located at /Applications/MAMP/bin/php/php7.0.0/conf and restarting the MAMP web server, phpunit now runs the code coverage. Thanks for your help.Lapoint
Obviously this solution is for Mac users only. The rest of us can use yum install php73-xdebug or similar.Wulf
Form xampp users, you'll just need to enable xdebug extension in your php.in (just set the path of zend_extension with some thing like this: /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-XXXXXXXX/xdebug.so), then restart your server, no need to use homebrew or to install anything.Grebe
Homebrew/php tag is deprecated So it's not possible to install xdebug through Homebrew anymore. Please use PECL instead: xdebug.org/docs/install#peclEada
K
32

Update for anyone else stuck;

pecl install xdebug

Keratin answered 30/5, 2018 at 14:38 Comment(2)
you should be add sudo pecl install xdebug. to install this command.Josiejosler
For who are using LAMP with docker - add to your Dockerfile: "RUN pecl install xdebug && docker-php-ext-enable xdebug"Kerakerala
F
13

For windows users:

1) Download xdebug

2) Rename the file to _php_xdebug.dll_ and copy the file to the ext folder in your php installation e.g C:\Program Files (x86)\php\ext

3) Open your php.ini file. For me it’s available at C:\Program Files (x86)\php\php.ini.

4) Paste the below code at the bottom of the file.

zend_extension = php_xdebug.dll
xdebug.remote_enable = 1
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
xdebug.show_local_vars = 1
Fie answered 13/11, 2018 at 15:26 Comment(1)
Need to add this also - xdebug.mode=coverageBoggers
J
7

it's worked for me. follow this step :

1.) install php7.x-dev, (x) is PHP version on your Linux server

sudo apt-get install php7.x-dev

2.) install Xdebug through PECL on Linux

sudo pecl install xdebug

3.) add the following php.ini file. (you can insert it on last line)

zend_extension="/wherever/you/put/it/xdebug.so"
Josiejosler answered 24/3, 2020 at 4:26 Comment(3)
Please don't post only code as an answer, but also include an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality and are more likely to attract upvotes.Willumsen
While this may answer the question, it was flagged for review. Answers with no explanation are often considered low-quality. Please provide some commentary in the answer for why this is the correct answer.Kelcey
okay, thanks for your feedback, i already added some information on my answerJosiejosler
F
6

Update for PHP 7.1

xdebug is essential for code lookup and coverage , so xdebug is must to be installed or enabled in test environment. xdebug in production environment is not suggestible, it will affect performance if you turned on

brew install php71-xdebug

Fiske answered 26/7, 2017 at 10:37 Comment(0)
T
5

As other developers answered, you need to install PHP Xdebug but I want to add new recommend for the developers who are using homestead by default have Xdebug (but it is off) and you can make it ON or OFF

If you want to make it on use below command in homestead

#for on :
xon

#for off:
xoff

Then check php -v and you will see Xdebug in the detail box

Thrash answered 24/7, 2018 at 11:31 Comment(1)
At the time of the post, the work around was the former. Moving forward, this should be the accepted answer and the ones people follow.Cymry
E
4

If you run phpunit inside a vagrant box then you don't need to install xdebug in local and homestead comes with xdebug install automatically. only needs to link homestead xdebug.ini file

Here is the step that worked for me:

cd ~/homestead/REPLACE THIS WITH YOUR HOMESTEAD FOLDER IN LOCAL //
vagrant ssh

sudo ln -s /etc/php/7.2/fpm/conf.d/20-xdebug.ini /etc/php/7.2/cli/conf.d/

In the above command if your running 7.3 or 7.1 then replace it based on your php version

Elbaelbart answered 21/11, 2018 at 6:19 Comment(0)
A
2

To Reproduce, kindly follow the solution(s) below;

  1. Steps to reproduce the behavior if you don't have Xdebug activated already:

    • Inside your project root directory open your CMD terminal and type php -i
    • A list of important information concerning your php will be listed in the CMD terminal
    • Copy and paste the output here https://xdebug.org/wizard
    • After you paste the output, click on the button Analyse my phpinfo() output to proceed
    • Follow the instructions to the latter after your redirect to the Summary page
    • After you are done with the installation, type php -v to confirm that Xdebug is activated successfully.
  2. If you already have Xdebug activated, follow these steps get test coverage running;

    • Type ./vendor/bin/phpunit --coverage-html tests/coverage into the CMD terminal to generate the test coverage
    • After it is done with running the tests successfully, the coverage report of the tests will be in the project_dir/tests/coverage directory
    • Inside the project_dir/tests/coverage directory, click on the index.html file to open and view the general report of the project's phpunit test coverage

NB: I worked in Laravel ^6.0
Tested in Windows 10 environment but can be replicated in other environments

Angio answered 12/1, 2020 at 17:21 Comment(0)
A
0

I am having PHP 7.1.33, Centos 7, PHPUnit 5.7.27 and this works for me

$ yum install gcc
$ pecl install xdebug-2.9.0
Build process completed successfully
Installing '/usr/lib64/php/modules/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.9.0
configuration option "php_ini" is not set to php.ini location
You should add "zend_extension=/usr/lib64/php/modules/xdebug.so" to php.ini

Proceeding after that Find position of php.ini file

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

Add Xdebug config

$ vi /etc/php.ini

Add this at the end

zend_extension=/usr/lib64/php/modules/xdebug.so

Check if Xdebug is installed

$ php -ini|grep 'xdebug support'
xdebug support => enabled
Ability answered 19/1, 2022 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.