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
主题 StackJimmy 设计