Skip to content

Commit

Permalink
Merge pull request #1133 from Blacksmoke16/and-yield
Browse files Browse the repository at this point in the history
Add method that allows defining a set of arguments the mock should yield
  • Loading branch information
davedevelopment committed Jul 12, 2021
2 parents 002ca46 + 66a71a1 commit b02619d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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

0 comments on commit b02619d

Please sign in to comment.