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…
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
- Set the
CLI interpreter
using the...
button - Hit + to add a new one and select
From Docker, Vagrant etc etc
- Select the
Docker Compose
radio button - Add a server by hitting the
New…
button (Docker for Mac, on a macbook) - Choose the service from the dropdown, for the back-end Sirius project this is
api-app
- Hit
OK
to save
- Back on the main screen, hit the little folder icon to edit the path mappings
- Add in your local path, eg
/Users/my_username/opg-sirius/back-end
- Add the equivalent remote path:
/var/www
Tell PHPStorm how to run the PHPUnit tests
PHPStorm > Preferences > Languages & Frameworks > PHP > Test Frameworks
+
to add new- Choose
PHPUnit by Remote Interpreter
- Choose your project interpreter, eg
api-app
- In the PHPUnit Library section choose
use Composer autoloader
radio button and set the path to the script as/var/www/vendor/autoload.php
- 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
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.
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
+
to add new- Choose
Behat by Remote Interpreter
- Choose your project interpreter, eg
api-app
- Set
Path to Behat executable
to/var/www/vendor/bin/behat
- Press the refresh button next to the path and check that it finds an appropriate Behat version
- Check
Default configuraton file
and set the value to/var/www/tests/behat.yml
Run the Behat tests!
- Ensure a local environment is up and running. Usually with
make dev-up
- 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
- Wait for the
Edit Configuration
dialog to open - Set the
Interpreter
toapi-app
- 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 - 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