Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method that allows defining a set of arguments the mock should yield #1133

Merged
merged 2 commits into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Change Log

## 1.4.4 (TBC)
* Add method that allows defining a set of arguments the mock should yield #1133

## 1.4.3 (2021-02-24)
* Fixes calls to fetchMock before initialisation #1113
* Allow shouldIgnoreMissing() to behave in a recursive fashion #1097
Expand Down
19 changes: 19 additions & 0 deletions library/Mockery/Expectation.php
Expand Up @@ -664,6 +664,25 @@ public function andSet($name, ...$values)
return $this;
}

/**
* Sets up a closure that will yield each of the provided args
*
* @param mixed ...$args
* @return self
*/
public function andYield(...$args)
{
$this->_closureQueue = [
static function () use ($args) {
foreach ($args as $arg) {
yield $arg;
}
},
];

return $this;
}

/**
* Alias to andSet(). Allows the natural English construct
* - set('foo', 'bar')->andReturn('bar')
Expand Down
6 changes: 6 additions & 0 deletions tests/Mockery/ExpectationTest.php
Expand Up @@ -285,6 +285,12 @@ public function testReturnsValuesSetAsArray()
$this->assertEquals(3, $this->mock->foo());
}

public function testAndYield(): void
{
$this->mock->shouldReceive('foo')->andYield(1, 2, 3);
self::assertSame([1, 2, 3], iterator_to_array($this->mock->foo()));
}

public function testThrowsException()
{
$this->mock->shouldReceive('foo')->andThrow(new OutOfBoundsException());
Expand Down