PHPUnit Logo

For the last several months, I’ve been using Visual Studio Code for all my PHP projects. I started using Better PHPUnit to run my unit tests because it has a quick keyboard shortcut to run a specific unit test from inside Code. I just set Better PHPUnit up on a new computer I forgot a couple of hoops I had to jump through in order to get it to run inside my Vagrant VM. This post is documentation so I won’t forget and that will hopefully help someone else.

The first step is to determine the name of the SSH user that Vagrant uses to connect through SSH. This is done using the vagrant ssh-config command from your host computer.

Scotts-Air:hdtgmcacher scottkeckwarren$ vagrant ssh-config
==> vagrant: A new version of Vagrant is available: 2.2.6!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html

Host default
  HostName 127.0.0.1
  User ubuntu
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/scottkeckwarren/Projects/hdtgmcacher/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

This is a “duh” statement but the “User” line is what we’re looking for.

The next item we need to know is what IP address our VM is using. This can be found in our Vagrant file in a line that looks like the one below.

config.vm.network "private_network", ip: "192.168.56.101"

Next we need to open our settings.json file inside Visual Studio Code and add the following lines:

"better-phpunit.ssh.enable": true,
"better-phpunit.ssh.paths": {
    "/your/local/path": "/var/www"
},
"better-phpunit.ssh.user": "<user from above>",
"better-phpunit.ssh.host": "<IP address from above>",
"better-phpunit.ssh.port": "22",
"better-phpunit.ssh.binary": "/usr/bin/ssh",

Now this might work but you’ll be prompted for a password each time which is super annoying. To make this process easier I got the value of my public key on my host computer:

less ~/.ssh/id_rsa.pub 

And then I append it to the authorized_keys in my VM:

echo "<value>" >> ~/.ssh/authorized_keys 

Like this post? Don't forget to follow us on Twitter and Facebook for updates.