PHPackages                             betterphp/php-native-mock - 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. betterphp/php-native-mock

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

betterphp/php-native-mock
=========================

PHP Native Mock is a helper class that allows for easy mocking of native PHP functions and class methods in unit tests using the UOPZ extension.

1.1.0(8y ago)011.3k↓25%[2 issues](https://github.com/betterphp/php-native-mock/issues)GPL-3.0PHPPHP &gt;= 7.0.0

Since May 6Pushed 8y ago1 watchersCompare

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

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

PHP Native Mock
===============

[](#php-native-mock)

Helper class to mock or redefine native PHP functions in unit tests. Works using the [UOPZ module](https://github.com/krakjoe/uopz), developed and tested against the master branch.

[![Build Status](https://camo.githubusercontent.com/6e108de5940ba31083ab3c0c3d5f3d516816c1d7cbfed443298dabfa1fccc3a0/68747470733a2f2f63692e6a6163656b6b2e636f2e756b2f6275696c645374617475732f69636f6e3f6a6f623d5048502532304e61746976652532304d6f636b)](https://ci.jacekk.co.uk/job/PHP%20Native%20Mock)

This is largely a wrapper around the uopz functions with some extra bits thrown in to make it a bit easier in tests. It basically relies on dark magic and, like all magic, should only be used very carefully. Creating weird behaviour in applications is very easy, for example

```
$this->redefineFunction('substr', function () {
    return 'Doge';
});
```

would have some very strange effects.

Most of the time when redefining a function sounds like a good idea - it's probably not, make sure there is no way a "normal" mocking approach won't work first.

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

[](#installation)

The library can be included via composer

```
{
    "require-dev": {
        "betterphp/php-native-mock": "~1"
    }
}
```

Documentation
-------------

[](#documentation)

Jenkins publishes a phpdoc [here](https://ci.jacekk.co.uk/view/Websites/job/PHP%20Native%20Mock/API_Docs/classes/betterphp.native_mock.native_mock.html)

Testing
-------

[](#testing)

We use phpcs and phpunit for testing, run both before commiting anything

```
./vendor/bin/phpcs -p --standard=./ruleset.xml .

```

```
./vendor/bin/phpunit -c ./phpunit.xml

```

phpunit will do code coverage checking which requires xdebug, if it's not installed this will fail gracefully - not to worry.

A report of the test coverage is published [here by Jenkins](https://ci.jacekk.co.uk/job/PHP%20Native%20Mock/HTML_Code_Coverage/index.html)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 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 ~87 days

Total

2

Last Release

3211d ago

### Community

Maintainers

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

---

Top Contributors

[![betterphp](https://avatars.githubusercontent.com/u/788007?v=4)](https://github.com/betterphp "betterphp (18 commits)")

---

Tags

testmockfunctionunit testnativeredefine

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/betterphp-php-native-mock/health.svg)

```
[![Health](https://phpackages.com/badges/betterphp-php-native-mock/health.svg)](https://phpackages.com/packages/betterphp-php-native-mock)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

36918.1M98](/packages/php-mock-php-mock)[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)[kahlan/kahlan

The PHP Test Framework for Freedom, Truth and Justice.

1.2k1.2M247](/packages/kahlan-kahlan)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33412.5M350](/packages/brain-monkey)[php-mock/php-mock-mockery

Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.

392.1M96](/packages/php-mock-php-mock-mockery)

PHPackages © 2026

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