43

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)?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
my-nick
  • 691
  • 1
  • 5
  • 11
  • But does it work in command line? – Tokeeen.com Nov 30 '17 at 08:55
  • >php bin/phpunit executes the tests – my-nick Nov 30 '17 at 12:37
  • Also trying to launch via phpstorm, but the setup is quiet weird. What is the value of using phpstorm instead of a terminal console ? (which can be inside phpstorm itself) – Tokeeen.com Nov 30 '17 at 13:27
  • You can run single tests from within the GUI. Just use shortcuts like CTRL-SHIFT-F10 to run specific test/class/directory, and SHIFT-F10 to execute last executed test. Also the output is parsed nicely, so you can actually jump to specific line in file just by clicking in output window. – my-nick Nov 30 '17 at 15:00
  • DX in PHPStorm hurts too much. We don't use the Symfony bridge. – Ryan Mar 19 '19 at 03:59
  • 1. require test-pack, 2. run php bin/phpunit on cli for download, 3. configure phpstorm: "path to phpunit.phar" > 'c:\project\path\bin\phpunit', "default configuration file" > 'c:\project\path\phpunit.xml.dist', 4. run tests - that's it – DerDu Dec 23 '20 at 15:36

5 Answers5

71

What I usually do is point my phpunit testing framework on PHPStorm to the secret .phpunit directory which was created by the bridge, like: enter image description here

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.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Renato Mefi
  • 2,131
  • 1
  • 18
  • 27
  • what should I do if I do not have this secret folder? – syl.fabre Feb 12 '18 at 17:26
  • @syl.fabre then I'm not sure you're using phpunit-bridge – Renato Mefi Mar 13 '18 at 09:35
  • 12
    actually I've found out that this secret folder is created the first time you run `phpunit-bridge`. You should add this to your answer. – syl.fabre Mar 13 '18 at 16:25
  • There's a button in the Phpstorm select file window (top right) so you can "see" the hidden files and directories. – Tokeeen.com Apr 19 '18 at 13:52
  • To clarify it should be noted that the phpunit file in bin/.phpunit/phpunit-(major)-(minor)/ is a file generated by the phpunit-bridge so you are not using the vanilla phpunit – Eydamos Jun 12 '18 at 09:59
  • 1
    This is the right solution, using the bridge instead an only library is "as Symfony point of view" the best choice, this works like a charm, thanks! – Tecnocat Oct 05 '18 at 09:42
  • in addition to @syl.fabre, you run phpunit-bridge from root doing this from the project root: ./vendor/bin/simple-phpunit – roelleor Mar 16 '20 at 14:51
  • 3
    Since the Symfony 5.1, you can also use the following stable path (not version dependent): `vendor/bin/.phpunit/phpunit/bin/simple-phpunit` – Kévin Dunglas Oct 27 '20 at 13:17
29

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)

phpstorm phpunit settings

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

Community
  • 1
  • 1
grexlort
  • 875
  • 1
  • 12
  • 18
  • This also works when configuring WSL as the "PHPUnit by Remote Interpreter" type. Make sure to configure the whole absolute path to `simple-phpunit` and the Test Runner Default configuration file. – Rvanlaak Jun 10 '20 at 18:53
3

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:

// path may differ, at the time being is:
bin/.phpunit/phpunit-6.5-0/phpunit
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
0

The accepted answer didn't work for me using PHPStorm 2020.1 with Symfony 5.2

I found that it works if I apply a blank value for 'Path to phpunit.phar', despite the fact that the preferences dialog complains that '!Path to phpunit.phar is empty'.

So in Preferences > Languages & Frameworks > PHP > Test Frameworks:

enter image description here

redbirdo
  • 4,937
  • 1
  • 30
  • 34
-1

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.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
0TshEL_n1ck
  • 441
  • 3
  • 9
  • 2
    I know that i can use Vendored PHPUnit instead of phpunit.phar, but that is not an improvement. php bin/phpunit (for Symfony4) will not even isntall phpunit in vendor, but in "bin" subdirectory. I was hoping for some clear advice how to configure PhpStorm to actually use symfony/phpunit-bridge not vanilla phppunt – my-nick Nov 30 '17 at 10:22