Home > Uncategorized > Using watchr with PHPUnit

Using watchr with PHPUnit

I’ve known about watchr and autotest for a while but have never taken the time to set them up.  I should have done this a long time ago, I noticed the efficiency gains immediately.

Initial Setup

Create a local Gemfile and install the necessary gems.

jheth@box1:~$ cd ~/project/src/test/
jheth@box1:~/project/src/test$ cat - > Gemfile << EOF
source :rubygems
gem 'watchr'
gem 'rev'  # Provides event support for Linux
EOF
jheth@box1:~/project/src/test$ bundle install --path=vendor --binstubs

This will install the watchr gem into a local vendor folder and create ~/project/src/test/bin which will contain the watchr executable script.

Configure Script

Next, configure the watchr.rb script.  You can configure any number of watches for files and/or folders.  The watch() function takes a regex with any number of captures to grab filename or parts of the path. The ‘do’ block is executed whenever a file changes. You can change the phpunit parameters to whatever you want.

~/project/src/test/watchr.rb
# Watch test files
watch("src/test/model/(.*\.php)") do |md|
   system("cd src/test/model && phpunit --debug --colors '#{md[1]}'")
end
# Watch src objects
watch("src/main/model/(.*)\.php") do |md|
   system("cd src/test/model && phpunit --debug --colors '#{md[1]}Test.php'")
end

Notice that these paths are RELATIVE, which means you must run this script from ~/project.  I had trouble with both absolute and relative (../) style paths so I just decided to run it from a higher level folder.

cd ~/project && ./src/test/bin/watchr ./src/test/watchr.rb

To see what files are being watched you can use the -l argument. This only prints the names, it doesn’t actually begin watching.

./src/test/bin/watchr -l ./src/test/watchr.rb

Fire this up in a separate terminal and let it just sit there all day while you edit your files. You will see the phpunit output everytime you save a file. I turned on –colors and –debug so I don’t have to guess when something goes wrong.

Wrap Up

This has already saved me a ton of time since I would previously switch between terminal sessions, typing or arrowing to the phpunit command I wanted to run. Now I never have to leave the editor and when saving either the test or the underlying source file, the tests get run. Decreasing the amount of time between edits and test execution is a huge time saver. I get instant feedback every time I save.

The startup command is a bit annoying to type so I added an alias to my .bash_profile.  No matter where I am, I can just pick a terminal and type ‘watchem’ and I’m up and running.

alias watchem='cd ~/project/ && ./src/test/bin/watchr ./src/test/watchr.rb'
Advertisements
Categories: Uncategorized Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: