Level 70
@kevdev If you don't need to cross-check with the exact data return type, then you may check the instanceOf. Check whether it returns the right instance or not.
5 likes
Hi I have a simple service that will fetch a data api and then loop it with generator. and i dont have any idea how will i do it. please see below some example of what class that i want to test.
class Service
{
public function fetchData()
{
$response = Http::get('https://external.api/data');
if ($response->status() === 200) {
foreach ($response->json()['data'] as $data) {
yield $data;
}
}
}
public function setup()
{
foreach ($this->fetchData as $value) {
(new ProcessData)->process($value);
}
}
}
@kevdev Yes, that's what I mean.
However, if you need to test implicitly, then you can follow this discussion: https://stackoverflow.com/questions/41343279/mocking-a-function-that-returns-a-generator-in-php-with-phpunit-phake
Please or to participate in this conversation.