PHPackages                             tcz/phpunit-mockfunction - 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. tcz/phpunit-mockfunction

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

tcz/phpunit-mockfunction
========================

PHPUnit extension that uses runkit to mock PHP functions (both user-defined and system)

v1.0.0(12y ago)4950.3k72PHP

Since Mar 20Pushed 7y ago4 watchersCompare

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

READMEChangelogDependenciesVersions (3)Used By (2)

Introduction
------------

[](#introduction)

MockFunction is a PHPUnit extension that uses `runkit` to mock PHP functions (both user-defined and system) or static methods and use mockobject-style invocation matchers, parameter constraints and all that magic.

To use this extension, you have to install `runkit` first (PECL package). For a working version see

To be able to mock system function (not user-defined ones), you need to turn on `runkit.internal_override` in the PHP config.

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

[](#installation)

If you use composer, installing MockFunction is easy:

```
"require-dev": {
    "tcz/phpunit-mockfunction": "1.0.0"
}

```

Then

```
php composer.phar update tcz/phpunit-mockfunction

```

Usage
-----

[](#usage)

Assuming you are in a PHPUnit test:

```
// Back to the future:
$flux_capacitor = new PHPUnit_Extensions_MockFunction( 'time', $this->object );
$einsteins_clock = time() + 60;
$flux_capacitor->expects( $this->atLeastOnce() )->will( $this->returnValue( $einsteins_clock ) );

```

Where `$flux_capacitor` is the stub function. It can be set up with the same fluent interface as a `MockObject` (excluding method, of course).

The 2nd parameter of the constructor (`$this->object`) is the object where we expect the function to be called. The "mocking" only takes effect from here, from all the other sources it will execute the "normal" function (see next line).

Variable `$einsteins_clock` contains the value that we will return instead of the "regular" value (we add 1 minute for the current time).

In the next line we set up the mock function with the fluent interface of a mock object.

The mocked function is active for the test object instance until `$flux_capacitor->restore();` is called. If you happen to forget this in the end of the test case, normally it is not a problem, because you will test anew instance of your tested class with each test case.

To mock static methods, you can use PHPUnit\_Extensions\_MockStaticMethod class. It work int the same way as with functions:

```
$mocked_static = new PHPUnit_Extensions_MockStaticMethod( 'MyClass::myMethod', $this->object );

```

Advanced mocking
----------------

[](#advanced-mocking)

You can use all invocation matchers, constraints and stub returns, for example:

```
// This will execute the original function at the end, but will test
// the number of exections ( $this->once() ) and the correct parameter ( $this->equalTo() ).
$mocked_strrev = new PHPUnit_Extensions_MockFunction( 'strrev', $this->object );
$mocked_strrev->expects( $this->once() )->with( $this->equalTo( 'abc' ) )->will( $this->returnCallback( 'strrev' ) );

// This object cannot execute shell_exec.
$mocked_shell = new PHPUnit_Extensions_MockFunction( 'shell_exec', $this->object );
$mocked_shell->expects( $this->never() );

// Expecting to check the existence of 2 file, returning true for both.
$mocked_file_exists = new PHPUnit_Extensions_MockFunction( 'file_exists', $this->object );
$mocked_file_exists->expects( $this->exactly( 2 ) )
    ->with(
        $this->logicalOr(
            $this->equalTo( '/tmp/file1.exe' ),
            $this->equalTo( '/tmp/file2.exe' )
        )
    )->will( $this->returnValue( true ) );

```

For further information see

[![Bitdeli Badge](https://camo.githubusercontent.com/03cd7147503ba0adc7b39aa8beb7a0496b9800aa970c60c97b591bbf6b4438ba/68747470733a2f2f64327765637a68766c38323376302e636c6f756466726f6e742e6e65742f74637a2f706870756e69742d6d6f636b66756e6374696f6e2f7472656e642e706e67)](https://bitdeli.com/free "Bitdeli Badge") [![Build Status](https://camo.githubusercontent.com/f2ec88cac9b9a421b42d0eeb3f2cb495eeb8006ec98d005fde0553d3ac30a5a7/68747470733a2f2f7472617669732d63692e6f72672f74637a2f706870756e69742d6d6f636b66756e6374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tcz/phpunit-mockfunction)

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 63.2% 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

Unknown

Total

1

Last Release

4442d ago

### Community

Maintainers

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

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

---

Top Contributors

[![tcz](https://avatars.githubusercontent.com/u/160008?v=4)](https://github.com/tcz "tcz (12 commits)")[![atmattpatt](https://avatars.githubusercontent.com/u/1574315?v=4)](https://github.com/atmattpatt "atmattpatt (6 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")

---

Tags

testingphpunitunitmockrunkit

### Embed Badge

![Health badge](/badges/tcz-phpunit-mockfunction/health.svg)

```
[![Health](https://phpackages.com/badges/tcz-phpunit-mockfunction/health.svg)](https://phpackages.com/packages/tcz-phpunit-mockfunction)
```

###  Alternatives

[dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

57026.3M456](/packages/dg-bypass-finals)[nette/tester

Nette Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏

4917.3M1.5k](/packages/nette-tester)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33812.5M350](/packages/brain-monkey)[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)[donatj/mock-webserver

Simple mock web server for unit testing

1382.5M80](/packages/donatj-mock-webserver)[blastcloud/guzzler

Supercharge your app or SDK with a testing library specifically for Guzzle.

272419.3k35](/packages/blastcloud-guzzler)

PHPackages © 2026

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