Skip to content

Commit 5aed5da

Browse files
committed
Rename PHPUnit 10 guide to PHPUnit Upgrade and add PHPUnit 11/12 sections
Closes #8172
1 parent 0e83463 commit 5aed5da

10 files changed

Lines changed: 311 additions & 139 deletions

File tree

docs/en/appendices/5-0-migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The above will disable creation of entity objects and return rows as arrays inst
247247
- `TestSuite` was removed. Users should use environment variables to customize
248248
unit test settings instead.
249249
- `TestListenerTrait` was removed. PHPUnit dropped support for these listeners.
250-
See [PHPUnit 10 Upgrade](../appendices/phpunit10)
250+
See [PHPUnit Upgrade](../appendices/phpunit-upgrade)
251251
- `IntegrationTestTrait::configRequest()` now merges config when called multiple times
252252
instead of replacing the currently present config.
253253

docs/en/appendices/migration-guides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ to ensure the tool can resolve class names correctly.
3131
- [5 1 Migration Guide](5-1-migration-guide)
3232
- [5 2 Migration Guide](5-2-migration-guide)
3333
- [5 3 Migration Guide](5-3-migration-guide)
34-
- [Phpunit10](phpunit10)
34+
- [PHPUnit Upgrade](phpunit-upgrade)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# PHPUnit Upgrade
2+
3+
CakePHP 5 supports PHPUnit 11 and 12. This guide covers migrating from older PHPUnit versions.
4+
5+
## phpunit.xml Adjustments
6+
7+
It is recommended to let PHPUnit update its configuration file via the following command:
8+
9+
vendor/bin/phpunit --migrate-configuration
10+
11+
> [!NOTE]
12+
> Run `vendor/bin/phpunit --version` to check your current PHPUnit version before executing migration commands.
13+
14+
With this command out of the way your `phpunit.xml` already has most of the recommended changes present.
15+
16+
### Extension Configuration
17+
18+
PHPUnit 10 removed the old hook system and introduced a new [Event system](https://docs.phpunit.de/en/10.5/extending-phpunit.html#extending-the-test-runner)
19+
which requires the following code in your `phpunit.xml` to be adjusted from:
20+
21+
``` xml
22+
<extensions>
23+
<extension class="Cake\TestSuite\Fixture\PHPUnitExtension"/>
24+
</extensions>
25+
```
26+
27+
to:
28+
29+
``` xml
30+
<extensions>
31+
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
32+
</extensions>
33+
```
34+
35+
## PHPUnit 9 to 10
36+
37+
### `->withConsecutive()` Removed
38+
39+
You can convert the removed `->withConsecutive()` method to a
40+
working interim solution like you can see here:
41+
42+
``` php
43+
->withConsecutive(['firstCallArg'], ['secondCallArg'])
44+
```
45+
46+
should be converted to:
47+
48+
``` php
49+
->with(
50+
...self::withConsecutive(['firstCallArg'], ['secondCallArg'])
51+
)
52+
```
53+
54+
The static `self::withConsecutive()` method has been added via the `Cake\TestSuite\PHPUnitConsecutiveTrait`
55+
to the base `Cake\TestSuite\TestCase` class so you don't have to manually add that trait to your TestCase classes.
56+
57+
### Data Providers Must Be Static
58+
59+
If your test cases leverage the data provider feature of PHPUnit then
60+
you have to adjust your data providers to be static:
61+
62+
``` php
63+
public function myProvider(): array
64+
```
65+
66+
should be converted to:
67+
68+
``` php
69+
public static function myProvider(): array
70+
```
71+
72+
## PHPUnit 10 to 11
73+
74+
PHPUnit 11 requires PHP 8.2 or later.
75+
76+
### Annotations Deprecated
77+
78+
PHPUnit 11 deprecates annotations in docblocks. You should migrate to PHP 8 attributes:
79+
80+
``` php
81+
// Before (deprecated)
82+
/**
83+
* @dataProvider myProvider
84+
*/
85+
public function testSomething(): void
86+
87+
// After
88+
#[DataProvider('myProvider')]
89+
public function testSomething(): void
90+
```
91+
92+
Common attribute replacements:
93+
94+
| Annotation | Attribute |
95+
|------------|-----------|
96+
| `@dataProvider` | `#[DataProvider('methodName')]` |
97+
| `@depends` | `#[Depends('methodName')]` |
98+
| `@group` | `#[Group('name')]` |
99+
| `@covers` | `#[CoversClass(ClassName::class)]` |
100+
| `@test` | `#[Test]` |
101+
102+
### Test Doubles for Abstract Classes Deprecated
103+
104+
Methods for creating mock objects for abstract classes and traits are hard-deprecated. Testing traits in isolation from the classes that use them is discouraged.
105+
106+
### Stub Expectations Deprecated
107+
108+
Configuring expectations on an object created with `createStub()` triggers a deprecation warning:
109+
110+
``` php
111+
// Avoid - will warn in PHPUnit 11
112+
$stub = $this->createStub(SomeClass::class);
113+
$stub->expects($this->once())->method('foo');
114+
115+
// Use createMock() instead when you need expectations
116+
$mock = $this->createMock(SomeClass::class);
117+
$mock->expects($this->once())->method('foo');
118+
```
119+
120+
### Test Class Naming
121+
122+
Test class names must match their file names. A test in `FooTest.php` must have a class named `FooTest`.
123+
124+
## PHPUnit 11 to 12
125+
126+
PHPUnit 12 requires PHP 8.3 or later.
127+
128+
### Annotations Removed
129+
130+
Support for docblock annotations has been removed. All tests must use PHP 8 attributes.
131+
132+
### Test Doubles for Abstract Classes Removed
133+
134+
Methods for creating mock objects for abstract classes and traits have been removed entirely.
135+
136+
### Stub Expectations Removed
137+
138+
Configuring expectations on objects created with `createStub()` no longer works. Use `createMock()` for test doubles that need expectation configuration.
139+
140+
## Using Rector for Automated Migration
141+
142+
[Rector](https://getrector.com/) can automate many of these changes:
143+
144+
``` bash
145+
composer require --dev rector/rector rector/rector-phpunit
146+
147+
# Create rector.php config
148+
vendor/bin/rector init
149+
150+
# Run rector
151+
vendor/bin/rector process tests/
152+
```
153+
154+
Configure Rector with PHPUnit rulesets to handle data provider static conversion, annotation to attribute migration, and other changes automatically.

docs/en/appendices/phpunit10.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

docs/ja/appendices/5-0-migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ CakePHP 5.0 には、破壊的な変更が含まれており、4.x リリース
182182
### TestSuite
183183

184184
- `TestSuite` は削除されました。単体テストの設定をカスタマイズするには、環境変数を使って下さい。
185-
- `TestListenerTrait` は削除されました。PHPUnitがこれらの listener のサポートを打ち切ったためです。詳細は [PHPUnit 10 へのアップグレード](../appendices/phpunit10) を参照して下さい。
185+
- `TestListenerTrait` は削除されました。PHPUnitがこれらの listener のサポートを打ち切ったためです。詳細は [PHPUnit アップグレード](../appendices/phpunit-upgrade) を参照して下さい。
186186
- `IntegrationTestTrait::configRequest()` が複数回呼ばれた際、設定を上書きするのではなく merge するようになりました。
187187

188188
### Validation

docs/ja/appendices/migration-guides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
- [5 0 Migration Guide](5-0-migration-guide)
77
- [5 1 Migration Guide](5-1-migration-guide)
88
- [5 2 Migration Guide](5-2-migration-guide)
9-
- [Phpunit10](phpunit10)
9+
- [PHPUnit アップグレード](phpunit-upgrade)

0 commit comments

Comments
 (0)