S
S
Shirshov Alexander2016-05-08 22:20:07
PHP
Shirshov Alexander, 2016-05-08 22:20:07

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();
    }

What I'm testing:
public function methodWithGenerator()
    {
        $this->getSourceLocation();
        for ($i=0; $i<=2; $i++) {
            yield $i;
        }
    }

I am getting an error:
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.

Version PHPUnit 5.3.2
If yield is replaced by return then the test passes! Has anyone come across?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Muhammad, 2016-05-08
@Keanor

This is because yield does not return a value, but the Generator class: php.net/manual/ru/class.generator.php

A
Anton Shamanov, 2016-05-08
@SilenceOfWinter

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 question

Ask a Question

731 491 924 answers to any question