Concise mock library for PHP tests

0.1.0 2016-03-25 17:14 UTC

This package is auto-updated.

Last update: 2024-04-19 19:33:47 UTC


Concise mock library for PHP tests.

Build Status


This mock library is meant to be a simple yet powerful alternative to existing solutions.

  • Mockup
$mock = mock(Foo::class, [
    'foo' => 'Hello',
  • PHPUnit
$mock = $this->getMock(Foo::class, [], [], '', false);
  • Prophecy
$prophet = new \Prophecy\Prophet();
$mock = $prophet->prophesize(Foo::class);
$mock = $mock->reveal();
  • Mockery
$mock = Mockery::mock(Foo::class);

Additionally, Mockup doesn't include assertions. Instead of forcing you to learn a specific assertion syntax, complex enough to cover all cases, it lets you use the assertions you already know (PHPUnit, phpspec, …). Here is an example in a PHPUnit test:

// The mock method was called once
$this->assertEquals(1, inspect($mock)->foo()->invokationCount());

Read more about spying method calls below.


composer require --dev mnapoli/mockup



You can mock a class or an interface:

use function Mockup\mock;

interface Foo
    public function foo($bar);

$mock = mock(Foo::class);

All its methods will do nothing and return null (null object pattern). The mock will implement the interface or extend the class given, as such it will work fine with any type-hint.

You can make some methods return values other than null:

$mock = mock(Foo::class, [
    'foo' => 'hello',

$mock->foo('john'); // hello

You can also use a closure to define the new method's body:

$mock = mock(Foo::class, [
    'foo' => function ($bar) {
        return strtoupper('hello ' . $bar);

$mock->foo('john'); // HELLO JOHN


You can spy calls to an object:

use function Mockup\{spy, inspect};

$spy = spy($cache);

inspect($spy)->set()->invokationCount(); // number of calls to $spy->set()
inspect($spy)->set()->parameters(0); // parameters provided to the first call to $spy->set()
inspect($spy)->set()->returnValue(0); // value returned by the first call to $spy->set()

The difference with a mock is that you are spying real calls to a real object. A mock is a null object.

Mockup does not provide assertions or expectations so that you can use the assertion library you prefer.

Every mock object is also a spy, so you can create a mock and spy its method calls:

use function Mockup\{mock, inspect};

$mock = mock(CacheInterface::class);