PHPackages                             tomaszdurka/mocka - 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. tomaszdurka/mocka

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

tomaszdurka/mocka
=================

0.14.0(8y ago)627.3k7[2 issues](https://github.com/tomaszdurka/mocka/issues)[1 PRs](https://github.com/tomaszdurka/mocka/pulls)4MITPHP

Since Jun 10Pushed 3y ago2 watchersCompare

[ Source](https://github.com/tomaszdurka/mocka)[ Packagist](https://packagist.org/packages/tomaszdurka/mocka)[ RSS](/packages/tomaszdurka-mocka/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (4)Versions (65)Used By (4)

Mocka
=====

[](#mocka)

[![Travis Build](https://camo.githubusercontent.com/dee142549e551018fe33f3860e6480bf5ce3e551ac7402f4decd0f8bb1974830/68747470733a2f2f6170692e7472617669732d63692e6f72672f746f6d61737a6475726b612f6d6f636b612e706e67)](https://travis-ci.org/tomaszdurka/mocka)[![Coverage Status](https://camo.githubusercontent.com/ae4e5401a11a59d8ad09ed8edccb0eea1b792c81ac359b93c11c324e4b90da8e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746f6d61737a6475726b612f6d6f636b612f62616467652e706e67)](https://coveralls.io/r/tomaszdurka/mocka)

About
-----

[](#about)

Mocka is clean, structured, but still very flexible mocking framework. It follows object-oriented guidelines to create mock classes, objects and their methods. Mocks can be modified during runtime before and once created or used. It has no assertion, expectations framework built-in - still fits well into any testing framework.

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

[](#installation)

Mocka is registered as composer package on [packagist](https://packagist.org/packages/tomaszdurka/mocka).

```
"tomaszdurka/mocka": "dev-master"

```

Library usage
-------------

[](#library-usage)

Mocking classes

```
$parentClassName = 'Exception';
$class = new ClassMock('MockedException', $parentClassName);
$exception1 = $class->newInstance(['exception message as constructor argument']);
$exception2 = $class->newInstanceWithoutConstructor();
```

Creating object of mocked classes

```
$class = new ClassMock('MockedException', 'Exception');
$object = $class->newInstance('message');
$object->getMessage();
```

Mocking methods

```
$class = new ClassMock('MockedException', 'Exception');
$class->mockMethod('getMessage');

// It's also possible to mock method only for generated object
$object = $class->newInstance('message');
$object->mockMethod('getMessage');

// It's possible to mock non-existent methods - they will work once mocked
$class->mockMethod('foo');

// It's also possible to mock static methods
$class->mockStaticMethod('bar');
```

Modifying method behaviour

```
// Each method returned by any above mock methods return MethodMock object which can be manipulated
$class = new ClassMock('MockedException', 'Exception');
$mockedMethod = $class->mockMethod('getMessage');

// Set closure which will be executed when mocked method is called
$class->mockMethod('getMessage')->set(function () {
    return 'modified message';
});

// Set numbered callbacks
$class->mockMethod('getMessage')
    ->set(function () {
        return 'default message';
    })
    ->at(0, function () {
        return 'first message';
    })
    ->at(2, function () {
        return 'third message';
    });

// There is also shortcut to make method return certain value
$class->mockMethod('getMessages')->set('default')->at(0, 'first message');

// To check how many times method has been called simply use mocked method object
$mockedMethod = $class->mockMethod('getMessage');
// call method...
echo $mockedMethod->getCallCount();
```

Mocking interfaces

```
$countableClass = new ClassMock('Collection', null, ['Countable']);
```

Referring back to original method

```
// It's a way to add extra behaviour to original method functionality
$class = new ClassMock('MockedException', 'Exception');
$object = $class->newInstanceWithoutConstructor();
$object->mockMethod('getMessage')->set(function() use ($object) {
    return 'prefix-' . $object->callOriginalMethod('getMessage', func_get_args());
});
```

Using with test framework like PHPUnit
--------------------------------------

[](#using-with-test-framework-like-phpunit)

```
class TestCase extends \PHPUnit_Framework_TestCase {

    use \Mocka\MockaTrait;

    public function testFoo() {
        // When using mocka trait there are two shortcut methods added to create mocked objects
        $countableExceptionClass = $this->mockClass('DateTime', ['Countable']);
        $dateTimeObject = $this->mockObject('DateTime', ['29-12-1984']);
    }

    public function testMockingMethod() {
        $dateTimeObject = $this->mockObject('DateTime', ['29-12-1984']);
        $this->assertSame('29', $dateTimeObject->format('d'));
        $mockedFormatMethod = $dateTimeObject->mockMethod('format')->set('foo');
        $this->assertSame(0, $mockedFormatMethod->getCallCount());
        $this->assertSame('foo', $dateTimeObject->format('d'));
        $this->assertSame(1, $mockedFormatMethod->getCallCount());
    }

    public function testMethodAssertions() {
        $pdo = $this->mockClass('PDO')->newInstanceWithoutConstructor();
        $pdo->mockMethod('exec')->set(function ($statement) {
            $this->assertInstanceOf('PDOStatement', $statement);
        });
    }
}
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 96.1% 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 ~51 days

Recently: every ~175 days

Total

28

Last Release

2975d ago

### Community

Maintainers

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

---

Top Contributors

[![tomaszdurka](https://avatars.githubusercontent.com/u/113303?v=4)](https://github.com/tomaszdurka "tomaszdurka (147 commits)")[![alexispeter](https://avatars.githubusercontent.com/u/1921425?v=4)](https://github.com/alexispeter "alexispeter (5 commits)")[![dlondero](https://avatars.githubusercontent.com/u/209782?v=4)](https://github.com/dlondero "dlondero (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tomaszdurka-mocka/health.svg)

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

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