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