Ludoo
static bits and pieces

Yesterday I set up to write unit tests for a few PHP classes I'm refactoring. Given my usual tendency to set aside serious tasks and concentrate on eye candy, after a few tests I decided to write my own test runner (something I've done at least twice before, never storing away the code for later use).

My test runner builds on a few assumptions:

The main test runner features are:

I made a few screenshots, showing a collapsed view of all available suites, the same for a single suite, with a warning indicator, and one suite expanded, showing test cases results and warnings.

The DynamicTestRunner code only needs to include the PEAR version of PHPUnit. Using a templating engine would have made the code more understandable, but at the cost of introducing a dependency.

If you want to preserve the option of being able to run each test file on its own, you can test if it has been called by testRunner with a simple if statement at the end of each test file (substituing of course testEntities with the name of the class declared in the file):

if (!isset($_SERVER['SCRIPT_FILENAME']) ||
    $_SERVER['SCRIPT_FILENAME'] == __FILE__) {
    $suite = new PHPUnit_TestSuite('testEntities');
    $result = PHPUnit::run($suite);
    echo $result->toString();
}
ludo ~ Sep 17, 2003 17:26:00 ~ upd Sep 17, 2003 18:27:23 ~ category PHP

Since it was announced on the PEAR newsgroup a few years ago, I have been using the Var_Dump class by Frederic Poeydomenge as one of my favourite PHP development tools. Dumping variables at strategic places has always been a practical and effective (if a bit simple) method of debugging in PHP and other languages. Unfortunately, when you're working with complex data or with class instances, using the echo construct or the var_dump function is not very useful, unless you go to great lengths to extract what you really need to display from your data.

Var_Dump is a class that assists you in exactly this task, extracting all available information from your data and displaying it in a variety of ways. It is the PHP analogous to the Python pprint module, and one could spend an interesting few minutes comparing the two very different PHP and Python solutions to dumping (or pretty printing, which is more what's happening here) data.

In its simplest form, the Var_Dump class can be used through a single static method, and a few optional constants. At the other end of the scale, a Var_Dump instance can be customized through skins, color sets, and display options to tailor its output to your exact needs. In this brief document, we will show examples of its use through a static method, since it's the one you most probably will resort to during development. More complex examples can be found on the author's site.

We start by importing the class code, and setting up a few variables and a class instance, that we will feed to Var_Dump.

require_once 'Var_Dump.php';
$a = array(0,1,2,34);
$b = array('a'=>'something', 'b'=>0, 0=>'b', 'c'=>$a);
class B {}
class A extends B {
    var $a;
    var $b = 1;
    function a($a) {
        $this->a = $a;
    }
}
$a_inst = new A(2);

Once we have that out of the way, we start feeding our data to Var_Dump by calling its display method as a static class method. First the simple array, without passing any extra argument to display.

Var_Dump::display($a);

Our friendly class spits out the following output:

0 Long (0)
1 Long (1)
2 Long (2)
3 Long (34)

Nice, isn't it? Ok, let's start using the optional arguments to display, and feed it something a bit more complex.

Var_Dump::display($b, 'b');

The second argument we passed to display in this example is the name of a key not to display, mainly used to avoid printing deeply nested data structures. As we can see from the following output, Var_Dump does a remarkable job of printing multidimensional arrays.

a String[9]  (something)
b Not parsed ()
0 String[1]  (b)
c Array(4)
    + 0 Long (0)
    | 1 Long (1)
    | 2 Long (2)
    + 3 Long (34)

On to the class instance, and the third argument you can pass to display.

Var_Dump::display($a_inst, null, VAR_DUMP_DISPLAY_MODE_HTML_TABLE);

As you have probably guessed, the third argument controls the output generated by Var_Dump through a few self-explanatory constants:

The output of our instance is pretty interesting, as Var_Dump squeezes all available bits of information from it, including its class and its base classes, if any.

Class nameString[1] (a)
Parent classString[1] (b)
Class vars (2)
aNull (Null)
bLong (1)
Class methods (1)
0String[1] (a)
Object vars (2)
aLong (2)
bLong (1)

The fourth, and last of the arguments you can pass to display is again a constant from a self-explanatory set:

Let's see what happens if we feed our instance to display, by using the VAR_DUMP_NO_CLASS_INFO constant, and reverting to the simpler output.

Var_Dump::display($a_inst, null,
    VAR_DUMP_DISPLAY_MODE_HTML, VAR_DUMP_NO_CLASS_INFO);
Class name   String[1] (a)
Parent class String[1] (b)
Object vars  Array(2) 
    + a Long (2)
    + b Long (1)

That's all for tonight, if you have suggestions for PHP topics you would like to read here, drop me a message.

ludo ~ Sep 04, 2003 23:23:55 ~ upd Sep 05, 2003 00:16:29 ~ category PHP
my feedonfeeds hack

I noticed from my logs that somebody using feedonfeeds is subscribed to my blog. Feedonfeeds is a nice PHP aggregator/reader, written by Steve Minutillo.

After shopping around for a while for a news aggregator, I settled on feedonfeeds a couple of months ago for a couple of reasons:

So I installed feedonfeeds, and staying up to date has become a joy.

Never satisfied, after a couple of days of use I decided I absolutely needed a few features that feedonfeeds was lacking. So I set about refactoring it, and the project turned out to be a full rewrite. As usual with these things, the result is a half-completed app, so that I now have the (minor) features I wanted, and lack most basic ones feedonfeeds provides out of the box. =)

My rewrite is template-based, and factors out most common operations (eg SQL statements etc) in a few classes, instead of using lots of functions. So far, I have:

update: I finally managed to add the most important missing functionality to my feedonfeeds rewrite, and package it.

ludo ~ Sep 03, 2003 13:45:00 ~ upd Jul 10, 2004 20:41:12 ~ category PHP