开发者

How do I test my plug-in interface which uses an abstract class?

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_decimal开发者_C百科s = 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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜