Skip to content

Commit 888bd44

Browse files
committed
Edited readme, adding examples
1 parent 3974031 commit 888bd44

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

README

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
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 );
16+
$einsteins_clock = time() + 60;
17+
$flux_capacitor->expects( $this->atLeastOnce() )->will( $this->returnValue( $einsteins_clock ) );
18+
19+
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 );
36+
$mocked_strrev->expects( $this->once() )->with( $this->equalTo( 'abc' ) )->will( $this->returnCallback( 'strrev' ) );
37+
38+
39+
// This object cannot execute shell_exec.
40+
$mocked_shell = new PHPUnit_Extensions_MockFunction( 'shell_exec', $this->object );
41+
$mocked_shell->expects( $this->never() );
42+
43+
44+
// Expecting to check the existence of 2 file, returning true for both.
45+
$mocked_file_exists = new PHPUnit_Extensions_MockFunction( 'file_exists', $this->object );
46+
$mocked_file_exists->expects( $this->exactly( 2 ) )
47+
->with(
48+
$this->logicalOr(
49+
$this->equalTo( '/tmp/file1.exe' ),
50+
$this->equalTo( '/tmp/file2.exe' )
51+
)
52+
)->will( $this->returnValue( true ) );
53+
54+
For further information see http://www.phpunit.de/manual/3.0/en/mock-objects.html

0 commit comments

Comments
 (0)