Featured image of post 解決 PHPUnit 出現「Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12.」問題

解決 PHPUnit 出現「Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12.」問題

解決 PHPUnit 出現「Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12.」問題

Photo by Luca Bravo on Unsplash

Metadata found in doc-comment Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12.

在更新完 PHPUnit 套件後,一直出現 Metadata found in doc-comment Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. 的訊息,雖然程式還是可以順利執行測試,但是看到這些訊息還是覺得有點煩

PHPUnit 11.2.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.20
Configuration: /var/www/html/kj_app/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 00:00.522, Memory: 32.00 MB

There was 1 PHPUnit test runner deprecation:

1) Metadata found in doc-comment for method \Scenario\Feature\FeatureScenarioTest::test_user_sign_up(). Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead.

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 1.

原始問題

因為他也沒明說是哪個部分有問題,只說之後 PHPUnit 12 會不再支援,查到的部分是原本寫在註解的 PHPUnit 設定會不再支援,我原本的測試程式長這樣會出錯

/**
 * @group scenario_user
 * @group test_group_3
 */
public function test_user_sign_up(){

}

解決方式

以後不需要在函式名稱前面寫 test 當作前綴

// Before
public function test_user_sign_up(){

}

改用一個屬性物件 Test 當作註解寫在函式前面即可,這樣就不會每個函式都是 test_ 前綴當作開頭

use PHPUnit\Framework\Attributes\Test;

// Aftesr
#[Test]
public function user_sign_up(){

}

群組方式使用 Group 屬性

// Before
/**
 * @group scenario_user
 * @group test_group_3
 */
public function test_user_sign_up(){

}

改用 Group 屬性,並在參數內帶入你的群組名稱即可

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\Group;

// Aftesr
#[Group('scenario_user')]
#[Group('test_group_3')]
#[Test]
public function user_sign_up(){

}

這樣就解決了 PHPUnit 測試不斷出現警告訊息的問題,也避免之後升級到 PHPUnit 12 時無法順利執行的狀況

Reference

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy