Prophecy PhpUnit integrates the Prophecy mocking library with PHPUnit to provide an easier mocking in your testsuite.
<?php
use Prophecy\PhpUnit\ProphecyTestCase;
class UserTest extends ProphecyTestCase
{
public function testPasswordHashing()
{
$hasher = $this->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());
}
}
Prophecy PhpUnit requires PHP 5.3.3 or greater.
First, add Prophecy to the list of dependencies inside your composer.json
:
{
"require-dev": {
"phpspec/prophecy-phpunit": "~1.0"
}
}
Then simply install it with composer:
$> composer install --prefer-dist
You can read more about Composer on its official webpage.
The special ProphecyTestCase
exposes a method prophesize($classOrInterface = null)
to use Prophecy.
For the usage of the Prophecy doubles, please refer to the Prophecy documentation.
If you want to add some logic in the setUp
and tearDown
methods of your testcase,
don't forget to call the parent methods to keep the initialization and prediction checking
of Prophecy.