Hello I am new in PHPUnit and try to execute the Test Example:EmailTest by Composer, the Link is: https://phpunit.de/getting-started/phpunit-8.html
I got the text in the command_Line
$ phpunit --bootstrap vendor/autoload.php tests/EmailTest
PHPUnit 3.7.21 by Sebastian Bergmann.
Cannot open file "vendor/autoload.php".
It appears that you have different installations of PHPUnit mixed up.
For instance, you may have used Composer to install PHPUnit and have configured the autoloader generated by Composer as PHPUnit's bootstrap script but then you invoke PHPUnit using an executable other than vendor/bin/phpunit.
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.
Using a composer-installed bin
I'm running a most basic task as follows:
<autoloader autoloaderpath="vendor/autoload.php">
<target name="asdf">
<echo msg="test"/>
<phpunit configuration="app/phpunit.xml"
haltonerror="true"
haltonfailure="true"
pharlocation="bin/phpunit"
/>
</target>
Now simply running this task:
phing -debug asdf
Will result in:
+Task: echo
-calling setter EchoTask::setMsg()
[echo] qwerqwer
+Task: phpunit
-calling setter PHPUnitTask::setConfiguration()
-calling setter PHPUnitTask::setHaltonerror()
-calling setter PHPUnitTask::setHaltonfailure()
-calling setter PHPUnitTask::setPharLocation()
#!/usr/bin/env php
Cannot open file "asdf.php".
Using a .phar
Using the same configuration except pharlocation:
+Task: echo
-calling setter EchoTask::setMsg()
[echo] test
+Task: phpunit
-calling setter PHPUnitTask::setConfiguration()
-calling setter PHPUnitTask::setHaltonerror()
-calling setter PHPUnitTask::setHaltonfailure()
BUILD FINISHED
No errors, but the phpunit tasktype doesnt start.
Unrecognized option
Had a third simple variant that seemed fine, which resulted in 'phpunit: unrecognized option -- p' that i sadly cant reproduce..
Versions
PU 5.7
Phing 2.16
PHP 7.1
I see you used PHPUnit 5.7. Did you use namespaces in your tests?
The current version of Phing is not compatible with namespaces like PHPUnit\Framework\TestCase, you have to use the old class \PHPUnit_Framework_TestCase instead.
The next Phing's release is going to be compatible with PHPUnit's namespaces (https://github.com/phingofficial/phing/issues/659). Meanwhile you can create phpunit.xml and add this to build.xml to run your tests:
<target name="phpunit" description="Run tests">
<exec executable="bin/phpunit" passthru="true">
<arg value="--configuration"/>
<arg value="app/phpunit.xml"/>
</exec>
</target>
I faced this issue when invoking Phing using Jenkins. The Phing plugin for Jenkins adds a -buildfile switch to the command calling Phing and when I removed this switch (running the command manually), my PHPUnit test suit was executed successfully. I recommend avoiding the Phing plugin for Jenkins and using a shell execution step to invoke Phing directly:
/path/to/php /path/to/phing/binary/
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
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