Download the PHP package idimsh/php-internals-mocker without Composer
On this page you can find all versions of the php package idimsh/php-internals-mocker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download idimsh/php-internals-mocker
More information about idimsh/php-internals-mocker
Files in idimsh/php-internals-mocker
Package php-internals-mocker
Short Description Helps mocking PHP internal functions calls
License MIT
Homepage https://github.com/idimsh/php-internals-mocker
Informations about the package php-internals-mocker
PHP Internal function mocker
Util to allow mocking PHP Internal function calls in tests.
Installation
The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json
:
Usage
This mocker is intended to be used in Unit Tests, assume a class like this:
Has to be tested with unit tests for method openConnction()
. A PhpUnit test case would be like:
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:
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:
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
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
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)
orPhpFunctionSimpleMocker::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
- Abdulrahman Dimashki
- All Contributors
- An old 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
There is a solution I havn't tested yet php-mock
License
Released under MIT License - see the License File for details.