I am attempting to get phpUnit to function properly with MAMP on OS High Sierra and when trying to run a simple test it throws
"Fatal error: require_once(): Failed opening required 'PHPUnit/Framework/TestCase.php'
I've looked at several StackOverflow articles and they suggest changing it to "autoload.php"
that then throws...
"Failed opening required 'PHPUnit/Autoload.php'"
This is on Zend Framework 1 with PHP 7.2.
If you use Composer to install the PHPUnit, make sure you run it like this:
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest
Or include vendor/autoload.php in your phpunit.xml like this:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
See the docs.
The issue was the version of the framework. 1.8 is just not going to work. Phpunit works just fine with a 3.0.3 Skeleton. I’m going to research migrating the codebase to 3.0.3
Thanks for you help
Related
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
I am trying to get familiar with testing (unit, functional) under Symfony 2.3.24/Windows7/PHP 5.4.7.
It seems PHPUnit is installed correctly (via Composer), but when I run a phpunit -c app/ command I get the following error:
Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream: No such file or directory in C:\xampp\php\phpunit on line 38
Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\php\phpunit on line 38
I googled for the issue to no avail. I additionnaly found in the official PHPUnit website that The code coverage report feature requires the Xdebug (2.1.3 or later) and tokenizer extensions.
What do you think about all this? your help is much appreciated.
In current versions of PHPUnit the phpunit script does not include PHP/CodeCoverage/Filter.php directly. It appears you are using an outdated version of PHPUnit and/or have a mixup of Composer/PEAR installations.
Please follow the instructions on https://phpunit.de/getting-started.html to properly install PHPUnit. And http://thephp.cc/news/2015/01/phpunit-migration-from-pear-to-phar explains how to migrate from a PEAR-based installation to Composer or PHAR.
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.
I'm currently trying to run PHPUnit on my Ubuntu 12.04 virtual machine and when I run phpunit under my application root that has phpunit.xml.dist file it reads from the config but I'm getting below error and it fails:
PHP Fatal error: Call to undefined method PHPUnit_Util_Test::getHookMethods() in phar:///usr/bin/phpunit/phpunit/Framework/TestSuite.php on line 633
Is there a reason why this is happening?
I had a similar issue in a different environment.
The problem was that another library's loader was registered too early which also contained another version of PHPUnit which had no that method. The proper order of autoloaders solved the problem.