Integrating the Prophecy mocking library in PHPUnit test cases

Installs: 28 028 199

Dependents: 1 320

Suggesters: 1

Security: 0

Stars: 158

Watchers: 18

Forks: 38

Open Issues: 3

v2.2.0 2024-03-01 08:33 UTC

This package is auto-updated.

Last update: 2024-05-27 12:07:34 UTC


Build Status

Prophecy PhpUnit integrates the Prophecy mocking library with PHPUnit to provide an easier mocking in your testsuite.



Prophecy PhpUnit requires PHP 7.3 or greater. Prophecy PhpUnit requires PHPUnit 9.1 or greater. Older versions of PHPUnit are providing the Prophecy integration themselves.

Setup through composer

composer require --dev phpspec/prophecy-phpunit

You can read more about Composer on its official webpage.

How to use it

The trait ProphecyTrait provides a method prophesize($classOrInterface = null) to use Prophecy. For the usage of the Prophecy doubles, please refer to the Prophecy documentation.

Below is a usage example:


namespace App;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use App\Security\Hasher;
use App\Entity\User;

class UserTest extends TestCase
    use ProphecyTrait;

    public function testPasswordHashing()
        $hasher = $this->prophesize(Hasher::class);
        $user   = new User($hasher->reveal());

        $hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass');


        $this->assertEquals('hashed_pass', $user->getPassword());