This morning, I realized that the version of PHPUnit supplied with Xampp has been deprecated for quite a while...
.
The version 3.7.21. installed in Xampp, but the actual version (at the time of this writing) is 6.0.13.
I tried some solution proposed on google (all old 5 years + solution), including
https://wehuberconsultingllc.com/wordpress/2007/08/18/updating-phpunit-on-xampp/
How to configure PhpUnit in Xampp?
http://www.righthandedmonkey.com/2012/09/setting-up-phpunit-with-xampp.html
http://forum.kohanaframework.org/discussion/7346/installing-phpunit-on-windows-xampp/p1
...
In any case, pear doesn't seem a viable solution. Is there a simple way to update PHPUnit in Xampp?
Actually, it can be updated in three simple steps:
Download the last version of PHPUnit here:
https://phpunit.de/index.html
Copy “phpunit.phar” in “C:\xampp\php”.
In the file: “phpunit.bat”, update the following line:
"%PHPBIN%" "C:\xampp\php\phpunit" %* to : "%PHPBIN%" "C:\xampp\php\phpunit.phar" %*
You don't need to restart apache.
Additional note: In your tests, you will need to replace: phpunit_framework_testcase by: TestCase
And include: use PHPUnit\Framework\TestCase at the top of your test files.
Of course, the test suites are still compatible on my production server (centos7, follow the official documentation to update on Linux https://phpunit.de/getting-started.html).
Thanks, Working OK with XAMPP v7.3.7
The latest phpunit.phar can be downloaded from: https://phar.phpunit.de
XAMPP Included Version = PHPUnit 3.7.21 by Sebastian Bergmann.
DOWNLOADED Version (2019-08-11) = PHPUnit 8.3.4 by Sebastian Bergmann and contributors.
I had similar problem and found different solution.
I have install phpunit with composer globaly
composer global require phpunit/phpunit "^9"
and localy in my project (both same version "^9").
cd C:/fullpath/to/myproject
composer require-dev phpunit/phpunit "^9"
However for some reason phpunit was triggered from the xampp folder.. so what I did was removed
phpunit
phpunit.bat
From C:\xampp\php and it worked for me.
Just remember to set composer path to your env path in Windows.
Now you can create phpunit.xml in you project root (here is what worked for me):
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
testdox="true"
verbose="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
Then just cd to my root and run phpunit in my terminal to run all my tests from the tests folder
Hope you will find this information helpfull
*Editor: Hypper.js - using Git Bash on Windows 10 (incase you wonder)
Related
I have developped some bundles i.e. https://github.com/975L/EmailBundle and I want to add tests for them. So, I have created the file Tests/Service/EmailServiceTest.php that contains one test.
I have installed phpunit using sudo apt install phpunit on my Ubuntu computer, but when I cd to my bundle's folder and run phpunit I have the following error:
1)
c975L\EmailBundle\Tests\Service\EmailServiceTest::testCreateEmailObject
Error: Class 'c975L\EmailBundle\Service\EmailService' not found
It looks like the autoload can't find the class, even if declared at the top with use.
I saw some solutions over the web that requires to create a bootstrap.php with autoload but even with this, it doesn't work...
So my question is how do we test a standalone Symfony Bundle with phpunit?
It is a good idea to install phpunit on a per project basis, as version differences could come up if you have multiple projects.
(1) composer require phpunit/phpunit --dev
I saw some solutions over the web that requires to create a bootstrap.php with autoload but even with this, it doesn't work...
You can use composer's autoloader as a bootstrap. A custom bootstrap might come in handy in some cases, but not necessary.
(2) Create a phpunit.xml.dist file, something like this (your current one does not specify a bootstrap):
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="MyBundle test suite">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Also check out vendor/bin/phpunit --generate-configuration which is nice.
(3) Run vendor/bin/phpunit
It should find the config file automatically, but you can also specify it with the --configuration option
I am working in OSX 10.11
I am trying to setup PHPUnit , MAKEGood and Xdebug with Eclipse .
XDebug is Done .
I can Run PHPUnit tests form the console .
But now configuring MakeGood is lot more harder than i expected .
MY PHP Executables
Do i have to add pear in Eclipse->Preferences->PHP->Libraries ?? i am not sure because i installed PHPUnit using Brew.
brew install homebrew/php/phpunit
But again i tried to include PEAR .
I gave the path as usr/local/bin because when i try which pear in terminal it will output as
/usr/local/bin/pear
And in my project properties under PHP-> include path i added the above PEAR library .
still from Makegood the error comes as
PHPUnit_Framework_TestCase class is not available. Fix..
I tried a lot of things , Ex:-
reinstalling pear
rm .metadata/.plugins/org.eclipse.dltk.core.index.sql.h2/*
restart Eclipse
Restart Computer
change pear library path
Actually i am not sure what i am doing wrong . Even i am not sure i need the pear library .
Any help is appreciated . Thanks in advance .
MakeGood and Composer need some fiddling to get them to work see
https://github.com/piece/makegood/issues/17
https://github.com/piece/makegood/issues/85
You might want to
install phpunit with composer
add a file MakeGoodPreload.php as your Preload Script in the MakeGood configuration.
add phpunit.xml
More recent PHPUnit releases are optionally done with composer.
First install composer:
curl -sS https://getcomposer.org/installer | php
See https://phpunit.de/manual/current/en/installation.html
then install phpunit
php composer.phar require "phpunit/phpunit=4.8.*"
now test from the command line
vendor/phpunit/phpunit/phpunit.php test/MakeGoodTest.php
using the MakeGoodTest.php file below.
The result should be:
PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Warning: Deprecated configuration setting "strict" used
.
Time: 86 ms, Memory: 4.50Mb
OK (1 test, 5 assertions)
Recent MakeGood releases support the user of the composer installed phpunit.
In your Eclipse project create a project "makegood" that contains your
composer installation, test/MakeGoodTest.php, MakeGoodPreload.php and phpunit.xml.
Right click properties of the project and go to the "MakeGood" tab.
in the PHPUnit tab add phpunit.xml and in the General Tab set the Preload Script to MakeGoodPreload.php.
Now you should be able to open MakeGoodTest.php in the editor and right click to
get "Run Tests in class ...".
running it should give you:
PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
Warning: Deprecated configuration setting "strict" used
.
MakeGood
[x] [32mPush and pop[39m
Time: 192 ms, Memory: 8.75Mb
OK (1 test, 5 assertions)
phpunit.xml
<phpunit backupGlobals="true"
backupStaticAttributes="false"
cacheTokens="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
strict="false"
verbose="false">
</phpunit>
MakeGoodPreload.php
<?php
// This is a preload script to be used with
// the Eclipse makegood continuous integration plugin
// see https://github.com/piece/makegood/releases
error_reporting(E_ALL);
$loader = require 'vendor/autoload.php';
test/MakeGoodTest.php
<?php
class MakeGoodTest extends PHPUnit_Framework_TestCase
{
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
I am encountering a strange issue while trying to run PHP unit tests on Travis CI.
.travis.yml
sudo: false
language: php
php:
- 5.4
env:
- VUFIND_HOME=$PWD VUFIND_LOCAL_DIR=$PWD/local
before_script:
- pear install pear/PHP_CodeSniffer
- pear channel-discover pear.phing.info
- pear install phing/phing
- composer global require fabpot/php-cs-fixer
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- phpenv rehash
script:
- phpunit --stderr --configuration module/VuFind/tests/phpunit.xml
- phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
- phpcs --standard=PEAR --ignore=*/config/*,*/tests/* --extensions=php $PWD/module
- phing php-cs-fixer-dryrun
module/VuFind/tests/phpunit.xml is a third party framework
module/Swissbib/tests/phpunit.xml is our own code
module/Swissbib/tests/phpunit.xml
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="sbvfrd">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
The tests from the third party framework run without errors. Our own tests do not work and we get the error message:
$ phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
Could not read "module/Swissbib/tests/phpunit.xml".
Locally (Mac OS X) all the tests run through. Strangely enough the Bootstrap.php defined in module/Swissbib/tests/phpunit.xml runs completely through on Travis CI, I verified this using echo statements. Nevertheless phpunit tells us that it could not read phpunit.xml.
Travis: https://travis-ci.org/swissbib/vufind
Repo: https://github.com/swissbib/vufind (development branch)
Any ideas what could be going wrong?
I found the solution by downloading the phpunit source and debugging with it.
We were changing the directory within the Bootstrap.php file to a different location then the phpunit command was run from. We run the phpunit command from our project root folder and then changed the working directory to the tests folder, because we were using relative paths. I changed everything to absolute paths (using __DIR__) so we do not have to change the working directory anymore.
Bottom line: Do not change the directory in the bootstrap file as it causes phpunit to fail with this error message: Could not read phpunit.xml.
I'm been following a PHPUnit tutorial for the first time and my tests run fine locally. However, when running my tests on Travis CI, no tests are executed and my build exits with 0.
My directory structure and full code can be seen on the repo.
Build log from Travis CI (Full build log)
1.51s$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /home/travis/build/idavidmcdonald/phpunit-tutorial/composer.phar
Use it: php composer.phar
before_script.2
0.33s$ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
0.08s$ vendor/bin/phpunit --debug
PHPUnit 3.7.14 by Sebastian Bergmann.
Configuration read from /home/travis/build/idavidmcdonald/phpunit-tutorial/phpunit.xml
Time: 13 ms, Memory: 2.00Mb
No tests executed!
The command "vendor/bin/phpunit --debug" exited with 0.
Done. Your build exited with 0.
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>phpunittutorial/tests/</directory>
</testsuite>
</testsuites>
</phpunit>
.travis.yml:
language: php
php:
- 5.4
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install
script: vendor/bin/phpunit --debug
My tests run successfully locally, however maybe there is an issue somewhere with my phpunit.xml file?
The directory containing your tests is incorrect.
The correct path would be phpUnitTutorial/tests. Note that Windows does not care about case sensitivity, but everyone else in the world does. Best thing would be to always use lower case for paths, or double check you are using the correct case everywhere (PSR-0 and PSR-4 would require path names with the same case as the class name, which will usually include upper case letters).
And by the way: You should probably upgrade to a more recent version of PHPUnit. That old 3.7 series is not getting any more updates for years now, and the transition to 4.x isn't too steep - you should just do it.
language: php
php:
- 5.4
- 5.5
install: composer install
script: ./vendor/bin/phpunit
Not sure about install: composer install, probably can be omitted
Whenever I run phpunit tests from PHPStorm I get an error. I have provided more info below. I am not sure where I have miss configured the setup.
My Setup
Ubuntu
PHPStorm 8.0.1
PHPUnit 4.3.4
More Info:
PHPUnit.phar is located at /usr/local/bin/phpunit.phar. I have setup PHPUnit path directly in PHPStorm. Tests run from bash with no issues. I have also setup my configuration file phpunit.xml in PHPUnit, which is located in the root of my project. The phpunit.xml file tells phpunit to load the composer autoload.php file.
PHPUnit Output:
/usr/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /tmp/ide-phpunit.php --configuration /home/mkelley/projects/CompanyName/phpunit.xml
Testing started at 10:33 AM ...
PHPUnit 4.3.4 by Sebastian Bergmann.
Configuration read from /home/mkelley/projects/CompanyName/phpunit.xml
PHP Fatal error: Call to undefined method CompanyNameTests\Boundaries\BoardMemberVotingBoundaryTest::hasExpectationOnOutput() in phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/ResultPrinter.php on line 545
PHP Stack trace:
PHP 1. {main}() /tmp/ide-phpunit.php:0
PHP 2. IDE_Base_PHPUnit_TextUI_Command::main($exit = *uninitialized*) /tmp/ide-phpunit.php:500
PHP 3. PHPUnit_TextUI_Command->run($argv = *uninitialized*, $exit = *uninitialized*) /tmp/ide-phpunit.php:243
PHP 4. PHPUnit_TextUI_TestRunner->doRun($suite = *uninitialized*, $arguments = *uninitialized*) phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:186
PHP 5. PHPUnit_Framework_TestSuite->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:423
PHP 6. PHPUnit_Framework_TestSuite->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestSuite.php:703
PHP 7. PHPUnit_Framework_TestCase->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestSuite.php:703
PHP 8. PHPUnit_Framework_TestResult->run($test = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestCase.php:771
PHP 9. PHPUnit_Framework_TestResult->endTest($test = *uninitialized*, $time = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestResult.php:760
PHP 10. PHPUnit_TextUI_ResultPrinter->endTest($test = *uninitialized*, $time = *uninitialized*) /home/mkelley/projects/CompanyName/vendor/phpunit/phpunit/src/Framework/TestResult.php:378
Process finished with exit code 255
I have searched Google and was unable to find a similar issue. I appreciate any help!
EDIT
Here is my phpunit.xml file. PHPStorm is using this as a "Use alternative configuration file"
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
bootstrap="./vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
This appears to be the autoloading issue. When you bootstrap your app for the test suite you must initialise your autoloader, which doesn't seem to be happening, as something doesn't get found. The easiest way would be to use Composer to manage the PHPUnit dependency and autoload your classes via the autoload directive. See the the psr-4 part in documentation.
Then in your PhpStorm PHPUnit configuration window select Use custom autoloader and specify path to your vendor/autoload.php script.
Sometimes is better an image...
As you can see, you can also use your phpunit.phar file.
I will answer my own question in case someone else comes across this issue.
The issue was autoloading PHPUnit via composer and using phpunit.phar. Once I removed the phpunit dependence from composer PHPStorm was able to successfully run all my tests.
The problem isn't that you autoloading phpunit via composer, but that in the composer you use an old version of phpUnit. In my case instead of using 4.0.0 I updated to 4.6.*.
I've been having this same issue with composer and found using the .phar didnt have any issues. Today I've just realised slaps forehead it was just caused by installing phpunit via composer and then not reindexing the vendor folder.
I haven't found that I've had this issue previously when installing new packages with composer but for some reason when installing phpunit it hadn't reindexed the vendor folder causing inconsistencies.
Reindex, everything working normally.