Answer the question
In order to leave comments, you need to log in
PHPUnit and generators?
I'm testing the methodWithGenerator method which returns a generator.
How I test:
public function testMethodWithGenerator()
{
$dataSource = $this->getMockBuilder(DataSource::class)
->setMethods(['getSourceLocation'])
->getMock();
$dataSource->expects($this->once())
->method('getSourceLocation')
->willReturn('asd');
$dataSource->methodWithGenerator();
}
public function methodWithGenerator()
{
$this->getSourceLocation();
for ($i=0; $i<=2; $i++) {
yield $i;
}
}
Expectation failed for method name is equal to when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
Answer the question
In order to leave comments, you need to log in
This is because yield does not return a value, but the Generator class: php.net/manual/ru/class.generator.php
New functions are still hard to digest, try (yield $i); or $i = (yield $i); , well, or an analogue - the Generator class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question