PHPStorm 8 and PHPUnit problems with #runInSeparateProcess - phpunit

I am running phpunit (composer provisioned and version 4.8) from PHPStorm 8. Usually it works fine but whenever I need to use the #runInSeparateProcess annotation it starts screaming this error:
Fatal error: Class 'PHPUnit_Util_Configuration' not found in - on line 334
Call Stack:
0.0013 395808 1. {main}() -:0
The PHPUnit configuration on the IDE is the following:
Language & Frameworks > PHP > PHPunit: custom autoloader pointing to codebase/vendor/autoload.php
Run/Debug Configuration: alternative configuration file which point to my local phpunit.xml
This is the content of the configuration:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd"
colors="true"
bootstrap="./vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="My Project">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
The tests run as expected from the command line with the same phpunit executable and the same configuration file.
Any suggestion?

Apparently removing all dependencies and re-installing phpunit from composer (phpunit 4.8.6) solved the problem.

A modification of the fix the folks over at Drupal are using (https://www.drupal.org/node/2597814)
Add to the top of your boostrap file:
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/path/to/composer/vendors/dir/autoload.php');
}
I tried many other methods, including upgrading PHPStorm and up/down-grading PHPUnit. This works.

Related

php-vcr will break autoload of PHPUnit

Enabling php-vcr in PHPUnit bootstrap file breaks the autoloading of class PHPUnit\Framework\ExceptionWrapper
I tried to downgrade PHPUnit to 7.x without success. Removing VCR from bootstrap solves the ExceptionWrapper issue
Here's the beginning of my bootstrap file
require_once __DIR__ . '/../vendor/autoload.php';
use VCR\VCR;
VCR::turnOn();
VCR::configure()->setMode('new_episodes');
PHPUnit configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<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="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
<testsuite name="end2end">
<directory>tests/End2End</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
Expected: successful tests
Actual:
./vendor/bin/phpunit --color=always --verbose --testsuite=integration
PHPUnit 8.1.5 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.4 with Xdebug 2.7.2
Configuration: /var/www/project/phpunit.xml.dist
............................
Fatal error: Uncaught Error: Class 'PHPUnit\Framework\ExceptionWrapper' not found in /var/www/project/vendor/phpunit/phpunit/src/Framework/TestResult.php on line 732
Error: Class 'PHPUnit\Framework\ExceptionWrapper' not found in /var/www/project/vendor/phpunit/phpunit/src/Framework/TestResult.php on line 732
Call Stack:
0.0002 422288 1. {main}() /var/www/project/vendor/phpunit/phpunit/phpunit:0
0.0224 1925752 2. PHPUnit\TextUI\Command::main() /var/www/project/vendor/phpunit/phpunit/phpunit:61
0.0224 1925864 3. PHPUnit\TextUI\Command->run() /var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:163
0.1295 6797680 4. PHPUnit\TextUI\TestRunner->doRun() /var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:207
0.1456 7214088 5. PHPUnit\Framework\TestSuite->run() /var/www/project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:613
0.1463 7214280 6. PHPUnit\Framework\TestSuite->run() /var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
0.6619 12277352 7. PHPUnit\Framework\TestSuite->run() /var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
0.6629 12277544 8. PHPUnit\Framework\DataProviderTestSuite->run() /var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
0.8272 18014088 9. EasyWelfare\Tests\Integration\Routes\AvailabilityTest->run() /var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
0.8272 18014088 10. PHPUnit\Framework\TestResult->run() /var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:808
Hint: the test that breaks involves SoapClient

CakePHP 2 Code Coverage now says "No files to generate coverage for"

My unit tests on CakePHP still run, but the code coverage has disappeared. All I get is "No files to generate coverage for".
Our application is currently running on CakePHP 2.10.15. I have PHPUnit 5.7 installed. Running PHP 7. I use the web runner for tests & coverage. I have XDebug 2.7.0beta1 running.
Did one of our recent upgrades break some sort of connection between Cake and PHPUnit?
Create a file phpunit.xml in your app folder:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd">
<logging>
<log type="coverage-text" target="tmp.txt" showOnlySummary="true"/>
</logging>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<!-- directories that you want in code coverage -->
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
</phpunit>
For me just adding this file like this worked just fine. For more information about this xml: https://phpunit.de/manual/5.7/en/appendixes.configuration.html
Be aware to include only the files and directories you need, otherwise it will became very slow.

Symfony 4, PHPUnit Bridge and phpunit location

If you install phpunit/phpunit package in Symfony 4 application, you get the message:
Adding phpunit/phpunit as a dependency is discouraged in favor of Symfony's PHPUnit Bridge.
Instead:
Remove it now: composer remove --dev phpunit/phpunit
Use Symfony's bridge: composer require --dev phpunit
So, I installed symfony/phpunit-bridge package.
It created bin/phpunit file and vendor/bin/simple-phpunit.
If I run bin/phpunit, it downloads phpunit project and installs its dependencies in bin/.phpunit/phpunit-6.5.
If I run vendor/bin/simple-phpunit, it downloads phpunit project and installs its dependencies in vendor/bin/.phpunit/phpunit-5.7.
Note that the versions are not the same. Why?
And why to not use composer and its autoloader? Now we have troubles with it and other tools like PHPStorm (broken phpunit debugging, etc).
I know, that I can add phpunit path to main composer autoload, but this method seems very dirty.
How to use phpunit in Symfony 4 proper way, with all debugging integrations, etc?
I have managed to fix this issue by adding phpunit.xml.dist in the root of the Symfony.
The file existed previously but I suspect that when I did remove phpunit, it also removed this file.. (I could be wrong but I am pretty certain)
When I add back the standard phpunit.xml.dist file to the root, it prompts me to add 'KERNEL_CLASS' variable in it.
When I add it in, it started working fine.
Full contents of my phpunit.xml.dist in case someone needs it
<?xml version="1.0" encoding="UTF-8"?>
<!--
https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="App\Kernel" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>

Phpunit tries to test all Symfony console commands

I'm working on a Symfony project. As I need to do unit testing, I downloaded and installed Phpunit 6.2.4 from its website.
However, when I tried to update my database, I got this output
php bin/console doctrine:schema:update --dump-sql
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
unrecognized option --dump-sql
bin/console doctrine:schema:update
Cannot open file "doctrine:schema:update.php".
I tried other console commands, but the result is the same. Basically, my guess was that somehow Phpunit tries to test every single file, so I edited the phpunit.xml file like this, using a previous one that worked in other project.
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="app/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
</exclude>
</whitelist>
<blacklist>
<directory>bin</directory>
<directory>docker</directory>
</blacklist>
</filter>
</phpunit>
As far as I know, blacklisting both bin and docker directories should result in phpunit not running anything inside them, but it still doesn't work.
Then I checked my composer.json for the symfony/phpunit-bridge, removed it and tried again, but the problem continues.
Has anyone ever faced this?
Blacklisting appear to remove directories from the code-coverage generation whitelist - but if src/ does not contain those sub-directories, and so it is redundant - and also a great deal slower adding and discarding huge numbers of files. On one project, I tried to blacklist vendor/, it was taking a minute to even show the initial command banner, before starting to run the tests.
Remove the <blacklist/> section, and the rest should be fine - if you don't mention bin/ or docker/ then PHPunit won't need to read the files, it so won't run them either.

PHPUnit: No test executed with a seemingly good config file

I get the message no tests executed when i try to do
phpunit
or
phpunit -c phpunit.xml
on the other hand, if i do
phpunit -c phpunit.xml ./tests
It works. But this is a problem considering that some tool that I'm using does not handle this well.
Directory structur
code
tests/
phpunit.xml
autoloader.php
And here is the config file
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
bootstrap="./tests/bootstrap.php"
>
<testsuites>
<testsuite name="StdTestSuite">
<directory>
tests/
</directory>
</testsuite>
</testsuites>
</phpunit>
The problem was the stupidest thing ever.
You can't have whitespace inside the tag.
So what you need to have in you phpunit.xml file is this
...
<testsuite name="StdTestSuite">
<directory>tests/</directory>
</testsuite>
...
Then running
phpunit
Should work

Resources