Question

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.

Was it helpful?

Solution 2

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.)

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top