Prophecy

Updated: 17 September 2022

Dummy

$dummy = $this->prophesize(SomeClass::class);
$sut->action($dummy->reveal());

// with phpspec
class MarkdownSpec extends ObjectBehavior
{
    function it_converts_text_from_an_external_source(Reader $reader)
    {
        // $reader is a Dummy at this point because we've added
        // no behaviour nor made any assertions on its methods.
    }
}

Stub

$stub = $this->prophesize(SomeClass::class);
$stub->getSomething()->willReturn('foo');
$sut->action($stub->reveal());

// with phpspec
class MarkdownSpec extends ObjectBehavior
{
    function it_converts_text_from_an_external_source(Reader $reader)
    {
        $reader->getMarkdown()->willReturn("Hi, there");

        // $reader is now a Stub because we are controlling its behaviour.

        $this->toHtmlFromReader($reader)->shouldReturn("Hi, there");
    }
}

Mock

$mock = $this->prophesize(SomeClass::class);
$mock->doSomething('bar')->shouldBeCalled();
$sut->action($mock->reveal());

// with phpspec
class MarkdownSpec extends ObjectBehavior
{
    function it_outputs_converted_text(Writer $writer)
    {
        $writer->writeText("Hi, there")->shouldBeCalled();

        // Adding an assertion before the double is used makes it a Mock.

        $this->outputHtml("Hi, there", $writer);
    }
}

Spy

$spy = $this->prophesize(SomeClass::class);
$sut->action($spy->reveal());
$spy->doSomething('bar')->shouldHaveBeenCalled();

// with phpspec
class MarkdownSpec extends ObjectBehavior
{
    function it_outputs_converted_text(Writer $writer)
    {
        $this->outputHtml("Hi, there", $writer);

        // Adding an expectation after the double is used makes it a Spy.
        $writer->writeText("Hi, there")->shouldHaveBeenCalled();
    }
}

Leave a comment