Upgrading PHPUnit - "metadata in doc-comments is deprecated [...] use attributes instead"
> composer test
Metadata found in doc-comment for method
MyTest::it_loads_user_ok(). Metadata in
doc-comments is deprecated and will no
longer be supported in PHPUnit 12. Update
your test code to use attributes instead.
Recently I was working on some package updates for Laravel 11 compatibility. As part of this, the version of PHPUnit I was using was bumped from 9 to 11. I ran the tests, and then started getting warnings like the above. The tests all passed, but a lot of notices were clogging up my terminal.
This is happening because our test code previously looked something like this:
class MyTest extends TestCase
{
/** @test */
public function it_loads_the_user_ok()
{
...
}
The @test
doc comment there meaning we don't need to add test
into our function names for PHPUnit to pick them up and run them. However, support for these doc comments is being removed in PHPUnit 12, with deprecation warnings being shown in current versions.
From the PR covering this update:
Support for attributes in PHPUnit will be implemented like so:
- PHPUnit 10 will first look for metadata in attributes before it looks at comments
- When metadata is found in attributes, metadata in comments is ignored
- Support for metadata in comments is closed for further development (bugs will be fixed, but no new functionality will be implemented based on annotations)
- Support for metadata in comments will be deprecated in PHPUnit 11
- Support for metadata in comments will be removed in PHPUnit 12
The solution is to use attributes to replace these comments. Our example above becomes:
use PHPUnit\Framework\Attributes\Test;
class MyTest extends TestCase
{
#[Test]
public function it_loads_the_user_ok()
{
...
}
In this case, the @test
comment is replaced by a Test
attribute, loaded from PHPUnit\Framework\Attributes\Test
. Once the comments get swapped out for attributes, the warnings go away, and the test suite is nice and clean again!
The full list of replacements, and how they map from comment to attributes, is available on the PHPUnit PR.
CyberWiseCon 2025
In May 2025, I'll be giving a talk at CyberWiseCon 2025 in Vilnius, Lithuania. From selling 10 Downing St, to moving the Eiffel Tower to Dublin, this talk covers real-world examples of unconventional ways to stop scrapers, phishers, and content thieves. You'll gain practical insights to protect assets, outsmart bad actors, and avoid the mistakes we made along the way!
Get your ticket now and I'll see you there!