Prophecy

A PHP Spec Framework

PHPSpec

sprechend & logisch

Bildquelle: Uribaani @ Deviantart

"Prophecy has been named that way because it concentrates on describing the future behavior of objects with very limited knowledge about them. But as with any other prophecy, those object prophecies can't create themselves – there should be a Prophet."


$ composer require "phpspec/prophecy" --dev

$ composer require "phpunit/phpunit:^4.5" --dev
$prophet = new Prophecy\Prophet;

/**
 * @var Prophecy\ObjectProphecy $prophecy
 */
$prophecy = $prophet->prophesize();

$prophecy->willExtend(Some\FancyClass::class);
$prophecy->willImplement(Some\FancyInterface::class);
<?php
// Der PHPUnit Weg...
class MyTest extends \PHPUnit_Framework_TestCase
{
    public function testSomething ()
    {
        $prophecy = $this->prophesize(Some\FancyClass::class);
        // oder
        $prophecy = $this->prophesize(Some\FancyInterface::class);

        $objectUnderTest = new FancyThing($prophecy->reveal());
    }
}
  • Dummies
  • Stubs
  • Mocks

Dummy


$dummy = $prophecy->reveal();

Stub


$dummy->doSomething(123)->willReturn('value');

Promise


$dummy->doSomething(123)->willReturn('value');

// ist equivalent zu

$dummy->doSomething(123)->will(new Prophecy\Promise\ReturnPromise(['value']));

Promises

/***** ReturnArgumentPromise *****/

$prophecy->doSomething(123)->willReturnArgument(1);

// ist equivalent zu

$prophecy->doSomething(123)->will(new Prophecy\Promise\ReturnArgumentPromise(1));

/***** ThrowPromise *****/

$prophecy->doSomething(123)->willThrow(new \Exception());

// ist equivalent zu

$prophecy->doSomething(123)->will(new Prophecy\Promise\ThrowPromise(new \Exception()));

/***** CallbackPromise *****/

$prophecy->doSomething(123)->will(function () {});

// ist equivalent zu

$prophecy->doSomething(123)->will(new Prophecy\Promise\CallbackPromise(function () {}));

Argumente


$userDummy->setName('John');

// ist equivalent zu

$userDummy->setName(new Prophecy\Argument\Token\ExactValueToken('John'));

// ist equivalent zu

$userDummy->setName(Prophecy\Argument::exact('John'));

Argumente


// Prophecy\Argument\Token\IdenticalValueToken
$userProphecy->setName(Prophecy\Argument::is('John'));

// Prophecy\Argument\Token\ExactValueToken
$userProphecy->setName(Prophecy\Argument::exact('John'));

// Prophecy\Argument\Token\TypeToken
$userProphecy->setName(Prophecy\Argument::type('string'));

// Prophecy\Argument\Token\ObjectStateToken
$userProphecy->setName(Prophecy\Argument::which('getName', 'John'));

// Prophecy\Argument\Token\CallbackToken
$userProphecy->setName(Prophecy\Argument::that(function ($arg) { return true; }));

Mehr Argumente


// Prophecy\Argument\Token\AnyValueToken
$userProphecy->setName(Prophecy\Argument::any());

// Prophecy\Argument\Token\AnyValuesToken
$userProphecy->setName(Prophecy\Argument::cetera());

// Prophecy\Argument\Token\StringContainsToken
$userProphecy->setName(Prophecy\Argument::containingString('oh'));

Mock


$entityManagerDummy->flush()->shouldBeCalled();

Predictions


$entityManager->flush()->shouldBeCalled();

// ist equivalent zu

$entityManager->flush()->should(new Prophecy\Prediction\CallPrediction());

Predictions


// Prophecy\Prediction\CallPrediction
$userProphecy->setName()->shouldBeCalled();

// Prophecy\Prediction\NoCallsPrediction
$userProphecy->setName()->shouldNotBeCalled();

// Prophecy\Prediction\CallTimesPrediction
$userProphecy->setName()->shouldBeCalledTimes(2);

// Prophecy\Prediction\CallbackPrediction
$userProphecy->setName()->should(function ($obj, $method) {});

Kombinationen


$userProphecy->getName()->shouldBeCalled()->willReturn('John');

Spies


$em = $this->prophesize('Doctrine\ORM\EntityManager');

$controller->createUser($em->reveal());

$em->flush()->shouldHaveBeenCalled();

PHPUnit

vs.

Prophecy

...some code...

Noch Fragen?

Danke euch!

Prophecy - A PHP Spec Framework

By Ole Rößner

Prophecy - A PHP Spec Framework

  • 1,194