PHPackages                             chmeldax/phpmocks - 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. chmeldax/phpmocks

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

chmeldax/phpmocks
=================

Enhanced mocking of PHP classes for testing purposes

v0.1(9y ago)015MITPHP

Since Mar 5Pushed 9y agoCompare

[ Source](https://github.com/chmeldax/phpmocks)[ Packagist](https://packagist.org/packages/chmeldax/phpmocks)[ Docs](https://github.com/chmeldax/phpmocks)[ RSS](/packages/chmeldax-phpmocks/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

PhpMocks
========

[](#phpmocks)

Making mocking a pleasure!

*Library to be used for testing classes*.

Features
--------

[](#features)

- Stubbing and mocking of all public class methods
- Supports abstract classes and interfaces
- Mocking of **static** methods
- Different behaviors of the mock **based on the method call parameters**
- Strict mocking by default (calls to non-mocked method are disabled by default)
- Fluent intuitive interface

Requirements
------------

[](#requirements)

- PHP 5.6+
- Composer
- allowed `eval` language construct

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

[](#installation)

```
composer require chmeldax/phpmocks

```

Usage
-----

[](#usage)

### Stubs

[](#stubs)

When you want to replace default behavior of the class, but you are not testing the method calls themselves, then use `allowMethodCall()`.

```
$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($className);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1');
$double = $doubleBuilder->build(); // This is the stub
```

#### Specifying return values

[](#specifying-return-values)

```
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1', 'return_value_2'); // Returns different values on #1 and #2 call
```

```
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andInvoke(function ($a, $b, $c) { // specify your own callback
        return 'return_value'
      });
```

```
$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($instance);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andCallOriginal(); // Calls the original method from $instance
```

#### Multiple with blocks

[](#multiple-with-blocks)

```
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->andReturn('return_value_1'); // Returns different values on #1 and #2 call

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second') // Different parameter value
    ->andReturn('return_value_2');

$double = $doubleBuilder->build();
$double->methodToBeStubbed(1, 2, 'first');  // returns 'return_value_1'
$double->methodToBeStubbed(1, 2, 'second'); // returns 'return_value_2'
```

See `tests/DoubleTest.php` for more examples.

### Mocks

[](#mocks)

When you are testing the interaction with class, use `expectMethodCall`. It will allow you to specify expected calls.

```
$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, null)
    ->times(10) // Expectation
    ->andReturn('return_value_1');
```

```
$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->atCalls(1, 3) // Expectation for 1st and 3rd call (counted globally for all "with" blocks)
    ->andReturn('return_value_1');

$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second')
    ->atCall(2) // Expectation for 2nd call (counted globally for all "with" blocks)
    ->andReturn('return_value_2');
```

#### Checking expectations

[](#checking-expectations)

```
$doubleBuilder->checkExpectations() // Throws exception if any expectation is not met
```

It is advised to use `tearDown()` (or similar method) in your testing framework.

See `tests/ExpectedMethodTest.php` for more examples.

Things to implement
-------------------

[](#things-to-implement)

- Support for PHP 7 (mainly return values, scalar types)
- Implement better Constraints
- Clean the code
- Introduce true unit tests

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

3352d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/56ab1763848a72246787031260e6e1eaef256a0c4df5bb176aee6e9162c896f2?d=identicon)[chmeldax](/maintainers/chmeldax)

---

Tags

testingmocks

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/chmeldax-phpmocks/health.svg)

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

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k910.7M134.6k](/packages/phpunit-phpunit)[phpunit/php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.

8.9k892.4M1.5k](/packages/phpunit-php-code-coverage)[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.5k](/packages/mockery-mockery)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[symfony/phpunit-bridge

Provides utilities for PHPUnit, especially user deprecation notices management

2.5k201.2M4.2k](/packages/symfony-phpunit-bridge)[brianium/paratest

Parallel testing for PHP

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

PHPackages © 2026

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