Answer the question
In order to leave comments, you need to log in
How to mock one class function in phpunit and leave the implementation of all the others (including the one being tested)?
I'm just getting started with testing. The task was to test the model function that calls the database update functions. I read that testing the database directly is quite difficult and decided to just mock the method that updates the values in the database. Here is an example code of what I mean:
class Model {
// метод который нужно мокнуть
public function update($values)
{
global $db;
$db->query('update ...');
}
// функция которую нужно протестить
public function change_time($start_time)
{
$end_time = $start_time + 1;
$this->update([
'start_time', $start_time,
'end_time', $end_time
]);
}
}
public function testEndTimeShouldBeCorrect()
{
$mock = $this->getMockBuilder(Model::class)
->setMethods(['update'])
->getMock();
$mock->expects($this->once())
->method('update')
->with($this->equalTo(5));
Model::change_time(4);
}
Answer the question
In order to leave comments, you need to log in
Do not use global variables, but inject a dependency, then you can change the object and test what you need.
Probably it is possible to return data to be checked in the change_time function. But I think it will look illogical from the point of view of the program logic, and this is not worth sacrificing for the sake of tests.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question