PHPackages                             bilge/mockery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. bilge/mockery

ActiveLibrary[Testing &amp; Quality](/categories/testing)

bilge/mockery
=============

Mockery is a simple yet flexible PHP mock object framework

0.9.3(11y ago)025BSD-3-ClausePHPPHP &gt;=5.3.2

Since Jan 24Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Bilge/mockery)[ Packagist](https://packagist.org/packages/bilge/mockery)[ Docs](http://github.com/padraic/mockery)[ RSS](/packages/bilge-mockery/feed)WikiDiscussions issue-871 Synced 2d ago

READMEChangelogDependencies (4)Versions (26)Used By (0)

Mockery
=======

[](#mockery)

[![Build Status](https://camo.githubusercontent.com/682d160b74b8ca0d1e57ad1c71639c292d002529ed0eb8bc305502ee81b39a4b/68747470733a2f2f7472617669732d63692e6f72672f6d6f636b6572792f6d6f636b6572792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mockery/mockery)[![Latest Stable Version](https://camo.githubusercontent.com/4c6374f4897520c57925618cf97bca77bae8f01a5494d57ed0a4fadba09d4506/68747470733a2f2f706f7365722e707567782e6f72672f6d6f636b6572792f6d6f636b6572792f762f737461626c652e737667)](https://packagist.org/packages/mockery/mockery)[![Coverage Status](https://camo.githubusercontent.com/eb1dcc5e3dd4738ad69a82e680ddfbc096135e6b9fba9fe3b6a17eb71d1618e1/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6f636b6572792f6d6f636b6572792f62616467652e737667)](https://coveralls.io/github/mockery/mockery)[![Total Downloads](https://camo.githubusercontent.com/09f97f2cf48900dc75fec65253bcace93bd077197529be72430eac91ccc70bdf/68747470733a2f2f706f7365722e707567782e6f72672f6d6f636b6572792f6d6f636b6572792f646f776e6c6f6164732e737667)](https://packagist.org/packages/mockery/mockery)

Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.

Mockery is released under a New BSD License.

Installation
------------

[](#installation)

To install Mockery, run the command below and you will get the latest version

```
composer require --dev mockery/mockery
```

⚠️️ The remainder of this README refers specifically to the master branch (1.0-dev).

Documentation
-------------

[](#documentation)

In older versions, this README file was the documentation for Mockery. Over time we have improved this, and have created an extensive documentation for you. Please use this README file as a starting point for Mockery, but do read the documentation to learn how to use Mockery.

The current version can be seen at [docs.mockery.io](http://docs.mockery.io).

Test Doubles
------------

[](#test-doubles)

Test doubles (often called mocks) simulate the behaviour of real objects. They are commonly utilised to offer test isolation, to stand in for objects which do not yet exist, or to allow for the exploratory design of class APIs without requiring actual implementation up front.

The benefits of a test double framework are to allow for the flexible generation and configuration of test doubles. They allow the setting of expected method calls and/or return values using a flexible API which is capable of capturing every possible real object behaviour in way that is stated as close as possible to a natural language description. Use the `Mockery::mock` method to create a test double.

```
$double = Mockery::mock();
```

If you need Mockery to create a test double to satisfy a particular type hint, you can pass the type to the `mock` method.

```
class Book {}

interface BookRepository {
    function find($id): Book;
    function findAll(): array;
    function add(Book $book): void;
}

$double = Mockery::mock(BookRepository::class);
```

A detailed explanation of creating and working with test doubles is given in the documentation, [Creating test doubles](http://docs.mockery.io/en/latest/reference/creating_test_doubles.html)section.

Method Stubs 🎫
--------------

[](#method-stubs-)

A method stub is a mechanism for having your test double return canned responses to certain method calls. With stubs, you don't care how many times, if at all, the method is called. Stubs are used to provide indirect input to the system under test.

```
$double->allows()->find(123)->andReturns(new Book());

$book = $double->find(123);
```

If you have used Mockery before, you might see something new in the example above — we created a method stub using `allows`, instead of the "old" `shouldReceive` syntax. This is a new feature of Mockery v1, but fear not, the trusty ol' `shouldReceive` is still here.

For new users of Mockery, the above example can also be written as:

```
$double->shouldReceive('find')->with(123)->andReturn(new Book());
$book = $double->find(123);
```

If your stub doesn't require specific arguments, you can also use this shortcut for setting up multiple calls at once:

```
$double->allows([
    "findAll" => [new Book(), new Book()],
]);
```

or

```
$double->shouldReceive('findAll')
    ->andReturn([new Book(), new Book()]);
```

You can also use this shortcut, which creates a double and sets up some stubs in one call:

```
$double = Mockery::mock(BookRepository::class, [
    "findAll" => [new Book(), new Book()],
]);
```

Method Call Expectations 📲
--------------------------

[](#method-call-expectations-)

A Method call expectation is a mechanism to allow you to verify that a particular method has been called. You can specify the parameters and you can also specify how many times you expect it to be called. Method call expectations are used to verify indirect output of the system under test.

```
$book = new Book();

$double = Mockery::mock(BookRepository::class);
$double->expects()->add($book);
```

During the test, Mockery accept calls to the `add` method as prescribed. After you have finished exercising the system under test, you need to tell Mockery to check that the method was called as expected, using the `Mockery::close` method. One way to do that is to add it to your `tearDown`method in PHPUnit.

```
public function tearDown()
{
    Mockery::close();
}
```

The `expects()` method automatically sets up an expectation that the method call (and matching parameters) is called **once and once only**. You can choose to change this if you are expecting more calls.

```
$double->expects()->add($book)->twice();
```

If you have used Mockery before, you might see something new in the example above — we created a method expectation using `expects`, instead of the "old" `shouldReceive` syntax. This is a new feature of Mockery v1, but same as with `accepts` in the previous section, it can be written in the "old" style.

For new users of Mockery, the above example can also be written as:

```
$double->shouldReceive('find')
    ->with(123)
    ->once()
    ->andReturn(new Book());
$book = $double->find(123);
```

A detailed explanation of declaring expectations on method calls, please read the documentation, the [Expectation declarations](http://docs.mockery.io/en/latest/reference/expectations.html)section. After that, you can also learn about the new `allows` and `expects` methods in the [Alternative shouldReceive syntax](http://docs.mockery.io/en/latest/reference/alternative_should_receive_syntax.html)section.

It is worth mentioning that one way of setting up expectations is no better or worse than the other. Under the hood, `allows` and `expects` are doing the same thing as `shouldReceive`, at times in "less words", and as such it comes to a personal preference of the programmer which way to use.

Test Spies 🕵️
-------------

[](#test-spies-️)

By default, all test doubles created with the `Mockery::mock` method will only accept calls that they have been configured to `allow` or `expect` (or in other words, calls that they `shouldReceive`). Sometimes we don't necessarily care about all of the calls that are going to be made to an object. To facilitate this, we can tell Mockery to ignore any calls it has not been told to expect or allow. To do so, we can tell a test double `shouldIgnoreMissing`, or we can create the double using the `Mocker::spy`shortcut.

```
// $double = Mockery::mock()->shouldIgnoreMissing();
$double = Mockery::spy();

$double->foo(); // null
$double->bar(); // null
```

Further to this, sometimes we want to have the object accept any call during the test execution and then verify the calls afterwards. For these purposes, we need our test double to act as a Spy. All mockery test doubles record the calls that are made to them for verification afterwards by default:

```
$double->baz(123);

$double->shouldHaveReceived()->baz(123); // null
$double->shouldHaveReceived()->baz(12345); // Uncaught Exception Mockery\Exception\InvalidCountException...
```

Please refer to the [Spies](http://docs.mockery.io/en/latest/reference/spies.html) section of the documentation to learn more about the spies.

Utilities 🔌
-----------

[](#utilities-)

### Global Helpers

[](#global-helpers)

Mockery ships with a handful of global helper methods, you just need to ask Mockery to declare them.

```
Mockery::globalHelpers();

$mock = mock(Some::class);
$spy = spy(Some::class);

$spy->shouldHaveReceived()
    ->foo(anyArgs());
```

All of the global helpers are wrapped in a `!function_exists` call to avoid conflicts. So if you already have a global function called `spy`, Mockery will silently skip the declaring it's own `spy` function.

### Testing Traits

[](#testing-traits)

As Mockery ships with code generation capabilities, it was trivial to add functionality allowing users to create objects on the fly that use particular traits. Any abstract methods defined by the trait will be created and can have expectations or stubs configured like normal Test Doubles.

```
trait Foo {
    function foo() {
        return $this->doFoo();
    }

    abstract function doFoo();
}

$double = Mockery::mock(Foo::class);
$double->allows()->doFoo()->andReturns(123);
$double->foo(); // int(123)
```

Versioning
----------

[](#versioning)

The Mockery team attempts to adhere to [Semantic Versioning](http://semver.org), however, some of Mockery's internals are considered private and will be open to change at any time. Just because a class isn't final, or a method isn't marked private, does not mean it constitutes part of the API we guarantee under the versioning scheme.

### Alternative Runtimes

[](#alternative-runtimes)

Mockery will attempt to continue support HHVM, but will not make any guarantees.

A new home for Mockery
----------------------

[](#a-new-home-for-mockery)

⚠️️ Update your remotes! Mockery has transferred to a new location. While it was once at `padraic/mockery`, it is now at `mockery/mockery`. While your existing repositories will redirect transparently for any operations, take some time to transition to the new URL.

```
$ git remote set-url upstream https://github.com/mockery/mockery.git
```

Replace `upstream` with the name of the remote you use locally; `upstream` is commonly used but you may be using something else. Run `git remote -v` to see what you're actually using.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 58.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~301 days

Recently: every ~266 days

Total

7

Last Release

3415d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/470626?v=4)[Bilge](/maintainers/Bilge)[@Bilge](https://github.com/Bilge)

---

Top Contributors

[![davedevelopment](https://avatars.githubusercontent.com/u/61351?v=4)](https://github.com/davedevelopment "davedevelopment (669 commits)")[![padraic](https://avatars.githubusercontent.com/u/19780?v=4)](https://github.com/padraic "padraic (143 commits)")[![robertbasic](https://avatars.githubusercontent.com/u/166625?v=4)](https://github.com/robertbasic "robertbasic (135 commits)")[![fhinkel](https://avatars.githubusercontent.com/u/101553?v=4)](https://github.com/fhinkel "fhinkel (27 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (26 commits)")[![Bilge](https://avatars.githubusercontent.com/u/470626?v=4)](https://github.com/Bilge "Bilge (12 commits)")[![villfa](https://avatars.githubusercontent.com/u/2891564?v=4)](https://github.com/villfa "villfa (11 commits)")[![beni0888](https://avatars.githubusercontent.com/u/2619784?v=4)](https://github.com/beni0888 "beni0888 (11 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (10 commits)")[![wouterj](https://avatars.githubusercontent.com/u/749025?v=4)](https://github.com/wouterj "wouterj (8 commits)")[![asmblah](https://avatars.githubusercontent.com/u/1714005?v=4)](https://github.com/asmblah "asmblah (8 commits)")[![fredemmott](https://avatars.githubusercontent.com/u/360927?v=4)](https://github.com/fredemmott "fredemmott (8 commits)")[![duncan3dc](https://avatars.githubusercontent.com/u/546811?v=4)](https://github.com/duncan3dc "duncan3dc (6 commits)")[![yugeon](https://avatars.githubusercontent.com/u/350226?v=4)](https://github.com/yugeon "yugeon (5 commits)")[![igorw](https://avatars.githubusercontent.com/u/88061?v=4)](https://github.com/igorw "igorw (5 commits)")[![pschultz](https://avatars.githubusercontent.com/u/607733?v=4)](https://github.com/pschultz "pschultz (5 commits)")[![demsey2](https://avatars.githubusercontent.com/u/3159367?v=4)](https://github.com/demsey2 "demsey2 (5 commits)")[![robertmain](https://avatars.githubusercontent.com/u/537684?v=4)](https://github.com/robertmain "robertmain (5 commits)")[![agiledivider](https://avatars.githubusercontent.com/u/1173523?v=4)](https://github.com/agiledivider "agiledivider (5 commits)")[![adambrett](https://avatars.githubusercontent.com/u/145340?v=4)](https://github.com/adambrett "adambrett (4 commits)")

---

Tags

testingtestlibraryBDDTDDmockerymockstubtest doublemock objects

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bilge-mockery/health.svg)

```
[![Health](https://phpackages.com/badges/bilge-mockery/health.svg)](https://phpackages.com/packages/bilge-mockery)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

36918.1M98](/packages/php-mock-php-mock)[php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

1718.2M399](/packages/php-mock-php-mock-phpunit)[php-mock/php-mock-mockery

Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.

392.1M96](/packages/php-mock-php-mock-mockery)[kahlan/kahlan

The PHP Test Framework for Freedom, Truth and Justice.

1.2k1.2M247](/packages/kahlan-kahlan)[php-mock/php-mock-prophecy

Mock built-in PHP functions (e.g. time()) with Prophecy. This package relies on PHP's namespace fallback policy. No further extension is needed.

16496.6k15](/packages/php-mock-php-mock-prophecy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
