Can I debug PHPUnit tests in IntelliJ/PhpStorm from devspace or kubectl?

54 Views Asked by At

I have many microservices running locally in a kubernetes/devspace environment.

Based on this and this, IntelliJ and PhpStorm don't support running PHPUnit tests from within devspace or kubectl.

So in the meantime, I am just running a bash script manually as a script via Run | Edit Configurations...:

#!/usr/bin/env bash

devspace enter -c foo php artisan config:clear;

kubectl exec -it deploy/foo -- \
  vendor/bin/phpunit \
  tests/MyExamples/ExampleTest.php -v \
  --log-junit reports/unitreport0.xml \
  -d memory_limit=-1

Is there a better way to do this using devspace? I'd like to be able to at least integrate it with the test runner instead of running a script.

If not, is there any way to extract the current test name or class in IntelliJ/PhpStorm so that I can pass it into the script as a parameter?

Also, is there any way to make the lines clickable in IntelliJ/PhpStorm?

/bar/foo/tests/MyExamples/ExampleTest.php:123

1

There are 1 best solutions below

0
mikebridge On

I think the best way to do this in IntelliJ/PHPStorm is to use sshd. This is well supported in devspace with a tiny amount of configuration.

First, add this line to the devspace.yaml file to set up sshd on your instance:

ssh: {}

Then you should be able to log in via ssh. Note that you will have appended some lines to your .ssh/config file, so you may want to verify these settings:

ssh foo.bar.devspace

Then in IntelliJ, go to FileSettingsToolsSSH Configurations, add a new entry:

Host: foo.bar.devspace

Username: devspace

Port: 10659 (NOTE: confirm this value in your ~/.ssh/config file. The default of 22 is probably incorrect)

Authentication Type: Key pair

Private key file: /Users/<myusername>/.devspace/ssh/id_devspace_ecdsa (replacing myusername with your own user name)

ensure that Parse config file ~/.ssh/config is checked

Then, in FileRemote Development, click on SSH ConnectionNew Connection, then choose the connection we just created from the Connection dropdown (all the other fields should be greyed out), and click Check Connection and Continue. (If this doesn’t work, your shell may be set incorrectly---make sure the $SHELL is not set to something like /sbin/nologin inside devspace).

In the "Choose IDE and Project" dialogue, choose an IDE and version and set the Project directory to some value---it doesn't look like you can set it to be empty, so maybe your home directory will work. You may get some confirmation dialogues from docker desktop as it installs Intellj/PHPStorm on the devspace client.

Run Intellij---it will connect to your devspace environment. You can load your project from the container. It will likely prompt you to install the PHP plugin and restart.

Now you should be able to Run a unit test---click on the Green arrow beside a test to make sure it works. You should also be able to Debug a unit test with a breakpoint.