willReturn('string') returns Prophecy object and can't be used in if statement

205 views
Skip to first unread message

e.vanwi...@waarneembemiddeling.nl

unread,
Jun 26, 2014, 10:00:02 AM6/26/14
to phpsp...@googlegroups.com
I'm just starting with PHPspec and experiencing the following use case:

I have a class with a property getType that returns a string. 
class MySpec extends ObjectBehavior
{
function it_should_return_a_true_dossier_validation_result_if_bigstatus_is_true(DossierItemInterface $dossierItem)
{
$dossierItem->getType()->willReturn('id');
$dossier->getDossierItems()->willReturn(new ArrayCollection(array($dossierItem)));
 
$this->validate($dossier)->isValid()->shouldReturn(true); 
}

In my implementation I want to check or the $dossierItem->getType() === 'id' but this check always fails since $dossierItem->getType() is an object of type Prophecy\Prophecy\MethodProphecy. 

Implementation class:
class Validator 
{
foreach($dossier->getDossierItems() as $dossierItem) {
if ($dossierItem->getType() === 'big') {
          $validation = $dossierItem->getData()->isValid();
}
}
}

Can someone point me in the right direction? I simplified the code examples in the application they are made more specific. 

e.vanwi...@waarneembemiddeling.nl

unread,
Jun 27, 2014, 7:45:19 AM6/27/14
to phpsp...@googlegroups.com
I found the solution for our problem. I found the exact same problem here: https://gist.github.com/shanethehat/8951398

The solution can be found here: https://github.com/phpspec/phpspec/pull/286
$stagingCollection->getIterator()
    ->willReturn(
        new \ArrayIterator(array(
            $staging1->getWrappedObject(),
            $staging2->getWrappedObject(),
        ))
    );

So using ->getWrappedObject() in the Spec the check $dossierItem->getType() === 'id' returns now the object instead of type Prophecy\Prophecy\MethodProphecy. 



Op donderdag 26 juni 2014 16:00:02 UTC+2 schreef e.vanwi...@waarneembemiddeling.nl:
Reply all
Reply to author
Forward
0 new messages