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
orshouldBeCalled()
- checks that method was called 1 or more timesNoCallsPrediction
orshouldNotBeCalled()
- checks that method was not calledCallTimesPrediction
orshouldBeCalledTimes($count)
- checks that method was called a specified number of timesCallbackPrediction
orshould($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: