Thursday, December 20, 2007

Unit testing in PHP

I needed to do some unit testing for some PHP5 utility classes, and wanted some unit test framework. Here are my requirements:

  • Simple. If it's more than one file, something is wrong.
  • Unrestrictive license. I should be able to include the one file in any package I create for distribution, give to clients, whatever.
  • Short namespace. I don't want to type SomeBloatedClass::testIfTrueOnTruesdayOnly(...), when something like assert("foo" == "bar") will do.
  • Easy to run, no need for anything more than a command line

Oddly I couldn't find anything. Lots of links to outdated information. Lots of monster web framework testing things. PHPUnit is baffling. I found out of date versions, links to PEAR that didn't work, etc. And it's huge! Why? Unit testing should be short and sweet.

So screw it: here's my 100-line version. It does just about everything the fat ones do. Here's the "manual" (more samples in source file)

  • Add require_once('UnitTest.php');
  • Create a class that extends UnitTest
  • Make a test by adding a method to the class. The method name must start with test
  • Test values in the test method with assert and assertEquals($val, $expected)
  • Optionally define a public function setup(), public function teardown() functions. These will be called before and after each test.
  • Add at the bottom unittest('classname')
  • Run test with php yourfile
  • Script will return 0, if all tests succeeded, 1 if failures occurred, and 2 if the class couldn't even be loaded

For example:

#!/usr/bin/env php
<?php
require_once('UnitTest.php');

class ATest extends UnitTest
{
 
    public function testPass()
    {
        assert(1 == 1);
    }

    public function testNot()
    {
        assert(1 == 2);
    }

    public function testFail()
    {
        assertEquals("foobar", 10);
        assertEquals("foobar", 10, "optional message goes here";
    }

    public function testError()
    {
        throw new Exception("wtf");
    }
}


// RUN IT
unittest('ATest');
?>

Running this will produce:

$  ./sample.php 
testPass: OK
testNot: Fail : Assertion failed @ line 63 in ATest::testNot
testFail: Fail : foobar != 10 @ line 63 in ATest::testFail
testError: Error : wtf @ line 24 in filename
Total Tests: 4
total = 4, pass = 1, fail = 2, error = 1
$ echo $?
1

Yeah the output isn't so pretty. So... change it! Need more fancy methods? Add 'em! You can modify the base, or subclass it. need "Test Suites" ... use directories!

And go forth and make unit tests!

Oh yeah, improvements and suggestions welcome. One more method might be assertEqualsFloat which would take 2 floats and a tolerance.

Wednesday, December 19, 2007

Google Charts API

I was poking around code.google.com and bumped into the weirdest Google API yet: The Google Charting API. It's how finance.google.com makes it's charts. You give 'em a URL, they give you a good looking graph png image. For instance:

<img src="http://chart.apis.google.com/chart?cht=p3&chd=s:hW&chs=250x100&chl=Hello|World" alt="achart" />

makes this (live example): achart

Ta-Da. Lots of other chart types are available, too. It's good for 50k request per day. And of course you need more than that, your backend process can make the graph and copy it to your local image server.

What's sad is that the quality of the graphs is a lot better than most other graphing/charting solutions out there.

Friday, December 14, 2007

aspell for emacs and Mac OS X

Here's how to add a spelling checker to Emacs on Mac OS X. This will work for both terminal based or GUI based versions.

First, for goodness sake, install MacPorts. After it is installed from terminal do:

$ sudo port -v selfupdate
$ sudo port install aspell-dict-en

You can install other languages too. Take a look at "port search aspell" for the list.

Then just add the following line to your .emacs

(setq ispell-program-name "/opt/local/bin/aspell")

Load you your text (or html or xm) file and do M-x ispell

Oh yeah! You can read more on the Emacs commands here

Tuesday, December 11, 2007

Tamperproof URLs and PHP

I finally posted my slides for "Tamperproof URLS and PHP" from the NYPHP talk on 27-Nov-2007 at http://modp.com/slides/securestring/. In addition the source code and unit test are posted. This is a temporary spot until code.google.com bumps up my resources and then it will go into SVN.

Thank you all for coming. Based on the questions and feedback, I have completely revamped the slides and added a lot more examples. Enjoy! --nickg