mnapoli/mockup

Concise mock library for PHP tests

0.1.0 2016-03-25 17:14 UTC

This package is auto-updated.

Last update: 2024-11-19 20:44:51 UTC


README

Concise mock library for PHP tests.

Build Status

Why?

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);
$mock->expect($this->any())
    ->method('foo')
    ->willReturn('Hello');
  • Prophecy
$prophet = new \Prophecy\Prophet();
$mock = $prophet->prophesize(Foo::class);
$mock->foo()->willReturn('Hello');
$mock = $mock->reveal();
  • Mockery
$mock = Mockery::mock(Foo::class);
$mock->shouldReceive('foo')
    ->andReturn('Hello');

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.

Installation

composer require --dev mnapoli/mockup

Usage

Mocks

You can mock a class or an interface:

use function Mockup\mock;

interface Foo
{
    public function foo($bar);
}

$mock = mock(Foo::class);
$mock->foo();

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

Spies

You can spy calls to an object:

use function Mockup\{spy, inspect};

$spy = spy($cache);
$foo->doSomething($spy);

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);
$foo->doSomething($mock);

inspect($spy)->set()->invokationCount();
inspect($spy)->set()->parameters(0);
inspect($spy)->set()->returnValue(0);