romand
romand

Reputation: 29

PHP Prophecy ->reveal() method - should I put the expectations (like "willReturn", "ShouldBeCalled") before or after it?

Working with PHP Prophecy now. I have two codes samples: One:

$aProphecy = $this->prophesize(A::class);
$aProphecy->someMethod()->willReturn([]);
//now can be used:
$aProphecy->reveal();

Two

$aProphecy = $this->prophesize(A::class);
$aProphecy->reveal();
$aProphecy->someMethod()->willReturn([]);

I can't understand which is the correct way, and why?

Upvotes: 0

Views: 581

Answers (1)

hiroorih
hiroorih

Reputation: 63

The order of your examples is not relevant at this point because both cases will be evaluated. However, I prefer the first variant, because it is clearer what is expected from $aProphecy.

The important thing to notice is that your code isn't checking anything as you need to inject $aProphecy to another object and reveal it there:

$aProphecy = $this->prophesize(A::class);
$aProphecy->someMethod()->willReturn([]);
$bObject = new B($aProphecy->reveal());
$bObject->methodWhichCallSomeMethodInside();
// check if someMethod was called exactly once
$aProchecy->someMethod()->shouldBeCalled(); 

Upvotes: 0

Related Questions