Skip to main content

Running tests in PHPStorm

Prerequisites

PHPStorm

Docker

Sirius project up and running

Hook PHPStorm up to Docker

PHPStorm

PHPStorm > Preferences > Build, Execute and Deployment > Docker

Hit + to add new. This should just magically detect Docker running on your machine, which is lovely. It used to be a lot harder than this…

Docker preferences

Set up the project interpreter

We need the IDE to use the resources in the docker container - not on your local machine - so we need to tell it where all the stuff is, like PHP, PHPUnit, Behat etc etc.

First, PHP:

PHPStorm > Preferences > Languages & Frameworks > PHP

  1. Set the CLI interpreter using the ... button
  2. Hit + to add a new one and select From Docker, Vagrant etc etc
  3. Select the Docker Compose radio button
  4. Add a server by hitting the New… button (Docker for Mac, on a macbook)
  5. Choose the service from the dropdown, for the back-end Sirius project this is api-app
  6. Hit OK to save

CLI Interpreter preferences

  1. Back on the main screen, hit the little folder icon to edit the path mappings
  2. Add in your local path, eg /Users/my_username/opg-sirius/back-end
  3. Add the equivalent remote path: /var/www

CLI Interpreter preferences

Tell PHPStorm how to run the PHPUnit tests

PHPStorm > Preferences > Languages & Frameworks > PHP > Test Frameworks

  1. + to add new
  2. Choose PHPUnit by Remote Interpreter
  3. Choose your project interpreter, eg api-app
  4. In the PHPUnit Library section choose use Composer autoloader radio button and set the path to the script as /var/www/vendor/autoload.php
  5. Click the little swirly arrow icon to make it search for the file, it should return a little message with the version number if it finds it 6 In the Test Runner section, set the default configuration file to /var/www/tests/phpunit.dev.xml

CLI Interpreter preferences

Run the unit tests!

This is the nice bit, right click on something that has tests in to run all of them at once, or find a test class and right click (or use the little green arrows) to run just that test.

CLI Interpreter preferences CLI Interpreter preferences

Run the unit tests with debugging or coverage

Note: If coverage is taking a long time, make sure you are running with the phpunit.dev.xml. It’s usually due to the addUncoveredFilesFromWhitelist="true" building coverage reports for unrelated code

If you want to run with coverage or debugging you currently need to rebuild the container with xdebug installed. This can be done by running make build-api-debug

Tell PHPStorm how to run Behat

PHPStorm > Preferences > Languages & Frameworks > PHP > Test Frameworks

  1. + to add new
  2. Choose Behat by Remote Interpreter
  3. Choose your project interpreter, eg api-app
  4. Set Path to Behat executable to /var/www/vendor/bin/behat
  5. Press the refresh button next to the path and check that it finds an appropriate Behat version
  6. Check Default configuraton file and set the value to /var/www/tests/behat.yml

Test framework behat preferences

Run the Behat tests!

  1. Ensure a local environment is up and running. Usually with make dev-up
  2. Right click on something that has behat tests in to run all of them at once, or find a test in a feature file and right click (or use the little green arrows) to run just that test
  3. Wait for the Edit Configuration dialog to open
  4. Set the Interpreter to api-app
  5. Set the behat profile in the Test Runner options to something appropriate for the test e.g. --profile=apiV1. The right profile can usually be found by looking at the profile used in the Makefile
  6. Click Ok and the test will run

If the test has been run previously it will run without opening the configuration dialog, in which case you may need to use Edit configurations... in the dropdown usually at the top of the PHPStorm window

Example behat configuration

This page was last reviewed on 3 March 2021. It needs to be reviewed again on 3 September 2021 by the page owner #opg-sirius-develop .
This page was set to be reviewed before 3 September 2021 by the page owner #opg-sirius-develop. This might mean the content is out of date.