I am writing a class that will inherit an interface. The client code will be written for that interface, and the class written to support it. The idea is that I will later write other classes for that interface, and objects of those two different classes should be completely interchangeable. Rather than writing a test class for that first class, I want to write one for the interface.
My plan was to write a test class that would take a factory object for the constructor (dependency injection), and use the factory to create new instances of the class under test.
That way, if I wanted to test ClassA, I could pass a ClassAFactory object to the constructor of the test class, and if I wanted to test ClassB, I would pass a ClassBFactory object. Both classes are designed to be interchangeable, and since only public methods are supposed to be tested, this seems ideal.
But what about testing the constructor? Would I be better writing an abstract test class and implmenting constructor tests in classes that inherit the abstract test class (different classes may be instantiated differently)?
If I did use the first idea, I guess I would have a test class for each class being tested, like:
class ClassATest extends [PHPUnit test case]
{
$myFactory = new ClassAFactory();
$myTest = new ClassTest($myFactory);
$myTest->test1();
$myTest->test2();
//etc.
}
What's the best way to go about this? I want to have a general test, so that when I write new classes to implement the common interface, I can just put an object of the same tests as used for the others. But, seeing as the different classes would have different constructors, perhaps writing an abstract test class and extending it for each new object would be better? What do you think?