phpspec/prophecy
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
doctrine/instantiator | 2.0.0 | 10.75 kB | MIT | prod | |
phpdocumentor/reflection-docblock | 5.4.0 | - | MIT | prod | |
sebastian/comparator | 5.0.1 | 19.55 kB | BSD-3-Clause | prod | |
sebastian/recursion-context | 5.0.0 | 5.15 kB | BSD-3-Clause | prod |
As a highly opinionated PHP object mocking framework, phpspec/prophecy is incredibly flexible and can be used in virtually any testing framework with only a minimal amount of required effort. It facilitates the creation of object prophecies and "dummies", which are basic PHP objects that serve only to fulfil prophecies. It even supports the creation of "stubs" or "mocks", which are object doubles with specific environments and behaviors, and allows for the customization of method prophecies, promises, and predictions. Furthermore, phpspec/prophecy offers functionalities for defining and inspecting argument tokens and for creating and working with spies.
Use of phpspec/prophecy involves the instantiation of a Prophet
object, which can create object prophecies. A simple example of this would be:
$prophet = new Prophecy\Prophet;
$prophecy = $prophet->prophesize();
$prophecy->willExtend('stdClass');
You can create "dummies" and "stubs" with reveal()
. For instance:
$dummy = $prophecy->reveal();
Or a stub with behavior like:
$prophecy->read('123')->willReturn('value');
$stub = $prophecy->reveal();
For more complex behaviors, you can use argument tokens and callbacks. Here's an example:
use Prophecy\Argument;
$user->setName(Argument::type('string'))->will(function ($args) {
$this->getName()->willReturn($args[0]);
});
You can also create mocks with 'predictions':
$entityManager->flush()->shouldBeCalled();
And spies with shouldHave
:
$em = $prophet->prophesize('Doctrine\ORM\EntityManager');
$controller->createUser($em->reveal());
$em->flush()->shouldHaveBeenCalled();
Check how all your predictions played out with checkPredictions()
as in:
$prophet->checkPredictions();
The documentation for phpspec/prophecy is contained within the package's README on its GitHub page, which you can access at https://github.com/phpspec/prophecy. Here, you will find thorough explanations for how to use the package, as well as sample code to illustrate major concepts and mechanics.