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

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

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.

2.15.0(3mo ago)1718.2M—6.6%20[3 issues](https://github.com/php-mock/php-mock-phpunit/issues)20WTFPLPHPPHP &gt;=7

Since Apr 1Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/php-mock/php-mock-phpunit)[ Packagist](https://packagist.org/packages/php-mock/php-mock-phpunit)[ Docs](https://github.com/php-mock/php-mock-phpunit)[ GitHub Sponsors](https://github.com/michalbundyra)[ RSS](/packages/php-mock-php-mock-phpunit/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (36)Used By (20)

[![.github/workflows/tests.yml](https://github.com/php-mock/php-mock-phpunit/actions/workflows/tests.yml/badge.svg)](https://github.com/php-mock/php-mock-phpunit/actions/workflows/tests.yml)

Mock PHP built-in functions with PHPUnit
========================================

[](#mock-php-built-in-functions-with-phpunit)

This package integrates the function mock library [PHP-Mock](https://github.com/php-mock/php-mock) with PHPUnit.

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

[](#installation)

Use [Composer](https://getcomposer.org/):

```
composer require --dev php-mock/php-mock-phpunit
```

Usage
-----

[](#usage)

PHP-Mock integrates with the trait [`PHPMock`](http://php-mock.github.io/php-mock-phpunit/api/class-phpmock.phpunit.PHPMock.html)into your PHPUnit test case. This trait extends the framework by the method [`getFunctionMock()`](http://php-mock.github.io/php-mock-phpunit/api/class-phpmock.phpunit.PHPMock.html#_getFunctionMock). With this method you can build a mock in the way you are used to build a PHPUnit mock:

```
namespace foo;

class BuiltinTest extends \PHPUnit\Framework\TestCase
{

    use \phpmock\phpunit\PHPMock;

    public function testTime()
    {
        $time = $this->getFunctionMock(__NAMESPACE__, "time");
        $time->expects($this->once())->willReturn(3);

        $this->assertEquals(3, time());
    }

    public function testExec()
    {
        $exec = $this->getFunctionMock(__NAMESPACE__, "exec");
        $exec->expects($this->once())->willReturnCallback(
            function ($command, &$output, &$return_var) {
                $this->assertEquals("foo", $command);
                $output = ["failure"];
                $return_var = 1;
            }
        );

        exec("foo", $output, $return_var);
        $this->assertEquals(["failure"], $output);
        $this->assertEquals(1, $return_var);
    }
}
```

There's no need to disable the mocked function. The PHPUnit integration does that for you.

### Restrictions

[](#restrictions)

This library comes with the same restrictions as the underlying [`php-mock`](https://github.com/php-mock/php-mock#requirements-and-restrictions):

- Only *unqualified* function calls in a namespace context can be mocked. E.g. a call for `time()` in the namespace `foo` is mockable, a call for `\time()` is not.
- The mock has to be defined before the first call to the unqualified function in the tested class. This is documented in [Bug #68541](https://bugs.php.net/bug.php?id=68541). In most cases you can ignore this restriction. But if you happen to run into this issue you can call [`PHPMock::defineFunctionMock()`](http://php-mock.github.io/php-mock-phpunit/api/class-phpmock.phpunit.PHPMock.html#_defineFunctionMock)before that first call (e.g. with `@beforeClass`). This would define a side effectless namespaced function. Another effective approach is running your test in an isolated process (e.g. with `@runInSeparateProcess`).

License and authors
-------------------

[](#license-and-authors)

This project is free and under the WTFPL. Responsable for this project is Markus Malkusch .

### Donations

[](#donations)

If you like this project and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance80

Actively maintained with recent releases

Popularity62

Solid adoption and visibility

Community42

Growing community involvement

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 57.7% 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 ~123 days

Recently: every ~82 days

Total

33

Last Release

102d ago

Major Versions

0.3 → 1.0.02015-10-27

0.3.x-dev → 1.0.x-dev2016-08-12

1.1.x-dev → 2.0.02017-02-17

PHP version history (3 changes)0.1PHP &gt;=5.4

1.0.0PHP &gt;=5.5

2.0.0PHP &gt;=7

### Community

Maintainers

![](https://www.gravatar.com/avatar/06864e2afad0a0cb0b46016664bf5895c3378c8b413e0bddd03075011fc6d731?d=identicon)[webimpress](/maintainers/webimpress)

![](https://www.gravatar.com/avatar/97ee86e5e22b92173b2787cad0ba7ac144109e126221c6d21adecdae32ef37fc?d=identicon)[malkusch](/maintainers/malkusch)

---

Top Contributors

[![michalbundyra](https://avatars.githubusercontent.com/u/7423207?v=4)](https://github.com/michalbundyra "michalbundyra (64 commits)")[![malkusch](https://avatars.githubusercontent.com/u/1623984?v=4)](https://github.com/malkusch "malkusch (40 commits)")[![chrisminett](https://avatars.githubusercontent.com/u/1084019?v=4)](https://github.com/chrisminett "chrisminett (2 commits)")[![crocodele](https://avatars.githubusercontent.com/u/1522694?v=4)](https://github.com/crocodele "crocodele (1 commits)")[![jimlind](https://avatars.githubusercontent.com/u/500897?v=4)](https://github.com/jimlind "jimlind (1 commits)")[![athos-ribeiro](https://avatars.githubusercontent.com/u/2052794?v=4)](https://github.com/athos-ribeiro "athos-ribeiro (1 commits)")[![marcoscoelho](https://avatars.githubusercontent.com/u/503693?v=4)](https://github.com/marcoscoelho "marcoscoelho (1 commits)")[![MasterOdin](https://avatars.githubusercontent.com/u/1845314?v=4)](https://github.com/MasterOdin "MasterOdin (1 commits)")

---

Tags

builtin-functionsmockphpphp-mockphpunittestingphpunittestBDDTDDmockstubtest doublefunction

### Embed Badge

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

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

###  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-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)[php-mock/php-mock-prophecy

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

16496.6k15](/packages/php-mock-php-mock-prophecy)[php-mock/php-mock-integration

Integration package for PHP-Mock

1410.5M3](/packages/php-mock-php-mock-integration)[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)
