2
votes

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.

2
What is the problem? Why can't you just test GBPound class? - NOtherDev
Because I want to test the ability to make the GBPound class, or any other combination of classes. If the class has users in Angola, I want them to be able to use the interface to create an ANKwanza class. - Nathan MacInnes
Why would you need GBPound anyway? GBPound is not so special that it couldnt couldnt be represented with Money alone. Just make $symbol and $num_decimals arguments required by the constructor and store them non-static. Also you might want to rename Money to Currency. - Gordon
It's just an example. My actual plug-in interface has nothing to do with money, it's just a lot more complex to explain. - Nathan MacInnes

2 Answers

1
votes

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

0
votes

I decided to go with the idea of testing the interface by creating an empty plug-in class. To me this seems much cleaner than mocking an abstract class. (However I am in the process of migrating all my tests over to PHPUnit for other reasons.)