PHPUnit_Framework_TestCase class is not available. Fix... - Makegood , Eclipse - php

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));
}
}
?>

Related

How to use PHPUnit extensions with Symfony's PHPUnit Bridge?

I'm using PHPUnit Bridge in my Symfony project. I'm currently using PHPUnit 7 and I want to upgrade to PHPUnit 8.
In PHPUnit 8 array subset assertions are deprecated and generate warnings. I want to use dms/phpunit-arraysubset-asserts package to provide them. With regular PHPUnit I'd just composer require it and call it a day.
Now, the Bridge doesn't have original PHPUnit as its dependency, instead it installs it to a temporary folder, patches it and runs from there. phpunit-arraysubset-asserts has PHPUnit dependency though and generates a warning:
Adding phpunit/phpunit as a dependency is discouraged in favor of Symfony's PHPUnit Bridge.
* Instead:
1. Remove it now: composer remove --dev phpunit/phpunit
2. Use Symfony's bridge: composer require --dev phpunit
I don't want to install phpunit/phpunit to avoid confusion.
I've tried to ignore it by adding a * replacement, but just adding the replacement to composer.json produces a Composer error:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- __root__ No version set (parsed as 1.0.0) conflicts with symfony/phpunit-bridge[v5.0.5].
- symfony/phpunit-bridge v5.0.5 conflicts with __root__[No version set (parsed as 1.0.0)].
- Installation request for __root__ No version set (parsed as 1.0.0) -> satisfiable by __root__[No version set (parsed as 1.0.0)].
- Installation request for symfony/phpunit-bridge v5.0.5 -> satisfiable by symfony/phpunit-bridge[v5.0.5].
What's the correct way to add a PHPUnit extension when using PHPUnit Bridge?
WARNING I do not know phpunit-bridge well (in fact today was my first day with it), but I can see the way to achieve what you want. It's not that convenient, but possible. I think it's better to either check with symfony team (maybe there is a supported way for this) or just use plain phpunit (it seems easier and it should be possible to integrate it with symfony's phpunit-bridge without relaying on bridge to install phpunit itself)
# create new symfony project
symfony new gronostaj-phpunit
# cd in it
cd gronostaj-phpunit
# install phpunit-bridge
symfony composer req phpunit-bridge
# to change phpunit version to 9.5
vim phpunit.xml.dist
# install phpunit in symfony bridge way
bin/phpunit
# to update phpunit config
bin/phpunit --migrate-configuration
# black magic begins here
cd bin/.phpunit/phpunit-9.5-0
# to bypass `autoload-dev` in later dump-autoload
mkdir -p tests/_files ; touch tests/_files/CoverageNamespacedFunctionTest.php tests/_files/CoveredFunction.php tests/_files/NamespaceCoveredFunction.php
# install phpunit plugin
composer require --no-plugins dms/phpunit-arraysubset-asserts
# cd back to the root of project
cd -
# create test
--- phpunit/gronostaj-phpunit ‹master* M?› » cat tests/SubsetTest.php
<?php
declare(strict_types=1);
namespace App\Tests;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use PHPUnit\Framework\TestCase;
class SubsetTest extends TestCase
{
use ArraySubsetAsserts;
public function testWithTrait(): void
{
$expectedSubset = ['bar' => 0];
$content = ['bar' => 0];
self::assertArraySubset($expectedSubset, $content, true);
}
public function testWithDirectCall(): void
{
$expectedSubset = ['bar' => 0];
$content = ['bar' => 0];
Assert::assertArraySubset($expectedSubset, $content, true);
}
}
# run test
--- phpunit/gronostaj-phpunit ‹master* M?› » bin/phpunit
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Testing
.. 2 / 2 (100%)
Time: 00:00.006, Memory: 8.00 MB
OK (2 tests, 2 assertions)
# done
For phpunit 8, use phpunit verion 8.0 in phpunit.xml.dist and install your plugin like this composer require --no-plugins 'dms/phpunit-arraysubset-asserts:*', that will work.
To make it reproducible for others in your team you will need to provide composer scripts (to create dummy files and install plugin, but it should do the trick) and hook them into something like post-install and post-update events.
I have been struggling with this issue as well in the past, and found this tip in the PHPUnit Bridge documentation:
It is also possible to require additional packages that will be
installed along the rest of the needed PHPUnit packages using the
SYMFONY_PHPUNIT_REQUIRE env variable. This is specially useful for
installing PHPUnit plugins without having to add them to your main
composer.json file.
So basically, edit phpunit.xml.dist and declare the env variable under the existing <php> tag:
<php>
<server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts" />
</php>
To add multiple packages, use a space delimiter:
<php>
<server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts spatie/phpunit-snapshot-assertions" />
</php>

Update PHPunit Xampp

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)

PHPUnit "Could not read phpunit.xml" on Travis CI

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.

PHPunit no tests execute on Travis CI

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

PHPUnit and PHPStorm... ResultPrinter.php Error

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.

Categories