Test your code with every change in your PHP files

Applying TDD to your PHP code is a big improve in your code as forces you to “think” the code before write it down. A common approach to TDD workflow could be:

  1. Write your test class
  2. Write your code that make pass the tests
  3. Refactor the code

If you take a closer look, between all the steps you MUST run all the tests for possible functionality breaks with every single change in the code, and make it manually could be a little disturbing.

So for this there are  tools that helps you track your code for changes and if changed will automatically run a bunch of actions. Some time ago I wrote an article about this topic but using Ruby language. ## Watchr ruby gem Ruby language has a huge background with TDD and testing in general and you could find simple, but powerful, tools that will help you. Watchr is an agile development tool that monitors a directory tree, and triggers a user defined action whenever an observed file is modified. Its most typical use is continuous testing, and as such it is a more flexible alternative to autotest.

Installing watchr

If you have rugygems installed in your system is as simple as execute:

gem install watchr

If you’re on Linux/BSD and have the rev gem installed, Watchr will detect it and use it automatically. This will make Watchr evented.

gem install rev

You can get the same evented behaviour on OS X by installing ruby-fsevent.

gem install ruby-fsevent

Executing watchr in you PHPUnit based code is as simple as create a configuration file (i.e. watchr.rb) with the next content:

watch( '(app/.*\.php)$' ) {|md| system("phpunit -c app/phpunit.xml.dist") }

After that you can execute in your terminal:

watchr watchr.rb

This will detect file changes in your php files and execute the phpunit command against them . As you can see a simple tool but that could increase your testing speed by keeping focus in your code. UPDATE: I think that is an undergoing library called ResourceWatcher that will implement this with PHP code, so I will be stay tunned to it.