How to configure PhpStorm to use symfony/phpunit-bridge

南楼画角 提交于 2019-12-02 19:00:11
Renato Mefi

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:

bin/.phpunit/phpunit-(major).(minor)/phpunit

or in some cases:

vendor/bin/.phpunit/phpunit-(major).(minor)/phpunit

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:

<phpunit bootstrap="vendor/autoload.php">

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.

I manage to run symfony/phpunit-bridge with success using this configuration:

  • PhpStorm 2018.2.5
  • Symfony 4.1.x
  • PHP 7.1 running on docker
  • "symfony/test-pack": "^1.0"

Steps:

after composer require --dev symfony/test-pack i have in dir /vendor/bin file simple-phpunit which should run symfony/phpunit-bridge just fine.

Then in PhpStorm in File | Settings | Languages & Frameworks | PHP | Test Frameworks set:

  • radio select to Path to phpunit.phar option
  • Path to phpunit.phar to absolute path of simple-phpunit file (e.g /application/vendor/bin/simple-phpunit)
  • check Default configuration file: and set input value to absolute localization of your phpunit.xml.dist (in my case /application/phpunit.xml.dist)

note: phpunit.xml.dist file should be configured to use symfony/phpunit-bridge - check https://symfony.com/doc/current/components/phpunit_bridge.html

Click Appply/Ok and now you can run tests from PhpStorm interface

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 the phpunit script first, eg.:

bin/phpunit

which will download a local copy of the PHPUnit and place it in the same folder, so the path to the phar executable will be:

bin/.phpunit/phpunit-6.5/phpunit
0TshEL_n1ck

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 add Run/Debug configuration. "phpunit/phpunit" and "symfony/phpunit-bridge" as I see was installed, this is all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!