Download the PHP package lucatume/function-mocker-le without Composer
On this page you can find all versions of the php package lucatume/function-mocker-le. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lucatume/function-mocker-le
More information about lucatume/function-mocker-le
Files in lucatume/function-mocker-le
Package function-mocker-le
Short Description A lightweight function mocking solution.
License GPL-2.0
Informations about the package function-mocker-le
Function Mocker, Light Edition
When function-mocker is overkill.
Code example
Installation
Use Composer to require the library in the project
Why mocking functions? What problem does this solve?
The library provides a lightweight and dependency-free function mocking solution.
The reason one might want to mock a function in the tests is to test any code that depends on a framework based on functions (e.g. WordPress).
The difference between this library and function-mocker is that this will only work when none of the function it defines are defined during the execution; function-mocker will allow instead monkey-patching the functions at runtime even if those are already defined.
Where function mocker depends on the Patchwork library to allow for user-land monkey-patching this library has a minimal code footprint and only provides a handful of functions.
Patchwork or function-mocker should be used if really loading the mocked functions source cannot be avoided in the tests.
Usage
The library core function is the define($function, $callback)
one: it defines a function and sets its content to a callback; in its basic usage it allows setting up a mocked function return value in the tests:
The $callback
argument can be a callable
of any kind, in this example I'm using the Prophecy mocking engine to use set complex expectations:
Relying on basic assertions provided by hte PhpUnit library is another option:
Where function-mocker tries to provide a feature-reach solution to the problem of mocking functions (and more); this project tries to provide just a basic starting point with no opinionated choices about its usage.
The other functions provided by the libary are just sugar wrappers around the define
core; see the example and the /tests
folder for clarity.
The undefine($function)
and undefineAll()
methods will "undefine" functions managed by the Function Mocker LE library; in this context "undefine" means that calling the undefined function will trhow an tad\FunctionMockerLe\UndefinedFunctionException
.
Tests
To run the tests install the composer dependencies and run PHPUnit: