I have a Carpenter
class that does it's work using a Lathe
and a Wood
object.
class Carpenter
{
function Work()
{
$tool = new Lathe();
$material = new Wood();
$tool->Apply($material);
}
}
Lathe
depends on an interface called Material
, so I can easily unit test Lathe
by giving it a fake Material
in my unit test. Wood
doesn't depend on anything, so it can also be easily tested.
interface Material {
// Various methods...
}
interface Tool {
function Apply(Material $m);
}
class Wood implements Material {
// Implementations of Material methods
}
class Lathe {
function Apply(Material $m) {
// Do processing
}
}
However, Carpenter
depends on the concrete classes Lathe
and Wood
because it has to create instances of them. That means that as it currently stands, I cannot unit test the Work()
method without inadvertantly bringing Lathe
and Wood
under test.
How should I change my design to unit test Carpenter
?
There's a couple of different directions you can take here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With