Note! This is an involved tutorial, and is focused on Local Classic (Version 3.3.0 and earlier).
We plan on drastically streamlining the PHPUnit experience with Local in future versions.
Requirements
- Local 2.3.0 or newer
- Local Ports Add-on
Step 1: Map the SSH Port
In order to access SSH inside the Local container, port 22
needs to be exposed. This is done using the Local Ports Add-on.
- Navigate to the site in Local
- Click More » Ports
- Add a port for
SSH
pointing to22
- Click “Remap Ports”
-
Note the Host Port of the newly added SSH port. We will reference this later. In this case, it’s
4008
.
Step 2: Install OpenSSH Server in the site’s container:
- Right-click on the site in Local’s sidebar
- Click “Open Site SSH” (If you’re curious, “Open Site SSH” isn’t true SSH, it’s attaching to a TTY)
- Run
apt-get update && apt-get install -y openssh-server
- Run the follow command to tell OpenSSH to start when the site is [re]started:
sed -i "/service mysql start/aservice ssh start" /etc/scripts/startup.sh
- Change the
root
password toroot
by running:passwd
and then enteringroot
twice. - Allow the
root
user to log in with SSH by running the following:sed -i "s/PermitRootLogin without-password/PermitRootLogin yes/" /etc/ssh/sshd_config
- Restart the site in Local
Step 3: Install PHPUnit in the site’s container
Reference: PHPUnit – Make WordPress Core
(If you still have the site’s SSH terminal window open, skip to #3 below)
- Right-click on the site in Local’s sidebar
- Click “Open Site SSH”
- Run
wget https://phar.phpunit.de/phpunit-6.5.phar && chmod +x phpunit-6.5.phar && mv phpunit-6.5.phar /usr/local/bin/phpunit
- Run
phpunit --version
to verify that PHPUnit was installed correctly
Step 4: Configure PhpStorm
- Navigate to PhpStorm » Preferences » Languages & Frameworks » PHP
- Click
...
beside CLI Interpreter to open the following window
-
Click the
+
button and then go to "From Docker, Vagrant, VM, Remote…` (if this option is not available, you may need to enable the Remote PHP Interpreter plugin in PhpStorm) -
Enter the following details:
- Host: This can be found by going to the site in Local and then copying the “Remote Host” option in the “Database” tab
- Port: See Step #1.5 at the beginning of this article
-
User name:
root
-
Password:
root
-
PHP interpreter path:
/usr/local/bin/php
-
Hit OK then Apply
-
Change the PHP language level option to whatever matches the PHP version that the Local site is running
-
Click on
...
beside “Path mapping” -
Map the site’s
public
folder in the project to/app/public
- When you’re done adding the CLI Interpreter and Path Mappings, it should look something like this. Make sure you hit Apply.
-
Go to Languages & Frameworks » PHP » Test Frameworks and click the
+
to add a new Test Framework -
Select the newly created Interpreter
-
Select “Path to phpunit.phar” under “PHPUnit library”
-
Enter
/usr/local/bin/phpunit
for “Path to phpunit.phar”
- Hit Apply and close the Preferences
Using PHPUnit!
In this case I’m checking out wordpress-develop
and running the phpunit.xml.dist
file. See PHPUnit – Make WordPress Core.