I had problems with configuring PhpStorm IDE to use http://symfony.com/doc/current/components/phpunit_bridge.html while working with Symfony 3.3.
I decided to just download phpunit.phar
to bin
and use it instead.
Symfony 3.4 (and Symfony 4), does not even have phpunit.xml.dist
out of the box, so there is a problem with using phpunit.phar
easily.
I've installed PHPUnit using flex:
composer req phpunit
That created phpunit.xml.dist and I was able to run tests from command line by:
php bin/phpunit
But again I could not make PhpStorm use it.
So I downloaded phpunit.phar
and it can work together with provided phpunit.xml.dist.
Question 1: Is there any way for PhpStorm IDE to use phpunit-bridge?
Question 2: What is the best practice for Symfony 4 (phpunit-bridge or vanilla phpunit.phar)?
It is very simple and doesn't matter version of Symfony, because of the concept of testing still stable. At first, you need to configure test framework on the PHPstorm preferences (screenshot), in your case, you can to use Vendored PHPUnit or downloaded manually, you can join in the
PHPUnit library
section. Then you need to addRun/Debug
configuration."phpunit/phpunit"
and"symfony/phpunit-bridge"
as I see was installed, this is all.Aside given answer, it's worth mentioning that the secret
.phpunit
directory won't appear out of thin air.After
composer req phpunit
, one has to run thephpunit
script first, eg.:which will download a local copy of the PHPUnit and place in the same folder, so the path to the phar executable will be:
I manage to run symfony/phpunit-bridge with success using this configuration:
Steps:
after
composer require --dev symfony/test-pack
i have in dir/vendor/bin
filesimple-phpunit
which should runsymfony/phpunit-bridge
just fine.Then in PhpStorm in
File | Settings | Languages & Frameworks | PHP | Test Frameworks
set:Path to phpunit.phar
optionPath to phpunit.phar
to absolute path ofsimple-phpunit
file (e.g/application/vendor/bin/simple-phpunit
)Default configuration file:
and set input value to absolute localization of yourphpunit.xml.dist
(in my case/application/phpunit.xml.dist
)note:
phpunit.xml.dist
file should be configured to usesymfony/phpunit-bridge
- check https://symfony.com/doc/current/components/phpunit_bridge.htmlClick Appply/Ok and now you can run tests from PhpStorm interface
What I usually do is point my phpunit testing framework on PHPStorm to the secret
.phpunit
directory which was created by the bridge, like:The location of the "phar" file is:
or in some cases:
After this, the specified
phpunit
executable will be called correctly when exeuting unit-tests, but with a--no-configuration
option. This can cause autoloading problems (a lot of "class not found" errors), because the autoloader generated by Composer is not specified anywhere.To fix this, you should have a
phpunit.xml
file in your project (this is common practice anyway), in which you specify Composer's autoloader, something like this:This
phpunit.xml
should then be specified in the "Default configuration file" option and you should be good to go.Regarding phpstorm using phpunit-bridge: It's possible as a custom script, but you won't have the nice interface and the possibility to run (and debug) specific tests via PHPStorm interface.