PHPackages                             michalv8/xpmock - 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. michalv8/xpmock

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

michalv8/xpmock
===============

PHPUnit: simple syntax to create mock-objects

2.0.0(5y ago)168.1k↓28.1%11MITPHPPHP &gt;=7.3

Since Jun 11Pushed 5y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (17)Used By (1)

xpmock
======

[](#xpmock)

[![Build Status](https://camo.githubusercontent.com/9bd418ad5eca7e78c6a515738280cdd0666c48fb4f0618fcbe338729cefe7b5b/68747470733a2f2f7472617669732d63692e6f72672f6d696368616c76382f78706d6f636b2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/ptrofimov/xpmock)[![Latest Stable Version](https://camo.githubusercontent.com/a32b5b4fdb5742a830011981937b2f48d6c04aa73cc21a87e421448c3bf74bb3/68747470733a2f2f706f7365722e707567782e6f72672f6d696368616c76382f78706d6f636b2f762f737461626c652e706e67)](https://packagist.org/packages/ptrofimov/xpmock)[![Total Downloads](https://camo.githubusercontent.com/e909d90320bad69cdb4f6b22535572c9903b552003ace0675ebaf46b8c6c898b/68747470733a2f2f706f7365722e707567782e6f72672f6d696368616c76382f78706d6f636b2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/ptrofimov/xpmock)

Introduction [\[in English\]](http://ptrofimov.github.io/) [\[на русском\]](http://habrahabr.ru/post/183010/)

**!!! New version 1.1.5: [multiple improvements](https://github.com/ptrofimov/xpmock#new)**

PHPUnit is standard testing framework in PHP world. No wonder - it is nifty. But speaking about way of mocking objects in PHPUnit many people complain on a bit redundant syntax. They suggest many addons for PHPUnit to create mocks like Mockery (I know this is not just addon).

But I am sure PHPUnit has well-developed mocking system. And this project XPMock is a way to simplify this syntax a bit. I need to underline that XPMock doesn't create some own mock objects. XPMock just calls the same PHPUnit methods creating the same native mocks but much simpler.

### Write this

[](#write-this)

```
$this->mock('MyClass')
    ->getBool(true)
    ->getNumber(1)
    ->getString('string')
    ->new();
```

### instead of that

[](#instead-of-that)

```
$mock = $this->getMockBuilder('MyClass')
    ->setMethods(['getBool', 'getNumber', 'getString'])
    ->disableOriginalConstructor()
    ->getMock();
$mock->expects($this->any())
    ->method('getBool')
    ->will($this->returnValue(true));
$mock->expects($this->any())
    ->method('getNumber')
    ->will($this->returnValue(1));
$mock->expects($this->any())
    ->method('getString')
    ->will($this->returnValue('string'));
```

Tool generates full-functional native PHPUnit mocks.

Syntax short description
------------------------

[](#syntax-short-description)

```
// init mock writer

$this->mock('MyClass') // init mock (all methods are real by default)

// mock methods

// $mock->expects($this->any())->method('getNumber')->will($this->returnValue(null))
->getNumber()
// $mock->expects($this->any())->method('getNumber')->will($this->returnValue(1))
->getNumber(1)
// $mock->expects($this->any())->method('getNumber')->will($this->returnValue(1))
->getNumber($this->returnValue(1))
// $mock->expects($this->once())->method('getNumber')->will($this->returnValue(null))
->getNumber($this->once())
// $mock->expects($this->once())->method('getNumber')->will($this->returnValue(1))
->getNumber(1, $this->once())
// $mock->expects($this->at(0))->method('getNumber')->will($this->returnValue(1))
// $mock->expects($this->at(1))->method('getNumber')->will($this->returnValue(2))
->getNumber(1, $this->at(0))
->getNumber(2, $this->at(1))
// $mock->expects($this->once())->method('getNumber')->with(1,2,3)->will($this->returnValue(null))
->getNumber([1,2,3], $this->once())
// $mock->expects($this->any())->method('getNumber')->with(1,2,3)->will($this->returnValue(1))
->getNumber([1,2,3], 1)
// $mock->expects($this->once())->method('getNumber')->with(1,2,3)->will($this->returnValue(1))
->getNumber([1,2,3], 1, $this->once())
// $mock->expects($this->any())->method('getNumber')->will($this->returnCallback(function(){}))
->getNumber(function(){})
// $mock->expects($this->any())->method('getNumber')->will($this->throwException(new \Exception('')))
->getNumber(new \Exception(''))

// create mock

// $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock()
->new()
// $this->getMockBuilder('MyClass')->setConstructorArgs([1,2,3])->getMock()
->new(1, 2, 3)
```

Handy reflection methods
------------------------

[](#handy-reflection-methods)

```
// get value of any property: static/non-static, public/protected/private

$value = $this->reflect('MyClass')->property; // class name (only static)
$value = $this->reflect(new MyClass())->property; // object

// set value of any property: static/non-static, public/protected/private property

$this->reflect('MyClass')->property = $value; // class name (only static)
$this->reflect(new MyClass())->property = $value; // object
$this->reflect(new MyClass())
    ->__set('property1', $value1)
    ->__set('property2', $value2); // chain

// call any method: static/non-static, public/protected/private

$this->reflect('MyClass')->method($arg); // class name (only static)
$this->reflect(new MyClass())->method($arg); // object
```

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

[](#installation)

1. If you don't have composer, [install it](http://getcomposer.org)
2. Add xpmock to your project

```
composer require ptrofimov/xpmock:dev-master

```

Usage
-----

[](#usage)

Option 1. Add trait to existing test case:

```
class MyTestCase extends \PHPUnit_Framework_TestCase
{
    use \Xpmock\TestCaseTrait;
}
```

OR Option 2. Extend your test case from xpmock's one:

```
class MyTestCase extends \Xpmock\TestCase
{

}
```

NEW
---

[](#new)

1. If you need to create object with some methods and classname doesn't matter, you could easily do it like this:

```
$mock = $this->mock()
    ->getString('string')
    ->getNumber(function () {
        return 2 + 2;
    })
    ->new();
```

2. Mocking static methods
3. Mocking abstract methods
4. Brief syntax to create mocks

```
$mock = $this->mock('MyClass', ['getNumber' => 1]);
```

5. Special method this() inside each mock gives you possibility to change non-public properties and call non-public methods of mock via Xpmock\\Reflection

```
$mock = $this->mock('MyClass')->new();
$mock->this()->protectedProperty = 'value';
$mock->this()->protectedMethod();
```

6. You can use $this pointer inside your fake methods

```
$mock = $this->mock('MyClass',
    [
        'property' => 1,
        'getProperty' => function () {
            return $this->property;
        },
    ]
);
```

7. It is easy now to create mocks all methods of which return some value, for example null (stub)

```
$mock = $this->mock('MyClass', null);
```

8. Very expected: now it is possible to adjust mocks after creation with magic method mock()

```
$myClass = $this->mock('MyClass', null);
$myClass->getNumber(); // null
$myClass->mock()
    ->getNumber(1);
$myClass->getNumber(); // 1
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% 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 ~209 days

Recently: every ~628 days

Total

14

Last Release

2007d ago

Major Versions

1.2.1 → 2.0.02020-11-18

PHP version history (3 changes)1.0.0PHP &gt;=5.3.0

1.2.0PHP &gt;=7.0

2.0.0PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ad071a05b3aee6495d46214602ffabf964cd7beb64905b8c662f602184b88e0?d=identicon)[michalv8](/maintainers/michalv8)

---

Top Contributors

[![ptrofimov](https://avatars.githubusercontent.com/u/867178?v=4)](https://github.com/ptrofimov "ptrofimov (70 commits)")[![michalv8](https://avatars.githubusercontent.com/u/1423150?v=4)](https://github.com/michalv8 "michalv8 (9 commits)")[![jeromemacias](https://avatars.githubusercontent.com/u/582446?v=4)](https://github.com/jeromemacias "jeromemacias (3 commits)")[![michaelmoussa](https://avatars.githubusercontent.com/u/183833?v=4)](https://github.com/michaelmoussa "michaelmoussa (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/michalv8-xpmock/health.svg)

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[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)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

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