PHPackages                             idimsh/php-internals-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. idimsh/php-internals-mocker

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

idimsh/php-internals-mocker
===========================

Helps mocking PHP internal functions calls

v1.0.0(6y ago)07MITPHPPHP ~7.1CI failing

Since Dec 3Pushed 6y ago1 watchersCompare

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

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

PHP Internal function mocker
============================

[](#php-internal-function-mocker)

[![Latest Version on Packagist](https://camo.githubusercontent.com/52954f7e28b59944563328d27d7ae618b86c3aa343fbcef154f2aaa47d2cee0e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6964696d73682f7068702d696e7465726e616c732d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/idimsh/php-internals-mocker)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/ce0f73ca0cbeb40da3595f16232a76873a719de8eb0f75b2f09d59c3e1a685b5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6964696d73682f7068702d696e7465726e616c732d6d6f636b65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/idimsh/php-internals-mocker)[![Coverage Status](https://camo.githubusercontent.com/af34ef7f4916c372a09cfee96df17b48a775f837ca3abdda9e969bc4280c84cb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6964696d73682f7068702d696e7465726e616c732d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/idimsh/php-internals-mocker/code-structure)[![Quality Score](https://camo.githubusercontent.com/3e6e87cd63cc3448ea972c7ec03e098a59f848b405ae616e4d58a8cc23550339/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6964696d73682f7068702d696e7465726e616c732d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/idimsh/php-internals-mocker)[![Total Downloads](https://camo.githubusercontent.com/23484003b4676293babc1e52ff5505b6900d16d89a1827861936b2ecc9f986f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6964696d73682f7068702d696e7465726e616c732d6d6f636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/idimsh/php-internals-mocker)[![PHP Version](https://camo.githubusercontent.com/70051b2dd43be8526a39bfb60f861e07adcc5951da75c6f06191a64b8b0b474d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6964696d73682f7068702d696e7465726e616c732d6d6f636b65723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/idimsh/php-internals-mocker)

Util to allow mocking PHP Internal function calls in tests.

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

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). Run the following command to install the latest version of a package and add it to your project's `composer.json`:

```
composer require-dev idimsh/php-internals-mocker
```

Usage
-----

[](#usage)

This mocker is intended to be used in Unit Tests, assume a class like this:

```
namespace Vendor\Namespace

class MyClass
{
    public function openConnction($hostname)
    {
        return fsockopen($hostname);
    }
}
```

Has to be tested with unit tests for method `openConnction()`. A PhpUnit test case would be like:

```
namespace VendorTest\Namespace

class MyClassTest extends \PHPUnit\Framework\TestCase
{
    public function testOpenConnction(): void
    {
        $object   = new \Vendor\Namespace\MyClass;
        $hostname = \uniqid('hostname');
        $actual   = $object->openConnection($hostname);
        // ...
    }
}
```

We do not really want to open a connection especially in unit tests, so this mocker can avoid the call to the native PHP `fsockopen()` and replace it with a call to a defined callback like:

```
namespace VendorTest\Namespace

use idimsh\PhpInternalsMocker\PhpFunctionSimpleMocker;

class MyClassTest extends \PHPUnit\Framework\TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        PhpFunctionSimpleMocker::reset();
    }

    public function testOpenConnction(): void
    {
        $hostname = \uniqid('hostname');
        $return   = \uniqid('some mock for the return of fsockopen()');

        PhpFunctionSimpleMocker::add(
            'fsockopen',
            \Vendor\Namespace\MyClass::class,
            function ($inputHostname) use ($hostname, $return) {
                static::assertSame($inputHostname, $hostname);
                return $return;
            }
        );

        $object = new \Vendor\Namespace\MyClass;
        $actual = $object->openConnection($hostname);
        static::assertSame($return, $actual);

        /** @noinspection PhpUnhandledExceptionInspection */
        PhpFunctionSimpleMocker::phpUnitAssertNotEnoughCalls($this);
    }
}
```

Methods Manual
--------------

[](#methods-manual)

1- `PhpFunctionSimpleMocker::reset()`: should be called in PhpUnit TestCase `setUp()` method or at the beginning of a test method.

2- `PhpFunctionSimpleMocker::add()`: to be called after `reset()` to register the callbacks expected to native functions, signature:

```
    /**
     * Register a call back to be called for the PHP internal function which is to be used in the class passed.
     *
     * If the $callback is null, then this PHP function is not expected to be called.
     *
     * Assertions can be done inside the callback.
     *
     * @param string        $internalFunctionName The PHP function name to mock
     * @param string        $beingCalledFromClass The class FQN which calls $internalFunctionName
     * @param callable|null $callback
     * @param int           $numberOfCalls        To mock more than once for the same callback, pass the number here
     */
    public static function add(
        string $internalFunctionName,
        string $beingCalledFromClass,
        ?callable $callback,
        int $numberOfCalls = 1
    ): void
```

It can be called multiple times with the same `$internalFunctionName` and different `$callback` for each call in the order expected.
The `$beingCalledFromClass` expects a class FQN which from the namespace will be extracted and the function will be registered at that namespace.

3- `PhpFunctionSimpleMocker::phpUnitAssertNotEnoughCalls($testCase)`: To be called from PhpUnit test method after all the assertions have been registered (last line), this method will make sure that the minimum number of calls has been reached.

4- `PhpFunctionSimpleMocker::assertPostConditions(?$testCase)`: Alternative to `PhpFunctionSimpleMocker::phpUnitAssertNotEnoughCalls($testCase)` and to be called from PhpUnit TestCase method: `assertPostConditions()`, instead of calling the previous method at the end of each Test method, a one call passing the TestCase is enough to assert minimum count.

Usage Conditions
----------------

[](#usage-conditions)

The native PHP function call that is to be mocked and replaced with a callback needs to be (All must apply):

- Called from a class method or a function that is defined inside a namespace and not from a class method or a function which reside in the global namespace.
- The call that PHP native function must not be preceeded by the global namespace resolution operator '\\'
- The `use function` statement is not used to import that native function into the namespace in the class.

Limitations
-----------

[](#limitations)

Quickly:

- PHP native functions that use references are not supported as of now, put planned to.
- In PhpUnit, assertions for not enough calls has to be explicitly handled by calling `PhpFunctionSimpleMocker::phpUnitAssertNotEnoughCalls($this)` or `PhpFunctionSimpleMocker::assertPostConditions($this)`, if any better ideas are there please share.
- For any strange issues, the `@runInSeparateProcess` options of PhpUnit might help, though I did not encounter such cases yet, please report if any.

Credits
-------

[](#credits)

- [Abdulrahman Dimashki](https://github.com/idimsh)
- [All Contributors](../../contributors)
- An old [Symfony](https://github.com/symfony/symfony) class for mocking PHP Internal functions, could not find the source of it. But the code in `PhpFunctionSimpleMocker::register()` is taken from it.

Alternatives
------------

[](#alternatives)

There is a solution I havn't tested yet [php-mock](https://github.com/php-mock/php-mock)

License
-------

[](#license)

Released under MIT License - see the [License File](LICENSE) for details.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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

Unknown

Total

1

Last Release

2348d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6051f256445126a33260284644052c0eafa146bb2877bdd46522a280676e3ccd?d=identicon)[idimsh](/maintainers/idimsh)

---

Top Contributors

[![idimsh](https://avatars.githubusercontent.com/u/5890777?v=4)](https://github.com/idimsh "idimsh (13 commits)")

---

Tags

phpphp-internalsphpunitphpunit-testsphpidimshphp-internals-mocker

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/idimsh-php-internals-mocker/health.svg)

```
[![Health](https://phpackages.com/badges/idimsh-php-internals-mocker/health.svg)](https://phpackages.com/packages/idimsh-php-internals-mocker)
```

###  Alternatives

[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.1k](/packages/larastan-larastan)[dave-liddament/sarb

Provides tools for baselining static analysis results and comparing against that baseline

1651.4M](/packages/dave-liddament-sarb)[letsdrink/ouzo-goodies

Utility classes, test assertions and mocking framework extracted from Ouzo framework.

132617.9k7](/packages/letsdrink-ouzo-goodies)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)[doppiogancio/mocked-client

A simple way to mock a client

2174.9k3](/packages/doppiogancio-mocked-client)

PHPackages © 2026

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