Problem/Motivation
Currently, running the phpunit job in at least some contrib modules will print the following warning several times:
PHP Warning: Undefined array key 0 in /builds/project/search_api/web/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php on line 410
(Line number varies by Drupal version, of course, but that’s the one in 11.4.x dev.)
It seems that in \Drupal\Core\Test\PhpUnitTestDiscovery::getTestClassInfo() even though there is an explicit check whether $groups is empty, it can then happen that $groups[0] is not set. My conclusion would be that $groups is not necessarily a list but could be an associative array.
Examples can be seen here and here.
Steps to reproduce
Run the phpunit job of a contrib module. It might only produce the error when test classes use the @group annotation instead of the Group attribute but that is simply a guess.
Proposed resolution
Maybe use array_first($groups) instead of $groups[0]? (It’s part of symfony/polyfill-php85 so should be fine to use even though we don’t require PHP 8.5 yet.)
Remaining tasks
- Create MR.
- Discuss solution.
- Review MR.
User interface changes
N/A
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
Issue fork drupal-3607165
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
drunken monkeyMy suggested very simple solution is in this MR, but there might of course be more to it than that. I’m not at all familiar with this code so can’t say.
Comment #4
smustgrave commentedIssue would have to land in main first but probably will need test coverage
Comment #5
mondrakeThe problem is in the tests:
won't work because since PHPUnit 10, if you add at least one attribute then the annotations are discarded. So the bug appears because there's no group metadata detected for the test. So yes maybe we could add some protection to prevent the warning but the array would still be empty.
Comment #6
mondrakeSee Tests with PHPUnit 10 attributes are now supported
Comment #7
mondrakeBTW, it looks like Drupal core own PHPStan rules are not included in contrib. Those would report the above as errors. Will ask in Slack #gitlab.
Comment #8
mondrake