{"id":26492734,"url":"https://github.com/idimsh/php-internals-mocker","last_synced_at":"2026-05-10T17:05:59.154Z","repository":{"id":56989067,"uuid":"225243741","full_name":"idimsh/php-internals-mocker","owner":"idimsh","description":"A Utility to allow mocking PHP Internal function calls in UnitTests.","archived":false,"fork":false,"pushed_at":"2019-12-26T18:36:06.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-17T12:48:22.245Z","etag":null,"topics":["php","php-internals","phpunit","phpunit-tests"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idimsh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-01T22:59:51.000Z","updated_at":"2019-12-26T18:36:08.000Z","dependencies_parsed_at":"2022-08-21T12:20:37.720Z","dependency_job_id":null,"html_url":"https://github.com/idimsh/php-internals-mocker","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idimsh%2Fphp-internals-mocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idimsh%2Fphp-internals-mocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idimsh%2Fphp-internals-mocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idimsh%2Fphp-internals-mocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idimsh","download_url":"https://codeload.github.com/idimsh/php-internals-mocker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244584824,"owners_count":20476619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","php-internals","phpunit","phpunit-tests"],"created_at":"2025-03-20T09:26:58.194Z","updated_at":"2026-05-10T17:05:59.118Z","avatar_url":"https://github.com/idimsh.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Internal function mocker\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status][ico-travis]][link-travis]\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![PHP Version][ico-phpversion]][link-packagist]\n\n\n\n\nUtil to allow mocking PHP Internal function calls in tests.\n\n\n## Installation\n\nThe 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`:\n\n```bash\ncomposer require-dev idimsh/php-internals-mocker\n```\n\n## Usage\n\nThis mocker is intended to be used in Unit Tests, assume a class like this:\n\n``` php\nnamespace Vendor\\Namespace\n\nclass MyClass \n{\n    public function openConnction($hostname) \n    {\n        return fsockopen($hostname);\n    }\n}\n```\n\nHas to be tested with unit tests for method `openConnction()`. A PhpUnit test case would be like:\n\n``` php\nnamespace VendorTest\\Namespace\n\nclass MyClassTest extends \\PHPUnit\\Framework\\TestCase\n{\n    public function testOpenConnction(): void\n    {\n        $object   = new \\Vendor\\Namespace\\MyClass;\n        $hostname = \\uniqid('hostname');\n        $actual   = $object-\u003eopenConnection($hostname);\n        // ...\n    }\n}\n```\n\nWe 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:\n``` php\nnamespace VendorTest\\Namespace\n\nuse idimsh\\PhpInternalsMocker\\PhpFunctionSimpleMocker;\n\nclass MyClassTest extends \\PHPUnit\\Framework\\TestCase\n{\n    protected function setUp(): void\n    {\n        parent::setUp();\n        PhpFunctionSimpleMocker::reset();\n    }\n    \n    public function testOpenConnction(): void\n    {\n        $hostname = \\uniqid('hostname');\n        $return   = \\uniqid('some mock for the return of fsockopen()');\n        \n        PhpFunctionSimpleMocker::add(\n            'fsockopen',\n            \\Vendor\\Namespace\\MyClass::class,\n            function ($inputHostname) use ($hostname, $return) {\n                static::assertSame($inputHostname, $hostname);\n                return $return;\n            }\n        );\n        \n        $object = new \\Vendor\\Namespace\\MyClass;\n        $actual = $object-\u003eopenConnection($hostname);\n        static::assertSame($return, $actual);\n\n        /** @noinspection PhpUnhandledExceptionInspection */\n        PhpFunctionSimpleMocker::phpUnitAssertNotEnoughCalls($this);\n    }\n}\n```\n\n## Methods Manual\n\n1- `PhpFunctionSimpleMocker::reset()`: should be called in PhpUnit TestCase `setUp()` method or at the beginning of a test method.  \n\n2- `PhpFunctionSimpleMocker::add()`: to be called after `reset()` to register the callbacks expected to native functions, signature:  \n``` php\n    /**\n     * Register a call back to be called for the PHP internal function which is to be used in the class passed.\n     *\n     * If the $callback is null, then this PHP function is not expected to be called.\n     *\n     * Assertions can be done inside the callback.\n     *\n     * @param string        $internalFunctionName The PHP function name to mock\n     * @param string        $beingCalledFromClass The class FQN which calls $internalFunctionName\n     * @param callable|null $callback\n     * @param int           $numberOfCalls        To mock more than once for the same callback, pass the number here\n     */\n    public static function add(\n        string $internalFunctionName,\n        string $beingCalledFromClass,\n        ?callable $callback,\n        int $numberOfCalls = 1\n    ): void\n```\n\nIt can be called multiple times with the same `$internalFunctionName` and different `$callback` for each call in the order expected.  \nThe `$beingCalledFromClass` expects a class FQN which from the namespace will be extracted and the function will be registered at that namespace.  \n\n3- `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.   \n\n4- `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.     \n\n## Usage Conditions\n\nThe native PHP function call that is to be mocked and replaced with a callback needs to be (All must apply):\n- 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.\n- The call that PHP native function must not be preceeded by the global namespace resolution operator '\\\\' \n- The `use function` statement is not used to import that native function into the namespace in the class.\n\n\n## Limitations\nQuickly:\n- PHP native functions that use references are not supported as of now, put planned to.\n- 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.      \n- For any strange issues, the `@runInSeparateProcess` options of PhpUnit might help, though I did not encounter such cases yet, please report if any.    \n\n## Credits\n\n- [Abdulrahman Dimashki][link-author]\n- [All Contributors][link-contributors]\n- 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.\n\n## Alternatives\nThere is a solution I havn't tested yet [php-mock](https://github.com/php-mock/php-mock)\n\n\n## License\n\nReleased under MIT License - see the [License File](LICENSE) for details.\n\n\n[ico-version]: https://img.shields.io/packagist/v/idimsh/php-internals-mocker.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/idimsh/php-internals-mocker/master.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/idimsh/php-internals-mocker.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/idimsh/php-internals-mocker.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/idimsh/php-internals-mocker.svg?style=flat-square\n[ico-phpversion]: https://img.shields.io/packagist/php-v/idimsh/php-internals-mocker?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/idimsh/php-internals-mocker\n[link-travis]: https://travis-ci.org/idimsh/php-internals-mocker\n[link-scrutinizer]: https://scrutinizer-ci.com/g/idimsh/php-internals-mocker/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/idimsh/php-internals-mocker\n[link-downloads]: https://packagist.org/packages/idimsh/php-internals-mocker\n[link-author]: https://github.com/idimsh\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidimsh%2Fphp-internals-mocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidimsh%2Fphp-internals-mocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidimsh%2Fphp-internals-mocker/lists"}