Vim mapping: Bringing PHPUnit skeleton test class code up to Zend Framework standards

I write a lot of unit tests. I like to apply our coding standards, which are based on Zend Framework coding standards, to these test classes.

I also hate writing the skeleton test classes from scratch, especially with PHPUnit will do this for me. You just have to run:

phpunit --skeleton-test Your_Class

...and it generates:

PHPUnit x.y.z by Sebastian Bergmann.

Wrote skeleton for "Your_Class" to "./Your_ClassTest.php".

Fresh off of the skeleton generation, the test class will have a few little things in it that are against Zend Framework standards. The Vim map below fixes these standards violations:

  1. Fixes method opening brace placements from public function testFoo() { to public function testFoo()[NEWLINE + 4 SPACES]{
  2. Changes all $this->object to $this->_object
  3. Changes protected $object to protected $_object
  4. Changes comments "@todo Implement testFoo()" to "Basic test of testFoo()" with a "@return void"; note that you should write more appropriate comments as needed!

Place this in your ~/.vimrc file:

map zfunit :%s/function test\(.*\) {/function test\1\r    {/g \
    \|%s/@todo Implement \(.*\)$/Basic test for \1\r     *\r     * @return void/g \
    \|%s/\$object/\$_object/g \
    \|%s/this->object/this->_object/g

To run it, just open your offending class file in vim and type zfunit. You will then see output of:

X substitutions on X lines
Y substitutions on Y lines

Take a look at your file now -- voila! All fixed. No manual moving of those annoyances to conform to the standards.

Enjoy!

NOTE: The above applies to usage of PHPUnit with the default skeleton template. You can provide alternate templates in your PHPUnit/Util/Skeleton/Template directory by adding your equivalent *.tpl file for the particular template to address the issues outlined here. Thanks for keeping me in line, Sebastian!