Skip to content

Commit 4d0e94f

Browse files
committed
Updated README
1 parent fceb839 commit 4d0e94f

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
This extension provides following features:
1111

12-
* `createMock()`, `getMockForAbstractClass()` and `getMockFromWsdl()` methods return an intersection type of the mock object and the mocked class so that both methods from the mock object (like `expects`) and from the mocked class are available on the object.
12+
* `createMock()`, `getMockForAbstractClass()` and `getMockFromWsdl()` methods return an intersection type (see the [detailed explanation of intersection types](https://medium.com/@ondrejmirtes/union-types-vs-intersection-types-fd44a8eacbb)) of the mock object and the mocked class so that both methods from the mock object (like `expects`) and from the mocked class are available on the object.
1313
* `getMock()` called on `MockBuilder` is also supported.
1414
* Interprets `Foo|PHPUnit_Framework_MockObject_MockObject` in phpDoc so that it results in an intersection type instead of a union type.
1515
* Defines early terminating method calls for the `PHPUnit\Framework\TestCase` class to prevent undefined variable errors.
@@ -18,6 +18,49 @@ It also contains this framework-specific rule (can be enabled separately):
1818

1919
* Check that both values passed to `assertSame()` method are of the same type.
2020

21+
## How to document mock objects in phpDocs?
22+
23+
If you need to configure the mock even after you assign it to a property or return it from a method, you should add `PHPUnit_Framework_MockObject_MockObject` to the phpDoc:
24+
25+
```php
26+
/**
27+
* @return Foo&PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private function createFooMock()
30+
{
31+
return $this->createMock(Foo::class);
32+
}
33+
34+
public function testSomething()
35+
{
36+
$fooMock = $this->createFooMock();
37+
$fooMock->method('doFoo')->will($this->returnValue('test'));
38+
$fooMock->doFoo();
39+
}
40+
```
41+
42+
Please note that the correct syntax for intersection types is `Foo&PHPUnit_Framework_MockObject_MockObject`. `Foo|PHPUnit_Framework_MockObject_MockObject` is also supported, but only for ecosystem and legacy reasons.
43+
44+
If the mock is fully configured and only the methods of the mocked class are supposed to be called on the value, it's fine to typehint only the mocked class:
45+
46+
```php
47+
/** @var Foo */
48+
private $foo;
49+
50+
protected function setUp()
51+
{
52+
$fooMock = $this->createMock(Foo::class);
53+
$fooMock->method('doFoo')->will($this->returnValue('test'));
54+
$this->foo = $foo;
55+
}
56+
57+
public function testSomething()
58+
{
59+
$this->foo->doFoo();
60+
// $this->foo->method() and expects() can no longer be called
61+
}
62+
```
63+
2164
## Usage
2265

2366
To use this extension, require it in [Composer](https://getcomposer.org/):

0 commit comments

Comments
 (0)