2017 © Pedro Peláez
 

library mockery

Mockery is a simple yet flexible PHP mock object framework

image

mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

  • Monday, July 30, 2018
  • by davedevelopment
  • Repository
  • 79 Watchers
  • 5558 Stars
  • 37,308,180 Installations
  • PHP
  • 10369 Dependents
  • 18 Suggesters
  • 318 Forks
  • 48 Open issues
  • 18 Versions
  • 9 % Grown

The README.md

Mockery

Build Status Latest Stable Version Coverage Status Total Downloads, (*1)

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., (*2)

Mockery is released under a New BSD License., (*3)

Installation

To install Mockery, run the command below and you will get the latest version, (*4)

composer require --dev mockery/mockery

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., (*5)

The current version can be seen at docs.mockery.io., (*6)

PHPUnit Integration

Mockery ships with some helpers if you are using PHPUnit. You can extend the Mockery\Adapter\Phpunit\MockeryTestCase class instead of PHPUnit\Framework\TestCase, or if you are already using a custom base class for your tests, take a look at the traits available in the Mockery\Adapter\Phpunit namespace., (*7)

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., (*8)

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., (*9)

``` php $double = Mockery::mock();, (*10)


If you need Mockery to create a test double to satisfy a particular type hint, you can pass the type to the `mock` method. ``` php 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 section., (*11)

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., (*12)

``` php $double->allows()->find(123)->andReturns(new Book());, (*13)

$book = $double->find(123);, (*14)


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: ``` php $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:, (*15)

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


or ``` php $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:, (*17)

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


## 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. ``` php $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., (*19)

``` php, (*20)

public function tearDown() { Mockery::close(); }, (*21)


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. ``` php $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., (*22)

For new users of Mockery, the above example can also be written as:, (*23)

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


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 🕵️ 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. ``` php // $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:, (*25)

``` php $double->baz(123);, (*26)

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


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 🔌 ### Global Helpers Mockery ships with a handful of global helper methods, you just need to ask Mockery to declare them. ``` php 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 its own spy function., (*28)

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., (*29)

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

abstract function doFoo();

}, (*31)

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


## 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 Mockery will attempt to continue support HHVM, but will not make any guarantees. ## 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. ```sh $ 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., (*33)

The Versions

30/07 2018

dev-master

9999999-dev https://github.com/mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

08/05 2018

1.1.0

1.1.0.0 https://github.com/mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

02/02 2018

0.9.x-dev

0.9.9999999.9999999-dev http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

06/10 2017

1.0

1.0.0.0 http://github.com/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

12/05 2017

dev-documentation/issue442

dev-documentation/issue442 http://github.com/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

28/02 2017

0.9.9

0.9.9.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

09/02 2017

0.9.8

0.9.8.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

02/02 2017

1.0.0-alpha1

1.0.0.0-alpha1 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

19/12 2016

0.9.7

0.9.7.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

30/09 2016

0.9.6

0.9.6.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

22/05 2016

0.9.5

0.9.5.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

02/04 2015

0.9.4

0.9.4.0 http://github.com/padraic/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.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

22/12 2014

0.9.3

0.9.3.0 http://github.com/padraic/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 succint 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.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2
  • lib-pcre >=7.0

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

03/09 2014

0.9.2

0.9.2.0 http://github.com/padraic/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 succint 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.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2
  • lib-pcre >=7.0

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

02/05 2014

0.9.1

0.9.1.0 http://github.com/padraic/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 succint 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.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2
  • lib-pcre >=7.0

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

05/02 2014

0.9.0

0.9.0.0 http://github.com/padraic/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 succint 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.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2
  • lib-pcre >=7.0

 

The Development Requires

bdd tdd test mock stub test double mockery testing library mock objects

01/04 2013

0.8.0

0.8.0.0 http://github.com/padraic/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 succint 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.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

  • hamcrest/hamcrest 1.1.0

bdd tdd test mock stub test double mockery testing library mock objects

24/01 2012

0.7.2

0.7.2.0 http://github.com/padraic/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 succint 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.

  Sources   Download

New BSD

The Requires

  • php >=5.3.2

 

bdd tdd test mock stub test double mockery testing library mock objects