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>
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
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)
I've created a simple PHP library using PHP 5.6.
It includes PHPUnit test cases and I succeeded in creating the package.
Then I found that I can install these packages directly from GitHub, using composer, in other projects also. I can run tests independently at the development stage. But once I publish the package I can't run the tests, as it is not properly finding the autoloader files.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
I've added the xml files in root directory. The path of the autoload file is correct when it is independent. But the directory structure will be a different one once it is installed.
The problem still persist if we use require_once the autoloader file, as the directory structure change after installation.
Questions:
Can we test our package after installing it into any frameworks (laravel/Symfony) or in any other projects?
What is the best practice? Is the test needed for the developers who is using the package?
If yes, then any solution to solve this? Is there any other method to autoload problem in both environments?
Repo in Github
I would say you're best to look at the other repositories and see if it works or not.
For me
cd vendor/phpunit/phpunit && composer install && phpunit => worked
cd doctrine/collections/ && composer install && phpunit => worked
So it seems that it should be working for you.
If you look at the phpunit.xml.dist for the other vendors they use:
bootstrap="./tests/Doctrine/Tests/TestInit.php" (doctrine)
bootstrap="tests/bootstrap.php" (phpunit)
It looks like your file isn't so different. Are you sure you ran composer install from inside your packages directory inside the vendor folder?
Update:
I added your repository as a dependency to a default Symfony installation.
"repositories": [
{
"url": "https://github.com/jerintk/Validator.git",
"type": "git"
}
],
And in the require block:
"Jthedev/Validators": "dev-master"
I then ran
composer update
It ran fine. You need to run composer update and check in the new composer.lock because it's out of date.
From there I cd'ed into the directory for your repo.
cd vendor/Jthedev/Validators
I then ran
composer install
and
vendor/phpunit/phpunit/phpunit
and got
OK (2 tests, 2 assertions)
Update Two
(since this was too long for a comment)
#JTheDev composer update adds your dependencies for the laravel project, but it doesn't create the vendor folder inside your vendor/your-project directory. If composer installed all dependencies separately for each project, like:
vendor/
my-project/
vendor/
dependency-A
dependency-C
another-library/
vendor/
dependency-A
dependency-B
it would waste a lot of space and bandwidth. Instead composer gets all the dependencies and installs them in separate folders and they work with each other because composer loads them all using vendor/autoload.php for that project. What you're talking about is creating the vendor folder inside vendor/your-project. This is not normal - usually you only have to run tests when developing yourself, but anyway your question is about how to run your tests for your project when it's added as a dependency for another project.
To do this you need to run composer install inside the vendor/your-project directory, that means:
cd vendor/Jthedev/Validators && composer install && vendor/phpunit/phpunit/phpunit
Final update (hopefully)
From chat:
The autoloader is only generated when you run composer install inside your project directory. You are correct, the vendor folder should not be there usually but you need it if you want to do what you are trying to do. It is not conventional. Normally developers run their tests inside their project root folder, not on the dependencies, but your questions was "how can I run my tests when it is a dependency". The answer is you need to create the vendor and autoload files inside your project folder
The tests only run if you run composer install inside the project folder. But it's not a problem if the tests don't run without doing that.
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 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.