Skip to content

Commit 1af4bda

Browse files
authored
Rename PHPUnit 10 guide to PHPUnit Upgrade and add PHPUnit 11/12 sections (#8187)
* Rename PHPUnit 10 guide to PHPUnit Upgrade and add PHPUnit 11/12 sections Closes #8172 * Add current requirements section, import note, and upgrade checklist * Remove temporary analysis files accidentally committed
1 parent 66989ae commit 1af4bda

10 files changed

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

docs/en/appendices/phpunit10.md

Lines changed: 0 additions & 150 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)