Mocking a function that returns a Generator in PHP with PHPUnit/Phake
Thank you Oliver Maksimovic for your comment, which has helped me in finding a solution that works for me.
I've decided to create the following function on my base testcase:
/*
* @param array @array
*
* @return \Generator|[]
*/
protected function arrayAsGenerator(array $array)
{
foreach ($array as $item) {
yield $item;
}
}
This allows me to do the following:
$mocked_instance = Phake::partialMock(MyInterface::class);
$numbers = [1, 2, 3, 4, 5];
Phake::when($mocked_instance)
->yieldData()
->thenReturn($this->arrayAsGenerator($numbers));