Category: Uncategorized

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!

Off to php|tek 2008!

For those of you that read my blog and happen to be attending php|tek 2008, I’m heading up that way today! Over the past few weeks, I’ve received a handful of Twitter messages and other emails regarding batch processing with PHP. I look forward to seeing the few curious, interested people there. Don’t hesitate to stop by and say hello.

I’ll be presenting on “Robust Batch Processing with PHP,” as well as my “The Grown-Up Company’s Guide to Development,” which has been spruced up a bit post-ZendCon 2007 (read: “made less boring”). Happy conferencing, everyone! Ayyyyyyyyy!

Congrats! Community Connect Inc. sells to Radio One for $38 million

Just a quick “congratulations” to my former employer, Community Connect Inc., for their recent sale to Radio One.

Congrats, CCI!

“The Grown-Up Company’s Guide to Development” on ZendCon Sessions

One of my talks from ZendCon 2007, “The Grown-Up Company’s Guide to Development,” has been posted to the ZendCon Sessions.

If you missed my talk and are interested, head on over there to check it out!

Is it a coincidence that everyone’s favorite Zend pal, Cal Evans, posted it on April Fool’s Day? That’s for you to decide. :) Thanks, Cal!

“Eyyyyyyy!”: ZendCon 2007, day #1/4

Well, ZendCon day #1 has drawn to a close. By day #1, I mean that I sat in on Sara Golemon, Wez Furlong‘s, and Marcus Boerger‘s tutorial on “Extending PHP.” Reading Sara’s book is one thing, but hearing it first hand from the masters themselves is another.

Also, a little later in the day, my colleagues from Schematic showed up, along with some former colleagues from Community Connect Inc., so quite a good day overall.

Now I’m inspired to write an extension…what’s it gonna’ be? :)

Tomorrow at 4 PM PST: Ben “It’s An Honor” Ramsey and I speak on “Mobilizing and Sharing: How Zend Framework Builds Community for Nokia MOSH.” Good luck to us!

See you all tomorrow….

WordPress Themes