Skip to content

Commit

Permalink
Merge pull request #435 from tkotosz/feature/add-will-yield-to-method…
Browse files Browse the repository at this point in the history
…-prophecy

Add willYield feature to Method Prophecy
  • Loading branch information
ciaranmcnulty authored Oct 3, 2019
2 parents 9687f4e + ff58635 commit 33b058f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
38 changes: 37 additions & 1 deletion spec/Prophecy/Prophecy/MethodProphecySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,42 @@ function it_adds_ReturnPromise_during_willReturn_call(ObjectProphecy $objectProp
$this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ReturnPromise');
}

function it_adds_CallbackPromise_during_willYield_call(ObjectProphecy $objectProphecy)
{
if (PHP_VERSION_ID < 50500) {
throw new SkippingException('Yield language feature was introduced in >=5.5');
}

$objectProphecy->addMethodProphecy($this)->willReturn(null);

$this->willYield(array('foo', 'bar'));
$this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\CallbackPromise');
}

function it_yields_elements_configured_in_willYield(ObjectProphecy $objectProphecy)
{
if (PHP_VERSION_ID < 70000) {
throw new SkippingException('Yield language feature was introduced in >=5.5 but shouldYield matcher only available in >=7.0');
}

$objectProphecy->addMethodProphecy($this)->willReturn(null);

$this->willYield(array('foo', 'bar'));
$this->getPromise()->execute(array(), $objectProphecy, $this)->shouldYield(array('foo', 'bar'));
}

function it_yields_key_value_paris_configured_in_willYield(ObjectProphecy $objectProphecy)
{
if (PHP_VERSION_ID < 70000) {
throw new SkippingException('Yield language feature was introduced in >=5.5 but shouldYield matcher only available in >=7.0');
}

$objectProphecy->addMethodProphecy($this)->willReturn(null);

$this->willYield(array(10 => 'foo', 11 => 'bar'));
$this->getPromise()->execute(array(), $objectProphecy, $this)->shouldYield(array(10 => 'foo', 11 => 'bar'));
}

function it_adds_ThrowPromise_during_willThrow_call(ObjectProphecy $objectProphecy)
{
$objectProphecy->addMethodProphecy($this)->willReturn(null);
Expand Down Expand Up @@ -421,4 +457,4 @@ final public function finalMethod() {}
// Return void type hint language feature only introduced in >=7.1
if (PHP_VERSION_ID >= 70100) {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'ClassWithVoidTypeHintedMethods.php';
}
}
34 changes: 34 additions & 0 deletions src/Prophecy/Prophecy/MethodProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ public function willReturn()
return $this->will(new Promise\ReturnPromise(func_get_args()));
}

/**
* @param array $items
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function willYield($items)
{
if ($this->voidReturnType) {
throw new MethodProphecyException(
"The method \"$this->methodName\" has a void return type, and so cannot yield anything",
$this
);
}

if (!is_array($items)) {
throw new InvalidArgumentException(sprintf(
'Expected array, but got %s.',
gettype($items)
));
}

// Remove eval() when minimum version >=5.5
/** @var callable $generator */
$generator = eval('return function() use ($items) {
foreach ($items as $key => $value) {
yield $key => $value;
}
};');

return $this->will($generator);
}

/**
* Sets return argument promise to the prophecy.
*
Expand Down

0 comments on commit 33b058f

Please sign in to comment.