Fails Running Unit Tests in Debug Mode in PhpStorm - phpunit

I can successfully run Unit tests via PhpStorm, but when I run them in Debug Mode it fails with following error:
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms.
Tried: host.docker.internal:9000 (through xdebug.client_host/xdebug.client_port) :-(
I use WSL2 on Win10 and Docker Desktop.
BTW, regular debug from browser is working as expected.
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
cacheResult="true"
cacheResultFile="storage/app/.phpunit.result.cache"
stopOnError="false"
stopOnFailure="false">
...
</phpunit>

EDIT: Updated the answer after seeing the phpunit.xml file content
The processIsolation="true" flag in the phpunit.xml file indicates that the tests run in parallel mode. Currently PHPStorm does not support debugging in parallel mode.
In order to use xdebug with phpunit, you should change processIsolation="false".

Related

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.

Why is PhpStorm running tests for only some tests with extra arguments (bootstrap, configuration)?

I am using PhpStorm and for some PHPUnit tests I can right click in the file menu on the left hand side and run some tests and it works.
In the console it shows
/usr/bin/php -dxdebug.coverage_enable=1 /srv/live/vendor/phpunit/phpunit/phpunit --coverage-clover /home/me/.PhpStorm2017.3/system/coverage/live$StringTest.coverage --bootstrap /srv/live/vendor/autoload.php --configuration /srv/live/phpunit.xml tests\src\CoreBundle\Helper\StringTest /srv/live/src/tests/src/CoreBundle/Helper/StringTest.php --teamcity
However, for other tests in another bundle when I right click and run it, the command is
/usr/bin/php /srv/live/src/tests/src/OtherBundle/Classes/Validator/StringLengthTest.php
and the test can't run. I get a class not found error, which I attribute to the command not having the extra arguments as that in the first version above. The other thing I noticed was that the 'Run test with coverage' was only available for the first test, when right clicking in the menu.
Where does this difference originate?
My phpunit.xml file is
<?xml version="1.0"?>
<phpunit cacheTokens="true"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true"
bootstrap="/srv/live/vendor/autoload.php">
<php>
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>/srv/live/src/tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">/srv/live/src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./web/phpunitLog/codeCoverage" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./web/phpunitLog/testdox.html" />
</logging>
</phpunit>
In File -> Settings -> Languages & Frameworks -> PHP -> Test Frameworks it is
Do you have any ideas on why I can run one test ok but not the other - I mean why the difference in the arguments (e.g. -dxdebug.coverage_enable=1)?
The php.ini file hasn't changed.
I found a solution (not sure if it's the best).
If I click Run -> Edit Configurations
Then click the plus sign and add a new PHPUnit configuration, I can choose the bundle where the tests were not working.
Now phpunit runs with the extra arguments when I right click in the file menu.
The thing is, I never did this for the test that was working, so although I found a way to get the failing test to work, I am not totally clear on why I saw the difference in the first place.
Thanks HunterFrazier for putting the wheels in motion.

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.

PHPStorm 8 and PHPUnit problems with #runInSeparateProcess

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.

PHPUnit skips all tests

When I attempt to run PHPUnit, all tests are skipped immediately - there is no error or anything. Can someone point me in the right direction?
[vagrant#vagrant selenium]$ phpunit .
PHPUnit 3.7.28 by Sebastian Bergmann.
Configuration read from /applications/selenium/phpunit.xml
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Time: 61 ms, Memory: 5.50Mb
OK, but incomplete or skipped tests!
Tests: 47, Assertions: 0, Skipped: 50.
My phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
stopOnFailure="false"
backupGlobals="false"
bootstrap="bootstrap.php">
<php>
<const name="BASE_URL" value="http://localhost" />
<const name="SCREENSHOT_PATH" value="/tmp/selenium" />
<const name="SCREENSHOT_URL" value="http://localhost/screenshots" />
<const name="COVERAGE_URL" value="http://localhost/remote/codeCoverage" />
</php>
<selenium>
<browser browser="*firefox"/>
</selenium>
</phpunit>
I finally found this (searching for "skipped all tests" didn't return this result apparently)
PHPUnit :: get message: OK, but incomplete or skipped tests! without any information where is happen
Which suggests I use phpunit --verbose and got the following messages for each test case:
Could not connect to the Selenium Server on localhost:4444.
So I guess that's where my problem lies now.

Resources