In my application, I have a service that uses a function related to a model.
This function has already been tested (on its Unit Test), and in my Feature test, I just need to "use" its output value.
Because this function is "complicated", I would mock its value without warry about what the function does. This is the scenario:
Model
class MyModel
{
public function calculateSomething()
{
// Implementation, already unit tested
// Here i put some "crazy" logic (this is not real :) )
if ($this->field_a < 10 || $this->field_b > 15) {
return true;
}
if ($this->field_c !== null || $this->field_e < 50) {
return false;
}
return true;
}
}
In my Service i dont need to re-create those conditions, i just need to say "in this test calculateSomething
will return true", dont care why it return true
Service
class MyService
{
public function myMethod($id)
{
$models = MyModel::all();
foreach($models as $model) {
if ($model->calculateSomething()) {
// Do domething here
} else {
// Do other stuff here
}
}
}
public function myMethodIsolated($model)
{
if ($model->calculateSomething()) {
// Do domething here
} else {
// Do other stuff here
}
}
}
Usually, I mock service, but I never mock a function inside a model, it's possible to mock the function calculateSomething ?
In my example, I provided an isolated version of the function, called myMethodIsolated where I pass the single instance.