|
| 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 |
0 commit comments