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:
- Fixes method opening brace placements from
public function testFoo() {
topublic function testFoo()[NEWLINE + 4 SPACES]{
- Changes all
$this->object
to$this->_object
- Changes
protected $object
toprotected $_object
- 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!