Download the PHP package xepozz/internal-mocker without Composer
On this page you can find all versions of the php package xepozz/internal-mocker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package internal-mocker
Introduction
The package helps mock internal php functions as simple as possible. Use this package when you need mock such
functions as: time()
, str_contains()
, rand
, etc.
Table of contents
- Installation
- Usage
- Register a PHPUnit Extension
- PHPUnit 9
- PHPUnit 10 and higher
- Register mocks
- Runtime mocks
- Pre-defined mock
- Mix of two previous ways
- State
- Tracking calls
- Global namespaced functions
- Internal functions
- Internal function implementation
- Restrictions
- Data Providers
Installation
Usage
The main idea is pretty simple: register a Listener for PHPUnit and call the Mocker extension first.
Register a PHPUnit Extension
PHPUnit 9
- Create new file
tests/MockerExtension.php
-
Paste the following code into the created file:
- Register the hook as extension in
phpunit.xml.dist
PHPUnit 10 and higher
- Create new file
tests/MockerExtension.php
-
Paste the following code into the created file:
- Register the hook as extension in
phpunit.xml.dist
Here you have registered extension that will be called every time when you run ./vendor/bin/phpunit
.
By default, all functions will be generated and saved into /vendor/bin/xepozz/internal-mocker/data/mocks.php
file.
Override the first argument of the Mocker
constructor to change the path:
Register mocks
The package supports a few ways to mock functions:
- Runtime mocks
- Pre-defined mocks
- Mix of two previous ways
Runtime mocks
If you want to make your test case to be used with mocked function you should register it before.
Back to the created MockerExtension::executeBeforeFirstTest
and edit the $mocks
variable.
This mock will proxy every call of time()
under the namespace App\Service
through a generated wrapper.
When you want to mock result in tests you should write the following code into needed test case:
You may also use a callback to set the result of the function:
So your test case will look like the following:
See full example
in \Xepozz\InternalMocker\Tests\Integration\DateTimeTest::testRun2
Pre-defined mock
Pre-defined mocks allow you to mock behaviour globally.
It means that you don't need to write MockerState::addCondition(...)
into each test case if you want to mock it for
whole project.
Keep in mind that the same functions from different namespaces are not the same for
Mocker
.
So back to the created MockerExtension::executeBeforeFirstTest
and edit the $mocks
variable.
After this variant each App\Service\time()
will return 150
.
You can add a lot of mocks. Mocker
compares the arguments
values with arguments of calling function and returns
needed result.
Mix of two previous ways
Mix means that you can use Pre-defined mock at first and Runtime mock after.
State
If you use Runtime mock
you may face the problem that after mocking function you still have it mocked in another test
cases.
MockerState::saveState()
and MockerState::resetState()
solves this problem.
These methods save "current" state and unload each Runtime mock
mock that was applied.
Using MockerState::saveState()
after Mocker->load($mocks)
saves only Pre-defined mocks.
Tracking calls
You may track calls of mocked functions by using MockerState::getTraces()
method.
$traces
will contain an array of arrays with the following structure:
Function signature stubs
All internal functions are stubbed to be compatible with the original ones.
It makes the functions use referenced arguments (&$file
) as the originals do.
They are located in the src/stubs.php
file.
If you need to add a new function signature, override the second argument of the Mocker
constructor:
Global namespaced functions
Internal functions
The way you can mock global functions is to disable them
in php.ini
: https://www.php.net/manual/en/ini.core.php#ini.disable-functions
The best way is to disable them only for tests by running a command with the additional flags:
If you are using PHPStorm you may set the command in the
Run/Debug Configurations
section. Add the flag-ddisable_functions=${functions}
to theInterpreter options
field.You may keep the command in the
composer.json
file under thescripts
section.Replace
${functions}
with the list of functions that you want to mock, separated by commas, e.g.:time,rand
.
So now you can mock global functions as well.
Internal function implementation
When you disable a function in php.ini
you cannot call it anymore. That means you must implement it by yourself.
Obviously, almost all functions are implemented in PHP looks the same as the Bash ones.
The shortest way to implement a function is to use syntax:
Keep in mind that leaving a global function without implementation will cause a recourse call of the function, that will lead to a fatal error.
Restrictions
Data Providers
Sometimes you may face unpleasant situation when mocked function is not mocking without forced using namespace
function
. It may mean that you are trying make PHP interpreter file in@dataProvider
. Be careful of it and as a workaround I may suggest you to call the mocker in test's constructor. So first move all code from your extension methodexecuteBeforeFirstTest
to new static method and call it in bothexecuteBeforeFirstTest
and__construct
methods.
That all because of PHPUnit 9.5 and lower event management system. Data Provider functionality starts to work before any events, so it's impossible to mock the function at the beginning of the runtime.