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.
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
Update for anyone else stuck;
pecl install xdebug
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
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
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
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"
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
To Reproduce, kindly follow the solution(s) below;
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.
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
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
Related
I'm just starting with Continuous Integration using Travis CI.
I have this file at the TLD of my repo:
sudo: false
language: php
php:
- '7.1'
This is how Travis CI sees my config:
{
"os": "linux",
"php": "7.1",
"dist": "trusty",
"sudo": false,
"group": "stable",
"language": "php",
}
Why is the build failing with a phpunit error? Could it be because of another .travis.yml file within the repo?
Network availability confirmed.
...edited for brevity...
git.checkout
0.34s$ git clone ...edited for brevity...
$ php --version
PHP 7.1.11 (cli) (built: Oct 26 2017 16:25:55) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
...edited for brevity...
$ composer --version
Composer version 1.6.5 2018-05-04 11:44:59
0.15s$ phpunit
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
...edited for brevity...
The command "phpunit" exited with 2.
Done. Your build exited with 1.
I'm not sure where to look next.
phpunit exits with an error because you haven't told phpunit where to look for your tests. Typically you would do this by placing phpunit.xml in the base directory of your repo. Among other things, phpunit.xml tells phpunit which directory contains your tests.
Your phpunit.xmlshould contain something like this:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Your application name">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Replace ./tests/ with the relative path to the folder containing your test files.
To test out your configuration, run phpunit while in the base directory of your repo. You should see the same result that Travis CI does.
As per the docs, "Travis CI uses .travis.yml file in the root of your repository to learn about your project and how you want your builds to be executed."
It wants something to run and defaults to running phpunit, which will fail without at least one test to run.
So for a placeholder build one could create a bash script in the same directory as the .travis.yml file, make it executable and point to it like this:
sudo: false
language: php
php:
- '7.1'
before_install:
- chmod +x build.sh
script: ./build.sh
This is a "build" script:
#!/usr/bin/env bash
echo This is a placeholder.
I'm new to Symfony and I'm following a couple of quick introductions on the Symfony website: the quick tour and installation guide. Both say you can install a server using:
composer require server --dev
When I run this command I get:
[InvalidArgumentException]
Could not find package server.
Did you mean one of these?
react/http
react/socket
cboden/ratchet
voryx/thruway
laravel/passport
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...
Okay, I can ignore this and use another local server, but I wondered if I was doing something wrong or if the documentation is out of date? Composer is installed fine on my machine (a Mac).
So the answer is: I wasn't in the 'quick_tour' directory that is created in the quick tour tutorial. It doesn't actually say to cd into that, but I suppose I should have guessed! After you cd into that the command works fine.
I had the same problem. You need to cd in the project directory before running the composer again. This resolved the error for me.
To fix the mistake, you must change your directory in the new file of your project and after it's running, you must give the correct route of the new file of the project created by composer.
composer create-project symfony/skeleton my-project
You must change directory with cd my-project to not stay in the previous directory when you had installed and created the file my-project.
When you're using symfony 5.1, you've to execute this following command:
symfony server:start -d
I am running CentOS 6 and am trying to install the SPL_Types extension for PHP7. I have php7 up and running however, any time I do pecl install SPL_Types,
I get massive compiler errors and can't find a way around. Ubuntu users can fix the issue by installing libpcre3-dev, but this is not a valid package on CentOS 6. I have verified that pcre and pcre-devel are installed and the latest version. Does anyone have a solution to this?
EDIT 1: A full log when I run the sudo pecl install SPL_Types command: http://pastebin.com/QG3mFjaf
I got it figured out. For those who wish to know, I had to use a third party source to compile and install the extension, then I had to edit my php.ini file manually to add the new module.
https://github.com/esminis/php_pecl_spl_types
then once you have it, run the commands:
phpize --clean
phpize
chmod +x configure
./configure --prefix=/usr
make install
Then, edit your php.ini file and under the dynamic modules section, add
extension=spl_types.so
Restart the httpd service by doing
sudo service httpd restart
Then verify the new module is loaded with phpinfo().
How do I install/setup Drupal Console on Windows 7? In the video by the author, the environment used is Linux.
I tried to install it on Windows 7, but I wasn't able to use it to generate a module code.
You can download the drupal console either using Git Bash
$curl https://drupalconsole.com/installer -L -o drupal
Or using windows command prompt
c:\>php -r "readfile('https://drupalconsole.com/installer');" > drupal
After downloading this file, place it beside php.exe file within your php folder (not project folder), restart your terminal/cmd-prompt and type
$drupal
to get started.
Besides downloading Drupal Console .phar file, you can also install Drupal Console using composer on windows.
First Download and install composer in your system.
Then on your windows command prompt or git terminal
$composer global require drupal/console:#stable
Create a file called drupal.bat
#php "%~dp0drupal.phar" %*
Here's the full list of steps I took:
php -r "readfile('https://drupalconsole.com/installer');" > drupal.phar
added installation location to the global PATH
created a drupal.bat file in the same directory as drupal.phar. drupal.bat contains: #php "%~dp0drupal.phar" %*
Now you can call >drupal from any location. I would upvote #Nate's answer, but this is a new account.
Open Git bash (or any terminal with curl capability)
Run curl https://drupalconsole.com/installer -L -o drupal.phar
Create file drupal.bat with content #php "%~dp0drupal.phar" %*
Copy both files (drupal.phar and drupal.bat) into C:\WINDOWS
Open new Command prompt window then try typing drupal list.
This steps assume:
Your PHP is on Path environment variables and you can run php --version.
You have Git for Windows installed.
you can install drupal console using composer
open the command line on windows then go to :
cd /path/to/drupal8.dev
now you can install drupal console :
composer require drupal/console:~1.0 --prefer-dist --optimize-autoloader
after that you installed drupal console you can use it from drupal root directory where composer.json exist as follow:
vendor/bin/drupal <the_command>
you should replace <the_command> with one of the drupal console commands. you can see complete list of the commands here: drupal console commands
Find a php.exe
Add this path to the environnement variable path
from terminal run
php -r "readfile('https://drupalconsole.com/installer');" > drupal.phar
i then use php-storm IDE :
file/settings/command line
add tool based on symfony console
fill php path and drupal.phar path
console is now set
tools/run command
drupal list
if you have php and composer all setup
make sure you're in your drupal root folder..open powershell (anyother cli) and do '
composer require drupal/console
'after this add.
'C:\<path to your drupalwebsite root folder>\vendor\bin'
to your paths Environment variables
and you have composer installed: Remember to install and add composer to path variables for every local drupal installation.
I've hit a roadblock with my phpunit functional tests. The AWS SDK requires APC, for some reason I can't get phpunit to load the extension. I'm not sure where I'm going wrong. The CLI is using the same ini file as MAMP
Gregs-MacBook-Pro:HvH-PHP greg$ php --ini
Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php5.3.6/conf
Loaded Configuration File: /Applications/MAMP/bin/php/php5.3.6/conf/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
Gregs-MacBook-Pro:HvH-PHP greg$
I've also attempted to add the ini file into app/phpunit.xml.dist
<php>
<ini name="mamp" value="/Applications/MAMP/bin/php/php5.3.6/conf/php.ini"/>
<server name="KERNEL_DIR" value="app/" />
</php>
Error message in CLI
PHP Fatal error: Call to undefined function apc_fetch() in /vendor/aws-sdk-for-php/lib/cachecore/cacheapc.class.php on line 58
EDIT: Some more test as per comments
In CLI I can run a test script with apc_fetch(); and it works successfully.
Running php -m also shows APC as an installed module
Any suggestions as to what else I should try?
I could propose you 2 solutions:
Create a symlink of /Applications/MAMP/bin/php/php5.3.6/conf/php.ini to /private/etc/php.ini
Modify phpunit execution file and append -c /Applications/MAMP/bin/php/php5.3.6/conf/ to the php run command. Also - check if correct php binary is used. If it is not - change to the correct path first and check if issue has been fixed