Phpstorm Phpunit



Maarten Balliauw March 7, 2013 Since version 3.7, PHPUnit allows us to install the test runner and optional dependencies using Composer. With PhpStorm 6, this workflow is now supported from the IDE. Using both the bundled Composer support and PhpStorm’s unit testing support, we can install PHPUnit via Composer without any hassle. All done, you are ready to run tests directly in PHPStorm. After completing the setup you can now run tests in PHPStorm. If everything is set up correctly you can for example: Right click a phpunit.xml config file and choose Run; Open a test file and run the file or a single test by clicking the green arrow icon; Debugging Tests. In this video I take you through how to setup PhpUnit in PhpStorm. I explain the settings and configurations. Hopefully this setup works for you.Reval Govend. PHPUnit runs the test method with each data set as a separate test. Here is a screenshot of running the test above in PhpStorm: Now you know how to use basic Data Providers. In the rest of the article I will dive into more advanced stuff and tips. Tip #1: Always name the data providers. By default, each data provider is referenced by its array. It's the first video in a series about the usage of PHPUnit and various interpreters in PhpStorm. Here Gary Hockin, PhpStorm Developer Advocate, explains how.

FeaturesPhpStorm

Since version 3.7, PHPUnit allows us to install the test runner and optional dependencies using Composer. With PhpStorm 6, this workflow is now supported from the IDE.

Using both the bundled Composer support and PhpStorm’s unit testing support, we can install PHPUnit via Composer without any hassle. Let’s find out how.

Let’s start off with a blank project. We can right-click the project and use the Composer | Init Composer… context menu to enable Composer support for our project. If you don’t have composer.phar anywhere on your system, the Click here to download from getcomposer.org helper will download the latest composer.phar from the official website.

Once initialized, we can use the new PhpStorm 6 Composer support to add dependencies. We can right-click the project and use the Composer | Add dependency… context menu to download PHPUnit into our project. That’s right: PhpStorm 6 comes with a nice UI for searching packages from the Packagist website.

After successfully installing PHPUnit and all dependencies, our project structure is now the following: the vendor folder containing all dependencies brought in using Composer, including PHPUnit.

Just like with PHPUnit installed using PEAR or as a PHAR file, we need to configure PhpStorm with the location of PHPUnit. Under settings, navigate to the PHP | PHPUnit pane. We can now select the Use custom loader option and specify the path to Composer’s generated autoload.php. Optionally we can specify a default PHPUnit configuration file or a PHPUnit bootstrap file to be used when running tests.

Our run configuration will look pretty simple: specify the directory containing tests (or a specific class/method or rely on the PHPUnit configuration file to find tests to run).

We can now invoke this run configuration and have our unit tests running using the PHPUnit version installed through Composer.

Please download the latest build, provide as much feedback for bugs and feature requests here, and leave questions in the comments below or in our forums!

Develop with pleasure!
– JetBrains Web IDE Team

What I wanted to achieve

As I am using DDEV for most of my projects as simple docker environment for web development and PHPStorm as IDE I wanted to be able to run test from PHPStorm - not only as a script, but fully integrated with coverage and test debugging. Basically, what I wanted to achieve is what you see on the screenshot:

PHPStorm has a pretty good docker integration if we are talking about docker run or docker-compose run. That means, with the PHPStorm docker integration, you can use a docker image to run your tests, however you cannot connect to an existing, running docker container and use that to run your scripts. With DDEV, that is what we would need: We have DDEV running and now want PHPStorm to execute our tests in the DDEV environment.

Solution

Phpstorm Phpunit

While we cannot use PHPStorms docker integration, what we can use is the SSH integration. PHPStorm also offers the option to add a remote interpreter over SSH. This gave me a starting point, so what we need to do to get it running is:

  1. Install an SSH server on our DDEV web container
  2. Make the SSH server accessible from the host
  3. Allow authentication with our private key
  4. Add PHP over SSH as Remote Interpreter in PHPStorm
  5. Add PHPUnit by Remote Interpreter

Displaylink others driver. Let's take a look - step by step:

Install an SSH Server in DDEV

First of all, I added the ssh package to the DDEV web image by adding the following line in DDEV's config.yaml:

Next I added a post-start hook to start the SSH server when starting DDEV in config.yaml:

When starting DDEV for the first time after adding this, the container image will be rebuilt with the SSH package and the SSH server will be started.

Make the SSH server accessible from the host

Dates

To allow connecting to the DDEV SSH server via the host from all platforms we need to forward the SSH port to the host. Hella gutmann driver. To do that, I added a file docker-compose.overwrite.yaml in the .DDEV folder with the following content:

This config makes our DDEV SSH server accessible via port 9922 from the host (can be used with our DDEV site name as host - in my case: site-shop.DDEV.site:9922).

Note
When using Linux based host systems (not Mac, not Windows, not WSL) you can directly access the SSH server via the container IP.
On systems using a virtualization layer in between (Windows/Mac) the IPs cannot be directly accessed - the port solution works around that limitation and allows us to use a more speaking name, too :)

Allow Authentication with our private key

As we mostly have an SSH agent running for all the things we do anyway, we should access our DDEV SSH server with that key, too, instead of using password based authentication. To achieve that, we need to add our public key to the server's authorized_keys file. DDEV has a feature called homeadditions which allows us to add files to our config that will be mounted in the home dir of our DDEV instance. That's perfect. Add the following structure in the folder .ddev/homeadditions:

Then add your public key to the authorized_keys file.

Tip
If you are using DDEV > 1.15 you can add your authorized_keys file to your global home additions. See https://ddev.readthedocs.io/en/stable/users/extend/in-container-configuration/
Otherwise, add the authorized_keys file to the .gitignore - especially if you are working on your projects with multiple people. That way you all can have your keys set up locally.

Phpstorm Install Phpunit

To ensure the file rights on the key are corrected, I extended my post-start hooks with a chmod command (replace the username with your username):

After restarting DDEV you should now be able to connect to your DDEV SSH server with your key. Try it:

Add PHP over SSH as Remote Interpreter in PHPStorm

Now that our SSH server is running, we need to configure our PHPStorm to run PHP via SSH.

  1. Open PHPStorm
  2. Go to Settings > Languages & Frameworks > PHP
  3. Click the tripe dot .. next to CLI interpreters
  4. Click the + sign and choose the From Docker, Vagrant, Vm, WSL, Remote option
  5. Choose SSH and configure the SSH connection
  6. Save all settings

Tip
With this config it's already possible to run all PHP script related things in PHPStorm in the DDEV environment.

Add PHPUnit by Remote Interpreter

As a last step to get our PHPUnit tests running via SSH we need to add a PHPUnit configuration - in PHPStorm:

  1. Go to Settings > Languages & Frameworks > PHP > Testing Frameworks
  2. Choose the SSH PHP interpreter we created in the previous step as CLI Interpreter
  3. Set up path mappings to allow PHPStorm to find the files
  4. Load phpunit via composer autoloader - the file dialog should already display the remote file system
  5. Add a configuration file if necessary

Phpstorm Phpunit Cannot Open File

All done, you are ready to run tests directly in PHPStorm.

Run Tests

After completing the setup you can now run tests in PHPStorm. If everything is set up correctly you can for example:

  • Right click a phpunit.xml config file and choose Run
  • Open a test file and run the file or a single test by clicking the green arrow icon

Debugging Tests

If you want to debug tests or run the tests with coverage, you need to enable xdebug first:

after that, you can use the bug icon in PHPStorm to run the tests with a debugger available.

Bonus: Run composer scripts via ddev

In some projects we are making heavy use of composer scripts as shortcuts for running tests, cgl fixer, PHPStan etc. With the setup we have now, it's easy to also run these scripts via our ddev interpreter. The configuration is similar to the PHPUnit configuration:

Phpunit phpstorm config
  1. Go to Settings > Languages & Frameworks > PHP > Composer
  2. Choose the remote SSH Cli Interpreter
  3. Set up the path mappings
  4. Add composer as composer executable

As a result you will now get the 'little green arrow' in the composer.json file, allowing you to run the scripts in PHPStorm on your DDEV environment with a single click.

Phpstorm Phpunit Version Not Installed

Feedback welcome
That's it for now, if you have any feedback, don't hesitate to contact me via Twitter (@sasunegomo) or TYPO3 Slack (@susi).