You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHPUnit extension that uses runkit to mock PHP functions (both user-defined and system) and use mockobject-style invocation matchers, parameter constraints and all that magic.
1
+
2
+
## Introduction ##
3
+
4
+
MockFunction is a PHPUnit extension that uses `runkit` to mock PHP functions (both user-defined and system) and use mockobject-style invocation matchers, parameter constraints and all that magic.
5
+
6
+
To use this extension, you have to install `runkit` first (PECL package). For a working version see https://github.com/zenovich/runkit/
7
+
8
+
To be able to mock system function (not user-defined ones), you need to turn on `runkit.internal_override` in the PHP config.
9
+
10
+
## Usage ##
11
+
12
+
Assuming you are in a PHPUnit test.
13
+
14
+
// Back to the future:
15
+
$flux_capacitor = new PHPUnit_Extensions_MockFunction( 'time', $this->object );
Where `$flux_capacitor` is the stub function. It can be set up with the same fluent interface as a `MockObject` (excluding method, of course).
20
+
21
+
The 2nd parameter of the constructor (`$this->object`) is the object where we expect the function to be called. The "mocking" only takes effect from here, from all the other sources it will execute the "normal" function (see next line).
22
+
23
+
Variable `$einsteins_clock` contains the value that we will return instead of the "regular" value (we add 1 minute for the current time).
24
+
25
+
In the next line we set up the mock function with the fluent interface of a mock object.
26
+
27
+
The mocked function is active for the test object instance until `$flux_capacitor->restore();` is called. If you happen to forget this in the end of the test case, normally it is not a problem, because you will test anew instance of your tested class with each test case.
28
+
29
+
## Advanced mocking ##
30
+
31
+
You can use all invocation matchers, constraints and stub returns, for example:
32
+
33
+
// This will execute the original function at the end, but will test
34
+
// the number of exections ( $this->once() ) and the correct parameter ( $this->equalTo() ).
35
+
$mocked_strrev = new PHPUnit_Extensions_MockFunction( 'strrev', $this->object );
0 commit comments