Skip to content
Mockery is a simple but flexible mock object framework for use in unit testing.
Find file
Pull request Compare This branch is 1039 commits behind padraic:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
build
examples/temp
library
tests
README

README

Mockery is a Cross Framework Mock Object and Stub Framework for PHP5, currently
released as an alpha version for interested developers to assist in offering
feedback. It is not yet fully functional, and stability is a relative term :).

Requirements:

PHP: 5.2.4, probably less but I haven't checked

Installation (Manual Installation without PEAR):

1. Copy the contents of /library to a location on the PHP include_path, e.g.
   /usr/share/php.

Yeah, that's pretty much it :).

Usage:

Mockery defines two functions called mockery() and mockery_verify() which are a shorter
means to call Mockery::mock() and Mockery::verify() which are both static methods. A simple
use case is as follows:

require_once 'Mockery/Framework.php';

class Addition {

    protected $math = null;

    public function __construct(Math $math) {
        $this->math = $math;
    }

    public function add($op1, $op2) {
        return $this->math->add($op1, $op2);
    }

}

class Math {

    public function add($a, $b) {
        return $a + $b;
    }

}

class AdditionTest extends PHPUnit_Framework_TestCase {

    public function testAdd() {
        $math = mockery('Math');
        $addition = new Addition($math);

        // set Mock expectations!
        $math->shouldReceive('add')->once()->with(1, 1)->andReturn(2);
        // perform the action!
        $result = $addition->add(1, 1);

        // Verify?
        mockery_verify();

        // Proceed to assertions (if any) as normal
        $this->assertEqual(2, $result);
    }

    public function testAddWithFailingMock() {
        $math = mockery('Math');
        $addition = new Addition($math);

        // set Mock expectations!
        // twice() is obviously wrong
        $math->shouldReceive('add')->twice()->with(1, 1)->andReturn(2);
        // perform the action!
        $result = $addition->add(1, 1);

        // Verify?
        mockery_verify();

        // Proceed to assertions (if any) as normal
        $this->assertEqual(2, $result);
    }

}

PHPUnit reports:

PHPUnit 3.3.14 by Sebastian Bergmann.

.E

Time: 0 seconds

There was 1 error:

1) testAddWithFailingMock(AdditionTest)
Mockery_ExpectationException: method add called incorrect number of times;
expected call 2 times but received 1

A vastly more detailed introduction including brief API documentation is available on my blog:
http://blog.astrumfutura.com/archives/392-The-Mockery-An-Independent-Mock-Object-and-Stub-Framework-for-PHP5.html
Something went wrong with that request. Please try again.