phpspec/prophecy-phpunit

PhpUnit test case integrating the Prophecy mocking library

Installs: 101 211

Dependents: 52

Stars: 42

Watchers: 8

Forks: 11

Open Issues: 1

Language: PHP

v1.1.0 2015-02-09 11:20 UTC

README

Build Status

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

Usage

<?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());
    }
}

Installation

Prerequisites

Prophecy PhpUnit requires PHP 5.3.3 or greater.

Setup through composer

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.

How to use it

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 tearDown method you are advised to call the parent method - doing so will ensure that the Prophect object has been unset before the next test case runs.