Mock Test Double Using Prophecy

Mock is a test double which simulates behavior and we have expectations for it. You define predictions for it and later check if any of them failed.

<?php

namespace tests;

use App\Output;
use App\Questioner;
use App\Scorer;
use Prophecy\Argument;

class QuestionerTest extends \PHPUnit_Framework_TestCase
{
    // ...

    /**
     * @test
     */
    function questioner_is_saved()
    {
        $outputMock = $this->prophesize(Output::CLASS);
        $outputMock->save([], Argument::type('string'))->shouldBeCalled();
        $questioner = new Questioner($outputMock->reveal());

        $questioner->saveAs('any name');
    }
}

Some of the predictions you can make:

  • CallPrediction or shouldBeCalled() - checks that method was called 1 or more times
  • NoCallsPrediction or shouldNotBeCalled() - checks that method was not called
  • CallTimesPrediction or shouldBeCalledTimes($count) - checks that method was called a specified number of times
  • CallbackPrediction or should($callback) - checks method against custom callback

Custom predictions can be created by implementing PredictionInterface.

This article is from the Test doubles using Prophecy series which is made from following articles:

  • Dummy Test Double Using Prophecy
  • Stub Test Double Using Prophecy
  • Spy Test Double Using Prophecy
  • Mock Test Double Using Prophecy
  • Fake Test Double Using Prophecy
  • Posted in: PHP, Prophecy, TDD, Technical

    Comments