how to install phpUnittest? - php

I am trying to write a Unit Test to test my YII application using phpunitest. I installed it and when I try to run my test I use shell command:
phpunit --bootstrap bootstrap.php DbTest.php
then I get this error:
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
Cannot open file "bootstrap.php".
how Can I fix this?

This problem can be easily fixed.)
You should write correct path to your bootstrap.php. That's all.)
You can run phpunit --bootstrap boostrap.php SMTest.php only if your boostrap.php lies in same directory that SMTest.php.
Or you can write absolute paths:
phpunit --bootstrap /www/tests/config/boostrap.php /www/tests/unit/SMTest.php
and invoke this command from any place.

your bootstrap goes in your phpunit.xml config file.
So it would look something like:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php" colors="true">
</phpunit>
and when you run your test you run it with
phpunit --configuration=phpunit.xml
For reference: The phpunit.xml config section in the manual.

Related

Run PHPUnit for plugins folders from root directory

I would like to run my PHPUnits tests in the plugins from the root directory.
The problem is that the individual plugins each come from their own repository and PHPUnit are registered there as via composer dev dependencies.
The build process for the main project "Application" pulls out the plugins using composer.
This structure has my current PHP project:
/Application
/Plugins
/PluginFirst
/tests
/PluginSecond
/tests
/PluginThird
/tests
Idea is to create shell or ruby script to iterate the plugins folder and run PHPUnit.
You can create a configuration file called phpunit.xml.dist and save it in your root project folder with the following configuration. Assuming you have also a test folder to run your own unit tests at the same level of the folder:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Application API Test Suite">
<directory>./tests/</directory>
<directory>./Plugins/</directory>
</testsuite>
</testsuites>
</phpunit>
Then you can run phpunit in this way:
phpunit --configuration phpunit.xml.dist
If you want to exclude for example the PluginThird folder, you can add this line inside the tag
<exclude>./Plugins/PluginThird/</exclude>

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.

Is it possible to run PHPUnit with a dynamically loaded extension library?

I have a suite of PHPUnit tests for my extension, and I want to run them as part of the extension's Hudson build process.
So I want to run PHPUnit specifying the extension library to load at runtime, but I can't figure out how to do this.
My directory structure is as follows:
/myextension.c
/otherextensionfiles.*
/modules/myextension.so
/tests/unittests.php
I've tried running PHPUnit with an configuration XML file as follows:
<phpunit>
<php>
<ini name="extension_dir" value="../modules/"/>
<ini name="extension" value="myextension.so"/>
</php>
</phpunit>
And then running it as follows (from the tests directory):
phpunit --configuration config.xml unittests.php
But then I get Fatal error: Call to undefined function myfunction(), so it's not loading the library.
I've also tried:
phpunit -d extension_dir=../modules/ -d extension=myextension.so unittests.php
And also dl('myextension.so') to the test setup, but no joy.
If it's relevant, this is using PHP 5.2 and PHPUnit 3.4.11.
I've cross-posted this question on the PHPUnit users mailing list.
I suspect not possible to set extension_dir at runtime in PHPUnit since it has the attribute PHP_INI_SYSTEM in this chart.
That means it's not possible to it with ini_set(), so I assume it's also not possible to set it with the PHPUnit config <ini name="extension_dir" value="/mydir/">.
EDIT:
However, since PHPUnit is just another PHP script, it is possible to run PHP directly, and pass in the override commands this way!
This gets around the above issue, and the following works:
php -d extension_dir=../modules -d extension=myextension.so /usr/bin/phpunit unittests.php
The only problem with this approach is that PHPUnit will no longer be able to find any extensions that were installed in the normal directory ( eg /usr/lib/php5/20060613/ ), this can be resolved by adding symbolic links from your extension_dir to the relevant files in the default extension_dir.

Categories