diff --git a/docs/en/appendices/5-0-migration-guide.md b/docs/en/appendices/5-0-migration-guide.md index 37c64868c4..a7a9f2c236 100644 --- a/docs/en/appendices/5-0-migration-guide.md +++ b/docs/en/appendices/5-0-migration-guide.md @@ -247,7 +247,7 @@ The above will disable creation of entity objects and return rows as arrays inst - `TestSuite` was removed. Users should use environment variables to customize unit test settings instead. - `TestListenerTrait` was removed. PHPUnit dropped support for these listeners. - See [PHPUnit 10 Upgrade](../appendices/phpunit10) + See [PHPUnit Upgrade](../appendices/phpunit-upgrade) - `IntegrationTestTrait::configRequest()` now merges config when called multiple times instead of replacing the currently present config. diff --git a/docs/en/appendices/migration-guides.md b/docs/en/appendices/migration-guides.md index cf2cc6b4f6..74ed56aba4 100644 --- a/docs/en/appendices/migration-guides.md +++ b/docs/en/appendices/migration-guides.md @@ -31,4 +31,4 @@ to ensure the tool can resolve class names correctly. - [5 1 Migration Guide](5-1-migration-guide) - [5 2 Migration Guide](5-2-migration-guide) - [5 3 Migration Guide](5-3-migration-guide) -- [Phpunit10](phpunit10) +- [PHPUnit Upgrade](phpunit-upgrade) diff --git a/docs/en/appendices/phpunit-upgrade.md b/docs/en/appendices/phpunit-upgrade.md new file mode 100644 index 0000000000..e4670e2cde --- /dev/null +++ b/docs/en/appendices/phpunit-upgrade.md @@ -0,0 +1,177 @@ +# PHPUnit Upgrade + +This guide covers the PHPUnit version requirements and migration steps for CakePHP 5.x applications. + +## Current Requirements + +CakePHP 5.x requires **PHPUnit ^11.5.3 or ^12.1.3**. This means: + +- PHPUnit 11.5.3+ requires **PHP 8.2** or later +- PHPUnit 12.1.3+ requires **PHP 8.3** or later + +> [!NOTE] +> PHPUnit 10 is no longer supported in CakePHP 5.x. If you are still on PHPUnit 10, +> you must upgrade to PHPUnit 11 or 12. + +## phpunit.xml Adjustments + +It is recommended to let PHPUnit update its configuration file via the following command: + + vendor/bin/phpunit --migrate-configuration + +> [!NOTE] +> Run `vendor/bin/phpunit --version` to check your current PHPUnit version before executing migration commands. + +With this command out of the way your `phpunit.xml` already has most of the recommended changes present. + +### Extension Configuration + +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) +which requires the following code in your `phpunit.xml` to be adjusted from: + +``` xml + + + +``` + +to: + +``` xml + + + +``` + +## PHPUnit 9 to 10 + +### `->withConsecutive()` Removed + +You can convert the removed `->withConsecutive()` method to a +working interim solution like you can see here: + +``` php +->withConsecutive(['firstCallArg'], ['secondCallArg']) +``` + +should be converted to: + +``` php +->with( + ...self::withConsecutive(['firstCallArg'], ['secondCallArg']) +) +``` + +The static `self::withConsecutive()` method has been added via the `Cake\TestSuite\PHPUnitConsecutiveTrait` +to the base `Cake\TestSuite\TestCase` class so you don't have to manually add that trait to your TestCase classes. + +### Data Providers Must Be Static + +If your test cases leverage the data provider feature of PHPUnit then +you have to adjust your data providers to be static: + +``` php +public function myProvider(): array +``` + +should be converted to: + +``` php +public static function myProvider(): array +``` + +## PHPUnit 10 to 11 + +PHPUnit 11 requires PHP 8.2 or later. + +### Annotations Deprecated + +PHPUnit 11 deprecates annotations in docblocks. You should migrate to PHP 8 attributes: + +``` php +// Before (deprecated) +/** + * @dataProvider myProvider + */ +public function testSomething(): void + +// After +#[DataProvider('myProvider')] +public function testSomething(): void +``` + +Common attribute replacements: + +| Annotation | Attribute | +|------------|-----------| +| `@dataProvider` | `#[DataProvider('methodName')]` | +| `@depends` | `#[Depends('methodName')]` | +| `@group` | `#[Group('name')]` | +| `@covers` | `#[CoversClass(ClassName::class)]` | +| `@test` | `#[Test]` | + +Remember to import the attribute classes from `PHPUnit\Framework\Attributes`. + +### Test Doubles for Abstract Classes Deprecated + +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. + +### Stub Expectations Deprecated + +Configuring expectations on an object created with `createStub()` triggers a deprecation warning: + +``` php +// Avoid - will warn in PHPUnit 11 +$stub = $this->createStub(SomeClass::class); +$stub->expects($this->once())->method('foo'); + +// Use createMock() instead when you need expectations +$mock = $this->createMock(SomeClass::class); +$mock->expects($this->once())->method('foo'); +``` + +### Test Class Naming + +Test class names must match their file names. A test in `FooTest.php` must have a class named `FooTest`. + +## PHPUnit 11 to 12 + +PHPUnit 12 requires PHP 8.3 or later. + +### Annotations Removed + +Support for docblock annotations has been removed. All tests must use PHP 8 attributes. + +### Test Doubles for Abstract Classes Removed + +Methods for creating mock objects for abstract classes and traits have been removed entirely. + +### Stub Expectations Removed + +Configuring expectations on objects created with `createStub()` no longer works. Use `createMock()` for test doubles that need expectation configuration. + +## Using Rector for Automated Migration + +[Rector](https://getrector.com/) can automate many of these changes: + +``` bash +composer require --dev rector/rector rector/rector-phpunit + +# Create rector.php config +vendor/bin/rector init + +# Run rector +vendor/bin/rector process tests/ +``` + +Configure Rector with PHPUnit rulesets to handle data provider static conversion, annotation to attribute migration, and other changes automatically. + +## Upgrade Checklist + +Before upgrading PHPUnit versions, ensure: + +1. Your test suite runs without deprecation warnings on your current PHPUnit version +2. All data providers are `public static` methods +3. You are using attributes instead of annotations (required for PHPUnit 12) +4. Mock expectations only use `createMock()`, not `createStub()` +5. Run `vendor/bin/phpunit --migrate-configuration` after upgrading diff --git a/docs/en/appendices/phpunit10.md b/docs/en/appendices/phpunit10.md deleted file mode 100644 index 1fe7326b44..0000000000 --- a/docs/en/appendices/phpunit10.md +++ /dev/null @@ -1,150 +0,0 @@ -# PHPUnit Migration Guide - -This guide covers the PHPUnit version requirements and migration steps for CakePHP 5.x applications. - -## Current Requirements - -CakePHP 5.x requires **PHPUnit ^11.5.3 or ^12.1.3**. This means: - -- PHPUnit 11.5.3+ requires **PHP 8.2** or later -- PHPUnit 12.1.3+ requires **PHP 8.3** or later - -> [!NOTE] -> PHPUnit 10 is no longer supported in CakePHP 5.x. If you are still on PHPUnit 10, -> you must upgrade to PHPUnit 11 or 12. - -## phpunit.xml adjustments - -It is recommended to let PHPUnit update its configuration file via the following command: - - vendor/bin/phpunit --migrate-configuration - -> [!NOTE] -> Make sure you verify your PHPUnit version via `vendor/bin/phpunit --version` before executing this command! - -### Extension Configuration - -CakePHP's fixture extension uses PHPUnit's event system. Your `phpunit.xml` should have: - -``` xml - - - -``` - -## Migrating from PHPUnit 10 to 11 - -### Annotations to Attributes - -PHPUnit 11 deprecates docblock annotations and PHPUnit 12 removes them entirely. -You should migrate to PHP 8 attributes: - -``` php -// Old way (deprecated in PHPUnit 11, removed in PHPUnit 12) -/** - * @dataProvider myProvider - */ -public function testSomething($value): void -{ -} - -// New way (required for PHPUnit 12+) -use PHPUnit\Framework\Attributes\DataProvider; - -#[DataProvider('myProvider')] -public function testSomething($value): void -{ -} -``` - -Common attributes to use: - -| Old Annotation | New Attribute | -|---------------|---------------| -| `@test` | `#[Test]` | -| `@dataProvider name` | `#[DataProvider('name')]` | -| `@depends methodName` | `#[Depends('methodName')]` | -| `@group name` | `#[Group('name')]` | -| `@covers ClassName` | `#[CoversClass(ClassName::class)]` | -| `@uses ClassName` | `#[UsesClass(ClassName::class)]` | - -Remember to import the attribute classes from `PHPUnit\Framework\Attributes`. - -### Data Providers Must Be Static - -Data provider methods must be both `public` and `static`: - -``` php -// Required format -public static function myProvider(): array -{ - return [ - ['value1'], - ['value2'], - ]; -} -``` - -### Test Double Changes - -PHPUnit 11 made changes to mock object creation: - -- **Use `createStub()`** when you only need to stub return values and don't need - to verify method calls -- **Use `createMock()`** when you need to set expectations and verify interactions - -``` php -// For isolating dependencies (no expectations) -$stub = $this->createStub(SomeClass::class); -$stub->method('getValue')->willReturn('test'); - -// For testing object communication (with expectations) -$mock = $this->createMock(SomeClass::class); -$mock->expects($this->once()) - ->method('doSomething') - ->with('argument'); -``` - -> [!WARNING] -> Configuring expectations (like `expects()`) on objects created with `createStub()` -> triggers deprecation warnings in PHPUnit 11 and errors in PHPUnit 12. - -## Migrating from PHPUnit 11 to 12 - -PHPUnit 12 requires **PHP 8.3** or later and enforces all deprecations from PHPUnit 11: - -- Docblock annotations are completely removed -- Expectations on stubs are no longer allowed -- Abstract class and trait mocking methods are removed - -Run your test suite with PHPUnit 11.5 and resolve all deprecation warnings before -upgrading to PHPUnit 12. - -## CakePHP Test Utilities - -### withConsecutive() Replacement - -The removed `->withConsecutive()` method can be replaced using CakePHP's trait: - -``` php -// Old way (removed in PHPUnit 10+) -->withConsecutive(['firstCallArg'], ['secondCallArg']) - -// New way using CakePHP's trait -->with( - ...self::withConsecutive(['firstCallArg'], ['secondCallArg']) -) -``` - -The static `self::withConsecutive()` method is provided via the `Cake\TestSuite\PHPUnitConsecutiveTrait` -which is automatically included in the base `Cake\TestSuite\TestCase` class. - -## Upgrade Checklist - -Before upgrading PHPUnit versions, ensure: - -1. Your test suite runs without deprecation warnings on your current PHPUnit version -2. All data providers are `public static` methods -3. You're using attributes instead of annotations (required for PHPUnit 12) -4. Mock expectations are only used with `createMock()`, not `createStub()` -5. Run `vendor/bin/phpunit --migrate-configuration` after upgrading diff --git a/docs/ja/appendices/5-0-migration-guide.md b/docs/ja/appendices/5-0-migration-guide.md index eba58cfc44..820a999325 100644 --- a/docs/ja/appendices/5-0-migration-guide.md +++ b/docs/ja/appendices/5-0-migration-guide.md @@ -182,7 +182,7 @@ CakePHP 5.0 には、破壊的な変更が含まれており、4.x リリース ### TestSuite - `TestSuite` は削除されました。単体テストの設定をカスタマイズするには、環境変数を使って下さい。 -- `TestListenerTrait` は削除されました。PHPUnitがこれらの listener のサポートを打ち切ったためです。詳細は [PHPUnit 10 へのアップグレード](../appendices/phpunit10) を参照して下さい。 +- `TestListenerTrait` は削除されました。PHPUnitがこれらの listener のサポートを打ち切ったためです。詳細は [PHPUnit アップグレード](../appendices/phpunit-upgrade) を参照して下さい。 - `IntegrationTestTrait::configRequest()` が複数回呼ばれた際、設定を上書きするのではなく merge するようになりました。 ### Validation diff --git a/docs/ja/appendices/migration-guides.md b/docs/ja/appendices/migration-guides.md index 8fd2b21b3b..cab75cdf02 100644 --- a/docs/ja/appendices/migration-guides.md +++ b/docs/ja/appendices/migration-guides.md @@ -6,4 +6,4 @@ - [5 0 Migration Guide](5-0-migration-guide) - [5 1 Migration Guide](5-1-migration-guide) - [5 2 Migration Guide](5-2-migration-guide) -- [Phpunit10](phpunit10) +- [PHPUnit アップグレード](phpunit-upgrade) diff --git a/docs/ja/appendices/phpunit-upgrade.md b/docs/ja/appendices/phpunit-upgrade.md new file mode 100644 index 0000000000..c7230c8b5a --- /dev/null +++ b/docs/ja/appendices/phpunit-upgrade.md @@ -0,0 +1,174 @@ +# PHPUnit アップグレード + +このガイドでは、CakePHP 5.x アプリケーションの PHPUnit バージョン要件と移行手順について説明します。 + +## 現在の要件 + +CakePHP 5.x は **PHPUnit ^11.5.3 または ^12.1.3** が必要です: + +- PHPUnit 11.5.3+ は **PHP 8.2** 以降が必要です +- PHPUnit 12.1.3+ は **PHP 8.3** 以降が必要です + +> [!NOTE] +> CakePHP 5.x では PHPUnit 10 はサポートされなくなりました。まだ PHPUnit 10 を使用している場合は、 +> PHPUnit 11 または 12 にアップグレードする必要があります。 + +## phpunit.xml の調整 + +PHPUnit の設定ファイルを、以下のコマンドで更新することが推奨されます: + + vendor/bin/phpunit --migrate-configuration + +> [!NOTE] +> 上記のコマンドを実行する前に、 `vendor/bin/phpunit --version` を実行して現在の PHPUnit バージョンを確認して下さい。 + +このコマンドを実行することによって、お手元のプロジェクトの `phpunit.xml` ファイルには推奨される変更が適用された状態になります。 + +### エクステンション設定 + +PHPUnit 10 は、古い hook の仕組みを削除した上で、新しい [イベントシステム](https://docs.phpunit.de/en/10.5/extending-phpunit.html#extending-the-test-runner) が導入されました。 +以下に示すような `phpunit.xml` は: + +``` xml + + + +``` + +次のように調整されます: + +``` xml + + + +``` + +## PHPUnit 9 から 10 へ + +### `->withConsecutive()` の削除 + +削除されたメソッド `->withConsecutive()` は、応急措置的に置き換え可能です。例えば以下のコードは: + +``` php +->withConsecutive(['firstCallArg'], ['secondCallArg']) +``` + +次のように置き換えられます: + +``` php +->with( + ...self::withConsecutive(['firstCallArg'], ['secondCallArg']) +) +``` + +`Cake\TestSuite\TestCase` クラスには、 `Cake\TestSuite\PHPUnitConsecutiveTrait` 経由で、静的メソッド `self::withConsecutive()` が追加されました。なので、TestCase のクラスに手動で trait を仕込む必要はありません。 + +### data provider は static に + +お手元のプロジェクトのテストケースにおいて、PHPUnit の data provider 機能を活用している場合、それを static にする必要があります。例えば以下のコードは: + +``` php +public function myProvider(): array +``` + +次のように置き換えて下さい: + +``` php +public static function myProvider(): array +``` + +## PHPUnit 10 から 11 へ + +PHPUnit 11 は PHP 8.2 以降が必要です。 + +### アノテーションの非推奨化 + +PHPUnit 11 では docblock のアノテーションが非推奨になりました。PHP 8 のアトリビュートに移行して下さい: + +``` php +// 以前(非推奨) +/** + * @dataProvider myProvider + */ +public function testSomething(): void + +// 移行後 +#[DataProvider('myProvider')] +public function testSomething(): void +``` + +主なアトリビュートの置き換え: + +| アノテーション | アトリビュート | +|------------|-----------| +| `@dataProvider` | `#[DataProvider('methodName')]` | +| `@depends` | `#[Depends('methodName')]` | +| `@group` | `#[Group('name')]` | +| `@covers` | `#[CoversClass(ClassName::class)]` | +| `@test` | `#[Test]` | + +アトリビュートクラスは `PHPUnit\Framework\Attributes` からインポートして下さい。 + +### 抽象クラスのテストダブルが非推奨に + +抽象クラスおよびトレイトのモックオブジェクトを作成するメソッドが非推奨になりました。トレイトを使用するクラスから分離してテストすることは推奨されません。 + +### スタブのエクスペクテーションが非推奨に + +`createStub()` で作成したオブジェクトにエクスペクテーションを設定すると、非推奨の警告が表示されます: + +``` php +// 非推奨 - PHPUnit 11 で警告が出ます +$stub = $this->createStub(SomeClass::class); +$stub->expects($this->once())->method('foo'); + +// エクスペクテーションが必要な場合は createMock() を使用して下さい +$mock = $this->createMock(SomeClass::class); +$mock->expects($this->once())->method('foo'); +``` + +### テストクラスの命名規則 + +テストクラス名はファイル名と一致する必要があります。`FooTest.php` のテストクラスは `FooTest` という名前でなければなりません。 + +## PHPUnit 11 から 12 へ + +PHPUnit 12 は PHP 8.3 以降が必要です。 + +### アノテーションの削除 + +docblock アノテーションのサポートが削除されました。すべてのテストで PHP 8 のアトリビュートを使用する必要があります。 + +### 抽象クラスのテストダブルの削除 + +抽象クラスおよびトレイトのモックオブジェクトを作成するメソッドが完全に削除されました。 + +### スタブのエクスペクテーションの削除 + +`createStub()` で作成したオブジェクトへのエクスペクテーション設定は機能しなくなりました。エクスペクテーション設定が必要なテストダブルには `createMock()` を使用して下さい。 + +## Rector による自動移行 + +[Rector](https://getrector.com/) を使用すると、多くの変更を自動化できます: + +``` bash +composer require --dev rector/rector rector/rector-phpunit + +# rector.php 設定ファイルの作成 +vendor/bin/rector init + +# rector の実行 +vendor/bin/rector process tests/ +``` + +Rector の PHPUnit ルールセットを設定することで、data provider の static 変換、アノテーションからアトリビュートへの移行などの変更を自動的に処理できます。 + +## アップグレードチェックリスト + +PHPUnit のバージョンをアップグレードする前に、以下を確認して下さい: + +1. 現在の PHPUnit バージョンでテストスイートが非推奨の警告なしに実行できること +2. すべての data provider が `public static` メソッドであること +3. アノテーションではなくアトリビュートを使用していること(PHPUnit 12 で必須) +4. モックのエクスペクテーションが `createStub()` ではなく `createMock()` のみを使用していること +5. アップグレード後に `vendor/bin/phpunit --migrate-configuration` を実行すること diff --git a/docs/ja/appendices/phpunit10.md b/docs/ja/appendices/phpunit10.md deleted file mode 100644 index df453e419a..0000000000 --- a/docs/ja/appendices/phpunit10.md +++ /dev/null @@ -1,65 +0,0 @@ -# PHPUnit 10 へのアップグレード - -CakePHP 5 では、PHPUnit の最低バージョンが `^8.5 || ^9.3` から `^10.1` に変更されました。 この文書は、PHPUnit や CakePHP 由来の、いくつかの破壊的変更を紹介するものです。 - -## phpunit.xml の調整 - -PHPUnitの設定ファイルを、以下のコマンドで更新することが推奨されます: - - vendor/bin/phpunit --migrate-configuration - -> [!NOTE] -> 上記のコマンドを実行する前に、 `vendor/bin/phpunit --version` を実行して PHPUnit 10 が実行されていることを確認して下さい。 - -このコマンド(訳注 : `vendor/bin/phpunit --migrate-configuration` のこと)を実行することによって、お手元のプロジェクトの `phpunit.xml` ファイルには推奨される変更が適用された状態になります。 - -### 新しいイベントシステム - -PHPUnit 10 は、古い hook の仕組みを削除した上で、新しい [イベントシステム](https://docs.phpunit.de/en/10.5/extending-phpunit.html#extending-the-test-runner) が導入されました。 -ここでは、以下に示すような `phpunit.xml` は…: - -``` php - - - -``` - -次のように調整されます: - -``` php - - - -``` - -## `->withConsecutive()` の削除 - -削除されたメソッド `->withConsecutive()` は、応急措置的に置き換え可能です。例えば以下のコードは: - -``` php -->withConsecutive(['firstCallArg'], ['secondCallArg']) -``` - -次のように置き換えられます: - -``` php -->with( - ...self::withConsecutive(['firstCallArg'], ['secondCallArg']) -) -``` - -`Cake\TestSuite\TestCase` クラスには、 `Cake\TestSuite\PHPUnitConsecutiveTrait` 経由で、静的メソッド `self::withConsecutive()` が追加されました。なので、Testcase のクラスに手動で trait を仕込む必要はありません。 - -## data provider は static に - -お手元のプロジェクトのテストケースにおいて、PHPUnitの data provider 機能を活用している場合、それを static にする必要があります。例えば以下のコードは: - -``` php -public function myProvider(): array -``` - -次のように置き換えて下さい: - -``` text -public static function myProvider(): array -``` diff --git a/toc_en.json b/toc_en.json index 6177a7eee4..aff3bc9f13 100644 --- a/toc_en.json +++ b/toc_en.json @@ -413,7 +413,7 @@ "link": "/appendices/cakephp-development-process" }, { "text": "Glossary", "link": "/appendices/glossary" }, - { "text": "PHPUnit 10", "link": "/appendices/phpunit10" } + { "text": "PHPUnit Upgrade", "link": "/appendices/phpunit-upgrade" } ] } ] diff --git a/toc_ja.json b/toc_ja.json index 30690bd08e..39d1c9acc6 100644 --- a/toc_ja.json +++ b/toc_ja.json @@ -500,7 +500,7 @@ "link": "/ja/appendices/cakephp-development-process" }, { "text": "用語集", "link": "/ja/appendices/glossary" }, - { "text": "PHPUnit 10", "link": "/ja/appendices/phpunit10" } + { "text": "PHPUnit アップグレード", "link": "/ja/appendices/phpunit-upgrade" } ] } ]