phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

Add willYield feature to Method Prophecy #435

Closed tkotosz closed 5 years ago

tkotosz commented 5 years ago

Adds willYield functionality. Usage:

function it_should_do_something(MyCollaborator $myCollaborator) {
    // arrange
    $myCollaborator->generate()->willYield(['foo', 'bar']);
    $this->beConstructedWith($myCollaborator);

    // act
    $result = $this->doSomething();

    // assert
    $result->shouldBe('foo/bar');
}

Depends on https://github.com/phpspec/prophecy/pull/434 (will rebase this PR once the other one merged)

Taluu commented 5 years ago

How about willYield($value1, $value2, ...) instead of willYield([$value1, $value2, ...]) ?

tkotosz commented 5 years ago

The shouldYield expects array if I remember correctly, so I thought best to expect array here as well.

antonfries commented 5 years ago

Is this already merged to master or in near future? I need exactly this in one of my bigger projects.

tkotosz commented 5 years ago

@stof FYI the test changes (fixing the build) are in a separate PR originally https://github.com/phpspec/prophecy/pull/434 (so I assume that one need to be reviewed and finalized) before this one can go forward

tkotosz commented 5 years ago

@antonfries In the meantime while this is not merged you can get around the issue by doing this:

Mock method to yield 0 items:

$collaborator->generate()->will(function() {
    if (false) { // yield nothing
        yield;
     }
});

Mock method to yield some items:

$collaborator->generate()->will(function() {
    yield MySuperItem::fromInt(1);
    yield MySuperItem::fromInt(2);
});
antonfries commented 5 years ago

@tkotosz Thanks, that will help me for the first infrastructure parts!

tkotosz commented 5 years ago

@stof rebased this PR from https://github.com/phpspec/prophecy/pull/434 , fixed the issues related this PR and squashed commits. Can you have a look again?

tkotosz commented 5 years ago

@stof rebased this PR from master now since the other PR has been merged :)

tkotosz commented 5 years ago

@stof rebased this PR from master again, to apply the recent build fix

ciaranmcnulty commented 5 years ago

This looks really useful, thanks

tkotosz commented 5 years ago

Thanks for the merge @ciaranmcnulty :tada: