I've encountered this issue a few times now in my task to add unittests to the project, the issue is that I need to return a function as return value (and cant change the class itself). Small example:
// The code has a:
$thing->doFoo()->getBar();
// So I start the mock:
$thingMock = $this->createMock(Thing::class);
$thingMock->method('doFoo')->willReturn(/* WHAT DO I WRITE HERE? */);
Then part two of the issue, I want to make a helperfunction where I can define the result of getBar
:
private function createThingMock($example){
$thingMock = $this->createMock(Thing::class);
$thingMock->method('doFoo')->willReturn(/* WHAT HERE? I must return $example */);
}
I currently have the following, but it's very bulky after a code format:
private function createThingMock($example){
$thingMock = $this->createMock(Thing::class);
$thingMock->method('doFoo')->willReturn(
new class($options)
{
private $options;
public function __construct($options) {
$this->options = $options;
}
public function getBar(): string
{
return $example ?? 'currently $example is always null';
}
}
);
}
That is the result after a code format and as you can see it's very bulky for something that feels it could be done a lot more simple. Suggestions?