phpspec/prophecy
repository·master·Indexed 27 days ago
https://github.com/phpspec/prophecyA highly opinionated and flexible PHP object mocking framework used for creating dummies, stubs, mocks, and spies. While originally designed for phpspec, it can be integrated into any PHP testing framework, such as PHPUnit. It allows developers to define object behavior using Promises, verify expectations with Predictions, and use Argument tokens for flexible method matching. Requires PHP 7.2.0 or greater.
What's inside Prophecy
- Prophecy is a highly opinionated, powerful, and flexible PHP object mocking framework. While originally designed for phpspec, it can be integrated into any PHP testing framework with minimal effort.
Install Prophecy via Composer
masterProphecy requires PHP 7.2.0 or greater. To install it, add
phpspec/prophecyto yourrequire-devsection incomposer.jsonand run the composer install command.{ "require-dev": { "phpspec/prophecy": "~1.0" } }$> composer install --prefer-distInitialize Prophecy and create prophecies
masterTo use Prophecy, you must first create a
Prophetinstance. The Prophet is responsible for generating prophecies (instances ofObjectProphecy) which describe the future behavior of objects. You can also specify if the prophesied object should extend a specific class or implement an interface.$prophet = new Prophecy\Prophet; $prophecy = $prophet->prophesize(); // Specify class hierarchy $prophecy->willExtend('stdClass'); $prophecy->willImplement('SessionHandlerInterface');Create Dummy objects
masterA Dummy is a simple object used to satisfy typehints. It extends or implements the specified classes/interfaces but contains no logic. All public methods return
nulland no exceptions are thrown. Use$prophecy->reveal()to obtain the dummy object.$dummy = $prophecy->reveal();Use Spies to verify calls
masterProphecy supports spying, which allows you to verify calls after they have occurred without pre-defining predictions. Use the
shouldHaveBeenCalled()syntax on the revealed object or the prophecy.$em = $prophet->prophesize('Doctrine\ORM\EntityManager'); $controller->createUser($em->reveal()); // Verify the call happened after the fact $em->flush()->shouldHaveBeenCalled();Create Mock objects with Predictions
masterMocks are doubles used to verify expectations. Unlike stubs, mocks use Predictions to ensure specific methods were called. You must call$prophet->checkPredictions()(typically in a test'stearDown) to trigger the verification of these predictions.Create Stub objects with Promises
masterStubs are doubles that behave in specific ways when certain methods are called. You define behavior using Promises. A stub will throw anUnexpectedCallExceptionif a method is called that has not been described in the prophecy.Call original methods on a prophesized class
masterProphecy does not support calling the original methods on a prophesized class. If you need to mock some methods while calling the original implementation of others, it is recommended to refactor the class to adhere to the single-responsibility principle.Basic usage example with PHPUnit
masterTo use Prophecy within a PHPUnit test case, initialize a new
Prophecy\Prophetinstance in yoursetUp()method and call$this->prophet->checkPredictions()in yourtearDown()method to verify expectations. Use$this->prophet->prophesize($className)to create a prophecy, and$prophecy->reveal()to get the actual object to inject into your code.<?php class UserTest extends PHPUnit\Framework\TestCase { private $prophet; public function testPasswordHashing() { $hasher = $this->prophet->prophesize('App\Security\Hasher'); $user = new App\Entity\User($hasher->reveal()); $hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass'); $user->setPassword('qwerty'); $this->assertEquals('hashed_pass', $user->getPassword()); } protected function setUp() { $this->prophet = new \Prophecy\Prophet; } protected function tearDown() { $this->prophet->checkPredictions(); } }Configure Method Promises
masterPromises define how a method should behave. You can use shorthand methods or the explicitwill()method with aPromiseInterfaceimplementation.Use Predictions for Method Verification
masterPredictions allow you to assert how many times or in what way a method was called.Use Argument Wildcards (Tokens)
masterInstead of hardcoding exact values in method prophecies, useProphecy\Argumenttokens to match arguments based on type, identity, or custom logic. More precise tokens take precedence over less precise ones.