I Feel Naked Without Tests

Ruby TechStuff PHP

Thu Mar 04 20:35:15 -0800 2010

I’m a ruby guy. I TATFT. I love writing tests, and I love having tests when I need to make changes in the code.

The Old and Busted Nasty Project

I am working on a new project right now unscrambling this nasty swiss-army setup on an old legacy AOLServer. The stuff in question is really just a webserver with “jump pages” that take data off the querystring, rearrange or rename it, add some new data to the querystring, and send the person along to another page or webserver or the online survey they are there to take.

Some of the pages have database access, though. And we don’t want this. We like order and structure, and for good reason.

The code is written in Tcl (seriously unfun). The new design will keep an Apache webserver for pages to munge querystring data, but it will be a dumb host. No database access. That will come from an existing Rails app that will get some shiny new services. The pages on the Apache jump host will be PHP pages because that allows for flexibility to publish a single page for a new survey rather than having to deploy and restart some big app.

So, to recap so far, I am refactoring a Tcl mess into a PHP - Rails exchange.

New Shiny

I spent the morning creating database stuff for this port, and then creating the migrations and models for the rails app. Good stuff. I love models. Who doesn’t?

Next up was starting to get my PHP legs back under me. I relearned stupid things like how to return a value from a PHP function call, how to include a library (or module or whatever it’s called in PHP), and how to use the semicolon key.

But wait, now I’ve got code that will be running in production. Being a ruby guy who is way into testing I was feeling a little weird about just having code that didn’t have any formal unit or functional tests.

So I reached out to my good friend David, who does PHP for a living, and he was quick to reply with PHP Unit. He’s been looking at using it too, and would love to be able to discuss thoughts on testing when I go down to Austin next week for SXSWi. Which I am looking forward to even more now.

PHP Unit Tests!

The installation was really easy. I also had to install PEAR. I setup a test directory like we have in rails. To make sure I had everything installed and wired up correctly I copied in the first example they gave:

require_once 'PHPUnit/Framework.php';

class StackTest extends PHPUnit_Framework_TestCase {
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}

You then run the code from the command line and get your results. Hooray.

Testing My Code

Finally I tested a couple of functions I wrote this afternoon:

/* Library (or module?) with helper functions for jump pages */

function ssid_split($ssid) {
  /* valid formats for ssid: 1_1101110_xx, 1_1101110_xx_1 */
  $ssid_pieces = preg_split('/_/', $ssid);
  return $ssid_pieces;
}
function panel_member_from_ssid($ssid) {
  $panel_member_id = array_shift(ssid_split($ssid));
  return $panel_member_id;
}
function study_number_from_ssid($ssid) {
  $ssid_pieces = ssid_split($ssid);
  $ret = ( count($ssid_pieces) >= 3 ) ? $ssid_pieces[1] : "";
  return $ret;
}

I heart ternary. Here are the tests to go with that:

require_once 'PHPUnit/Framework.php';
include_once('../../lib/jump_functions.php');

class JumpFunctionsTest extends PHPUnit_Framework_TestCase {
 protected function setUp() {
   $this->ssid = "123123_1101110_op";
 }
 public function testSsidSplit() {
   $ary = ssid_split($this->ssid);
   $this->assertEquals(3, count($ary));
   $this->assertEquals("123123", $ary[0]);
   $this->assertEquals("1101110", $ary[1]);
   $this->assertEquals("op", $ary[2]);
 }
 public function testPanelMemberFromSsid() {
   $this->assertEquals("123123", panel_member_from_ssid($this->ssid));
 }
 public function testPanelMemberFromSsid_InvalidSsid() {
   $this->ssid = "123123";
   $this->assertEquals("123123", panel_member_from_ssid($this->ssid));
   $this->assertEquals("", panel_member_from_ssid(""));
 }
 public function testStudyNumberFromSsid() {
   $this->assertEquals("1101110", study_number_from_ssid($this->ssid));
   $this->ssid = "121";
   $this->assertEquals("", study_number_from_ssid($this->ssid));
 }
}

Sooooo….

I’m not looking to write the most awesome PHP ever seen. I just want to wrap this thing up so I can work on something that I actually want to work on. But if I have to write it, I am going to test it. And I am pretty dang happy (so far) with this little discovery. Thanks David!

blog comments powered by Disqus