Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ec3fa9

Browse files
derrabusondrejmirtes
authored andcommitted
Modernize code examples in README.md
1 parent b1df1f5 commit 9ec3fa9

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

‎README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This extension provides following features:
1111

1212
* `createMock()`, `getMockForAbstractClass()` and `getMockFromWsdl()` methods return an intersection type (see the [detailed explanation of intersection types](https://phpstan.org/blog/union-types-vs-intersection-types)) 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.
14-
* Interprets `Foo|PHPUnit_Framework_MockObject_MockObject` in phpDoc so that it results in an intersection type instead of a union type.
14+
* Interprets `Foo|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.
1616
* Specifies types of expressions passed to various `assert` methods like `assertInstanceOf`, `assertTrue`, `assertInternalType` etc.
1717
* Combined with PHPStan's level 4, it points out always-true and always-false asserts like `assertTrue(true)` etc.
@@ -27,41 +27,49 @@ It also contains this strict framework-specific rules (can be enabled separately
2727

2828
## How to document mock objects in phpDocs?
2929

30-
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:
30+
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 type:
3131

3232
```php
33-
/**
34-
* @return Foo&PHPUnit_Framework_MockObject_MockObject
35-
*/
36-
private function createFooMock()
33+
private function createFooMock(): Foo&\PHPUnit\Framework\MockObject\MockObject
3734
{
3835
return $this->createMock(Foo::class);
3936
}
4037

41-
public function testSomething()
38+
public function testSomething(): void
4239
{
4340
$fooMock = $this->createFooMock();
4441
$fooMock->method('doFoo')->will($this->returnValue('test'));
4542
$fooMock->doFoo();
4643
}
4744
```
4845

49-
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.
46+
If you cannot use native intersection types yet, you can use PHPDoc instead.
47+
48+
```php
49+
/**
50+
* @return Foo&\PHPUnit\Framework\MockObject\MockObject
51+
*/
52+
private function createFooMock(): Foo
53+
{
54+
return $this->createMock(Foo::class);
55+
}
56+
```
57+
58+
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.
5059

5160
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:
5261

5362
```php
54-
/** @var Foo */
55-
private $foo;
63+
private Foo $foo;
5664

57-
protected function setUp()
65+
protected function setUp(): void
5866
{
5967
$fooMock = $this->createMock(Foo::class);
6068
$fooMock->method('doFoo')->will($this->returnValue('test'));
6169
$this->foo = $fooMock;
6270
}
6371

64-
public function testSomething()
72+
public function testSomething(): void
6573
{
6674
$this->foo->doFoo();
6775
// $this->foo->method() and expects() can no longer be called

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /