views:

21

answers:

1

I'm using PHP 5.3 and SimpleTest, but more general answers are welcome. Each plug-in will be a class which extends an abstract class... how do I test that this interface works properly? Do I have to create several plug-ins and test them? Or is there a more satisfying way?

As an example, imagine writing something to represent money. Users can extend this with different currency classes.

abstract class Money
{
private static $symbol;
private static $num_decimals;

public function __construct($amount) { ...}
public function __toString() { ... }
}

Then a plugin would look like this:

class GBPound extends Money
{
private static $symbol = "£";
private static $num_decimals = 2;
}

Perhaps I could emulate multiple inheritance, extending the UnitTest class and the money class, but that could get messy!

It's so difficult because unit testing is about testing the interface, but the class itself is the plug-in interface.

+1  A: 

Not sure about simpletest, but PHPUnit can create mockups of abstract classes, that allow you to test them directly.

Mchl
Thanks for the answer. I'm still not entirely sure what the test would look like though. The static variables are set statically in-line for each plugin, and can't be changed. I would want to test: static::$symbol="£" and static::$num_decimals=2, self::toString()=="£3.24". The only way I can think of doing this is to create a plugin or two which are purely for testing purposes.
Nathan MacInnes
There's no point in testing `static::$symbol="£"` since that would be testing if PHP itself works, which is not your job. You need to assume it works, and that the users of your class will extend it correctly. Your test should concentrate on testing __toString() method, which can be done either by creating a 'fake' plugin, or using PHPUnit's ability to generate mockups.
Mchl
Yeah, that's what I meant. Hence the = and not ==. Should have added $obj->amount=3.24 (or whatever the interface would be). I'll have a look into PHPUnit. I decided against it originally (this is my first TDD project in PHP) because I wanted to get up-and-running quickly and PHPUnit looked like it had a steeper learning curve than SimpleTest.
Nathan MacInnes