PHPackages                             fnmock/fnmock - 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. fnmock/fnmock

ActiveLibrary

fnmock/fnmock
=============

Mock PHP functions

0.1.1(11y ago)245PHP

Since Mar 26Pushed 11y agoCompare

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

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

FnMock
======

[](#fnmock)

A PHP testing tool for mocking functions.

Most mocking frameworks only allow mocking of objects. FnMock is a tiny class that makes it easy to test functions outside the context of a class.

> **Important**: Mocking functions is only supported when the function caller is within a namespace.

Example
-------

[](#example)

```
// Code
namespace Http;

class Client {
    public function touch($url) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        $response = curl_exec($ch);
        curl_close($ch);

		return $response;
    }
}
```

```
// Test
use FnMock\FnMock;

$client = new Http\Client();

FnMock::mock('Http\curl_exec', function($ch) {
    $url = curl_getinfo($ch)['url'];
    assert($url === 'http://google.com');
	return 'Fake response from server';
});

$response = $client->touch('http://google.com');
assert($response === 'Fake response from server');

// Important: Reset FnMock as part of your tearDown process
FnMock::reset();
```

Install
-------

[](#install)

```
composer require fnmock/fnmock

```

Integrate it with PHPUnit
-------------------------

[](#integrate-it-with-phpunit)

```
use FnMock\FnMock;

class TestCase extends \PHPUnit_Framework_TestCase
{
    protected function mockFunction($fn, callable $callback)
    {
        FnMock::mock($fn, $callback);
    }

    public function tearDown()
    {
        FnMock::reset();
        parent::tearDown();
    }
}
```

Or just use the trait we provide

```
use FnMock\PHPUnitTrait;

class TestCase extends \PHPUnit_Framework_TestCase
{
    use PHPUnitTrait;
}
```

License
-------

[](#license)

FnMock is licensed under the MIT License - see the `LICENSE.txt` file for details

Author
------

[](#author)

Adam Nicholson -

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

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

Total

2

Last Release

4071d ago

### Community

Maintainers

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

---

Top Contributors

[![adamnicholson](https://avatars.githubusercontent.com/u/7761178?v=4)](https://github.com/adamnicholson "adamnicholson (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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