Comparison with PHPUnit mocks
Last updated on
6 November 2025
Now that we saw how you can use prophecy as a start, let's compare it with the other frequently used PHPUnit mocking framework.
PHPUnit expectations always add an assert while Prophecy does not. A real equivalent would be $prophecy->get('param1', 'param2')->willReturn('some return value')->shouldBeCalled(). If the return value is not asserted directly or indirectly (ie. if the tested code does not use it) a shouldBeCalled() or similar prediction is an absolute must. For example:
class ImageOperation {
public function __construct(Image $image) {
$this->image = $image;
}
public function shrinkAndDesaturate() {
$this->image->crop(50, 50);
// Oops! We accidently left this commented out.
//$this->image->desaturate();
}
}
class ImageTest extends UnitTestCase {
public function testShrinkAndDesaturate() {
$image = $this->prophesize(Image::class);
$image->crop(50, 50)->willReturn(TRUE);
$image->desaturate()->willReturn(FALSE);
$op = new ImageOperation($image->reveal());
// Since there is no shouldBeCalled() prediction for the each method call, this test
// will pass, even though it should fail since there was no desaturate() call.
$op->shrinkAndDesaturate();
}
}
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.