As the title says, I’m trying to understand how to unit test (or maybe some other sort of test) a framework that is built similar to FuelPHP where you utilize static calls to set and access everything.
This leads to code like:
class Appointment {
public static function updateStartTime($binds) {
$query = "UPDATE appointment SET start_time = :start_time
WHERE appointment_id = :appointment_id";
return DB::update($query, $binds);
}
}
And you end up with controller code that looks like:
$result = Appointment::updateStartTime(array(
'start_time' => '10:00 am',
'appointment_id' => 10
));
So to test the Appointment::updateStartTime method, I need to be able to provide a fake DB::update method to it, but I’m not sure where to start on that.
I know that I could just include a mock class as part of the test suite:
class DB {
public static function update($params) { return 'success'; }
}
But this seems tedious. Is there a better way to add test coverage to my framework, while still using this coding style?
Static Methods are Death to Testability. PHPUnit offers some mocks for static class functions but this works only under certain conditions (only possible within classes themselves, classes must use late static binding, see Stubbing and Mocking Static Methods).
So generally I would say: Don’t test the framework, this should be done within the framework community. Instead just don’t use static classes in your own code, so you can unit-test your own code.