Running PHPUnit from Eclipse on a remote system

For the last few years I’ve been using virtual machine to keep my dev Linux isolated from my desktop. Recently I decided to improve my test driven development by integrating PHPUnit with Eclipse. I didn’t want to waste time on switching between IDE and console to run a test. The challenge is to create an external tool which will SSH into the remote server, run tests and feedback to Eclipse. In fact this method lets you do anything on the remote server, not only unit test.

1. Download openSSH
If you develop on Windows as I do the first step is to install command line SSH client (putty won’t work). Go to http://sshwindows.sourceforge.net/ and install openssh.

2. Generate SSH keys
Once that done login on your remote server and generate SSH keys. It’s required in order to automate the process – you don’t want to be prompted about password each time you run a test. In fact Eclipse doesn’t handle STDIN so you have no choice. If you want to learn more about SSH keys go to https://wiki.archlinux.org/index.php/SSH_Keys.

$ ssh-keygen -t rsa 

Generating public/private rsa key pair.
Enter file in which to save the key (/home/lukasz/.ssh/id_rsa): eclipse.key
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:
Your identification has been saved in eclipse.key.
Your public key has been saved in eclipse.key.pub.

$ mkdir .ssh
$ cat eclipse.key.pub >> .ssh/authorized_keys
$ chmod 400 .ssh/authorized_keys

I leave the passphrase empty to make it easier. Copy the private key (eclipse.key) to your desktop. I keep it on G: drive.

Run your desktop console to try the key and confirm server’s finger print. Start –> Run –> cmd [enter].

ssh -iG:eclipse.key [email protected]

Obviously the IP and the username should be change accordingly to your setup.

3. Create a batch script
Next what we need is a small batch script which will be executed by Eclipse. I called it eclipse-external.bat and put it into my project. If your path to OpenSSH is different please change it.

@ECHO OFF
set file=%4
set file=%file:=/%
"C:Program Files (x86)OpenSSHbinssh.exe" %1 %2 %3 %file%

There is no magic here. The script executes SSH and takes 4 arguments. First one is a path to the ssh key. Second is username(at)address. Third is a bash script on the server and the last one will be a file name to be unit tested. Just because I develop on windows I need to replace all “” in a file name with “/”.

4. Create a bash script
We need one more script. This time on the remote server.

$ mkdir eclipse
$ vim eclipse/unittest.sh
$ chmod +x eclipse/unittest.sh

unittest.sh:

#!/bin/bash
FNAME=$1
cd /PathToYourProject/tests/
if [ $FNAME == ${FNAME##*/tests/} ];
then
    phpunit
else
    phpunit ${FNAME##*/tests/}
fi

5. Eclipse setup
All the hard work is done. Now it’s the time to put it all together in Eclipse.

Go to Run –> External Tools –> External Tools Configurations
eclipse1

eclipse2

Set location to your batch script. In the arguments window paste:

-iG:eclipse.key
[email protected]
./eclipse/unittest.sh
${resource_path}

Obviously the second line needs to be changed.

${resource_path} is the active file. If you want to run a particular test make it active then run the config. If a selected file is not a test all test will be run.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s