PHPackages                             mvkasatkin/mocker - 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. mvkasatkin/mocker

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

mvkasatkin/mocker
=================

Mocking helper for PHPUnit

1.0.9(8y ago)19515MITPHPPHP &gt;=7.0.0

Since Sep 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mvkasatkin/mocker)[ Packagist](https://packagist.org/packages/mvkasatkin/mocker)[ RSS](/packages/mvkasatkin-mocker/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (2)Versions (10)Used By (5)

Mocker
======

[](#mocker)

[![Build Status](https://camo.githubusercontent.com/63847544e21d22908f468b8edc4885d8e113ce746c83ea6f7aefab75c666bffd/68747470733a2f2f7472617669732d63692e6f72672f6d766b617361746b696e2f6d6f636b65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mvkasatkin/mocker)[![Coverage Status](https://camo.githubusercontent.com/fe4d9554e10c032dd88e9deec2736328c6e2f9cf18e6ac93ad2634009d087baa/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d766b617361746b696e2f6d6f636b65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mvkasatkin/mocker?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/eb5789a795b8e4c4ad79d643520193ab2a679df7f444ef7fbd5480e9d856a379/68747470733a2f2f706f7365722e707567782e6f72672f6d766b617361746b696e2f6d6f636b65722f76657273696f6e)](https://packagist.org/packages/mvkasatkin/mocker)[![License](https://camo.githubusercontent.com/fbf6d6a5204a96027c371dbbac36a773c80f0a1da02e90188fae161f4a53bd9e/68747470733a2f2f706f7365722e707567782e6f72672f6d766b617361746b696e2f6d6f636b65722f6c6963656e7365)](https://packagist.org/packages/mvkasatkin/mocker)

Helper for convenient work with PHPUnit mocks.
It uses the library **phpunit/phpunit-mock-objects** supplied with PHPUnit.

Install
-------

[](#install)

```
composer require mvkasatkin/mocker

```

Initialize
----------

[](#initialize)

```
    public function setUp()
    {
        parent::setUp();
        Mocker::init($this);
    }
```

Simple test-double
------------------

[](#simple-test-double)

Works with regular classes, abstract classes and interfaces.

```
        $mock = Mocker::create(SomeClass::class);
        $this->assertInstanceOf(SomeClass::class, $mock);
```

Test-double with methods
------------------------

[](#test-double-with-methods)

```
        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('firstMethod')->returns(true),
            Mocker::method('secondMethod', 1)->returns(true),
            Mocker::method('thirdMethod', 1, [$param1, $param2])->returns($value),
            Mocker::method('fourthMethod', 1)->with([$param1, $param2])->returns($value),
        ]);
```

In this example:

**$mock-&gt;firstMethod** - can be called any number of times with any parameters and returns true
**$mock-&gt;secondMethod** - must be called once and returns true
**$mock-&gt;thirdMethod** - must be called once with the parameters $param1, $param2 and returns $value
**$mock-&gt;fourthMethod** - must be called once with the parameters $param1, $param2 and returns $value

Test-double with/return map:
----------------------------

[](#test-double-withreturn-map)

```
        /** @var SomeClass $mock */
        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('checkMap', 5)->returnsMap([
                ['arg1', 'arg2', 'arg3', 'A'],
                ['arg1', 'arg2', null, 'B'],
                ['arg1', null, null, 'C'],
                [null, null, null, 'D'],
            ]),
        ]);
        $this->assertEquals('A', $mock->checkMap('arg1', 'arg2', 'arg3'));
        $this->assertEquals('B', $mock->checkMap('arg1', 'arg2'));
        $this->assertEquals('C', $mock->checkMap('arg1'));
        $this->assertEquals('D', $mock->checkMap());
        $this->assertEquals(null, $mock->checkMap('a', 'b', 'c'));
```

*Mocking of private methods is impossible.*
*Mocking of protected methods is possible, but it's not a «best practice».*

Test-double with constructor call
---------------------------------

[](#test-double-with-constructor-call)

```
        $mock = Mocker::create(SomeClass::class, [...], [$arg1, $arg2]);
        $this->assertInstanceOf(SomeClass::class, $mock);
```

Constructor of the SomeClass will be invoked with args: $arg1, $arg2

Protected properties and methods
--------------------------------

[](#protected-properties-and-methods)

Despite the fact that testing internal implementation of classes is not the best practice, sometimes it is still necessary to set or verify a protected property, or to call a protected method.

It works both with protected properties and methods, and with private:

```
        $o = new SomeClass();
        Mocker::setProperty($o, 'protectedProperty', 'a');
        Mocker::setProperty($o, 'privateProperty', 'b');
        $this->assertEquals('a', Mocker::getProperty($o, 'protectedProperty'));
        $this->assertEquals('b', Mocker::getProperty($o, 'privateProperty'));
        $this->assertEquals('aZ', Mocker::invoke($o, 'privateMethod', ['a']));
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~17 days

Recently: every ~34 days

Total

9

Last Release

3019d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e557e816560f9c9295e3d74205ce5ac83f84ac226ffe51de5375556acae3f9e9?d=identicon)[mvkasatkin](/maintainers/mvkasatkin)

---

Top Contributors

[![mvkasatkin](https://avatars.githubusercontent.com/u/3389061?v=4)](https://github.com/mvkasatkin "mvkasatkin (25 commits)")

---

Tags

phpunitmockingmockstub

### Embed Badge

![Health badge](/badges/mvkasatkin-mocker/health.svg)

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

###  Alternatives

[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)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)[icecave/isolator

Dependency injection for global functions.

371.3M29](/packages/icecave-isolator)[janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

29221.0k28](/packages/janmarek-mockista)

PHPackages © 2026

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