PHPackages                             jeremeamia/func-mocker - 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. jeremeamia/func-mocker

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

jeremeamia/func-mocker
======================

Allows you to overwrite global function used within a namespace for the purposes of testing.

0.1.0(10y ago)19221MITPHPPHP ~5.6|~7.0

Since Mar 15Pushed 10y ago3 watchersCompare

[ Source](https://github.com/jeremeamia/php-func-mocker)[ Packagist](https://packagist.org/packages/jeremeamia/func-mocker)[ Docs](https://github.com/jeremeamia/php-func-mocker)[ RSS](/packages/jeremeamia-func-mocker/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

PHP FuncMocker
==============

[](#php-funcmocker)

**FuncMocker** – Mocking PHP functions like a... punk rocker?

[![Latest Version on Packagist](https://camo.githubusercontent.com/fc26cf13fd9868cb99d50c5810df5320b07725b684a48158a4ef93fa14bcd688/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6572656d65616d69612f66756e632d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeremeamia/func-mocker)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/e57264c83a7389f9d16eb6bcdad9a54a43b4496c4eff551070dacaffc6670803/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6572656d65616d69612f7068702d66756e632d6d6f636b65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/jeremeamia/php-func-mocker)[![Total Downloads](https://camo.githubusercontent.com/0e1488af1811c874b457e48a7cfd226afe7cff0592d63498aafea36bf52f0a3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6572656d65616d69612f66756e632d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeremeamia/func-mocker)

Allows you to overwrite (i.e., mock) global functions used within a given namespace for the purposes of testing.

There are *two* main use cases I developed this for:

1. When you are testing objects that call non-deterministic functions like `time()` and `rand()`, and you need these functions to return deterministic values for the sake of the test.
2. When you are working with code where you have objects that must make calls to functions. This is pretty common in legacy codebases that were not previously object-oriented in nature. For example, if you're project has a function called `db_add()` that you end up using in an object in your model layer, you might want to "mock" that function when you are testing so you don't actually make calls to the database in your unit tests.

The simple technique behind this code is described in this [blog post by Fabian Schmengler](http://www.schmengler-se.de/en/2011/03/php-mocking-built-in-functions-like-time-in-unit-tests/). Basically, it involves taking advantage of PHP's [namespace resolution rules](http://php.net/manual/en/language.namespaces.rules.php).

Install
-------

[](#install)

Via Composer

```
$ composer require jeremeamia/func-mocker
```

Usage
-----

[](#usage)

Let's you have a `RandomNumberGenerator` class in the namespace, `My\App`, that calls the global function `rand()`. You could overwrite the usage of `rand()` for that particular namespace by using **FuncMocker**.

```
use My\App\RandomNumberGenerator;
use FuncMocker\Mocker;

Mocker::mock('rand', 'My\App', function () {
    return 5;
});

$rng = new RandomNumberGenerator(1, 10);
echo $rng->getNumber();
//> 5
echo $rng->getNumber();
//> 5
echo $rng->getNumber();
//> 5
```

### Longer Example

[](#longer-example)

Assume there is a class that uses the global function (e.g., `time()`) that you'd like to mock.

```
