Download the PHP package ernestmarcinko/mockutils without Composer
On this page you can find all versions of the php package ernestmarcinko/mockutils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ernestmarcinko/mockutils
More information about ernestmarcinko/mockutils
Files in ernestmarcinko/mockutils
Package mockutils
Short Description PHPUnit test mocking utility tools
License MIT
Homepage https://github.com/ernestmarcinko/mockutils
Informations about the package mockutils
Mock Utility tools for PHPUnit TestCase
This package provides ome utility functions for PHPUnit TestCase class (and descendants) via the MockUtils trait:
- Global Mocks -
setGlobalMocks()&unsetGlobalMocks()methods to set global function mocks within a namespace - Exception assertion - via
expectCatchException()- Method similar to expectException(), but without terminating the test execution
Installation & Inclusion
To install the package:
Including in your tests
There are two ways, one is using a trait to extend your TestCase functionality (recommended):
..or use the MockUtilsTestCase class instead of TestCase:
MockUtilsTestCase is only an empty class extending TestCase and using MockUtils.
Global Mocks
The trait adds two utility functions to setGlobalMocks() and unsetGlobalMocks() to set and unset global mocks.
setGlobalMocks
Sets global mocks to the given namespace.
Parameters
| Param | Type | Description | Required | Extra info |
|---|---|---|---|---|
$global_mocks |
array<string,mixed\|callable> |
Array of global function name and return value or callable | true | |
$namespace |
string |
The namespace of the code where the global function should be set | false | Make sure to use the code namespace, not the test code namespace |
Example
Say you have a service, which uses curl_exec to get a response from an API. During testing you want to mock it
and avoid actual connection to the API and instead test with pre-defined responses.
In the test we need the curl_exec to return 'response' for the mock, to do that:
It's also possible to define a callable instead of a static response:
unsetGlobalMocks
Unsets the given global mocks or all global mocks previously defined.
Parameters
| Param | Type | Description | Required | Extra info |
|---|---|---|---|---|
$global_mocks |
array<string> |
Array of global function names | false |
Examples
Exception Assertion
expectCatchException
Checks if the exception was thrown without execution termination.
Compared to PHPUnit core TestCase::expectException this function will not terminate the test execution.
The function to test must be however passed in as a closure, ex.: expectCatchException(fn()=>$o->myMethod(), ...)
Parameters
| Param | Type | Description | Required | Extra info |
|---|---|---|---|---|
$fn |
callable |
Function to test | true | Use closure fn()=>{$myObj->myMethod();} to pass in any method |
$throwable |
class-string<Throwable> |
Expected exception class name | true | |
$message |
string |
The expected error message | false | (optional) If set, then the function will also check the Exception message. |
Return values
The function is void, does not return anything, however:
expectCatchExceptionwill triggerTestCase::fail()if no exception was thrown, or any of the criteria was not met.