PHP Traits for Interface Contract Testing

Interfaces, those neat artifacts created to represent contracts to be fulfilled by classes. When appropriately used, they’re a very powerful way to express the expectations a client class has on implementations. Yet, most languages lack the tools to express those aspects that you can’t see by looking at the method signatures.

What should a method return under certain circumstances? If condition X is met, should it throw an exception? If so, of what type? All those questions can be answered by writing good documentation, that’s true, but, what about enforcing those rules? And who writes good docs in a world where working code is more important than documented code? That’s where the documenting facet of unit tests come in handy.