Active
Project:
Coding Standards
Component:
Coding Standards
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
21 Jun 2024 at 06:43 UTC
Updated:
29 Oct 2025 at 07:29 UTC
Jump to comment: Most recent
Part of #2057905: [policy, no patch] Discuss the standards for phpunit based tests
We also do not have any restrictions on how they are named, but a best practice of naming single-use providers with this pattern has emerged:
testWhateverThing(),providerTestWhateverThing().tbd
Come up with an enforceable standard
If we adopted this change, the Drupal Project would benefit by ...
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
Add current text in blockquotes
Add proposed text in blockquotes
For a full explanation of these steps see the Coding Standards project page
Comments
Comment #2
quietone commentedBjörn Brala (bbrala) commented in the coding standards meeting, #3456119: Coding Standards Meeting Tuesday 2024-06-18 2100 UTC, that there is a rector for removing test from dataproviders, https://github.com/rectorphp/rector-phpunit/blob/main/docs/rector_rules_...
Edit: try this link, https://getrector.com/find-rule?query=removedata
Comment #3
jonathan1055 commentedThe agreement and documenting of this standard has become more urgent in the last few weeks, because the PHPStan in contrib pipelines running 'next minor' (which is currently 11.2-dev) now report errors such as
I don't know what method name it is expecting, but whatever it is I guess this should be our new standard, to be documented in the Coding ld be. Probably it would be a new paragraph in php/php-coding-standards#naming
Comment #4
jonathan1055 commentedThe link in #2 is broken. Any ideas where it was meant to point?
Comment #5
jonathan1055 commentedI found the core issue that fixed the names - #3351089: Fix PHPStan L1 errors "@dataProvider Foo related method not found."
But actually, PHPStan at level 1 is not actually checking any expected name of the data provider function. The warning above is when checking that the test function contains the expected tag
@dataProvider whateverYouCalledItbut without the()after the name.This could also be added into the new section of the standards page.
Comment #6
larowlanStrong -1 from me
We don't enforce method names anywhere else, so I don't think enforcing one for data providers is appropriate
Comment #7
jonathan1055 commentedYes, the urgency which I stated in #3 was in fact not the case. The PHPStan warning is only complaining about the matching
@dataProvidertag in the test function's doc block.Comment #8
quietone commentedWe do have a standard for method/function names and class names which core violates. I am not convinced that Drupal should not enforce it's own standard.
As for core, they are not enforced because a core committer has pushed back on implementing the naming standard for methods. Instead of discussing that I chose to work on implementing other standards.
Comment #9
quietone commentedComment #10
quietone commentedFor completeness that standards for naming a function are in item 2, 3, and 4 of Classes, Methods and Properties. It states the following
It is about style not the actual name.
Comment #11
joachim commentedI'm +1 on this.
But:
> *single-use* providers
What happens when a single-use provider is converted to a multi-use provider because we add another test which uses it too?
Comment #12
borisson_The link in #2 no longer works.
I also tend to agree with @joachim in #11, single use providers can easily go to provide for multiple test methods.
I also just commented this in the slack discussion:
I don't think the names have to match the test names. I have gotten into the habit of doing something like
provideMenuLinks- a name that is based on what is being returned. If there are multiple needs it could beprovideInvalidMenuLinks/provideValidMenuLinksor evenprovideMenuLinksAllUpperCase.I agree with the having provide or provider in the method names, and I personally prefer the earlier, because that way we kind of force people to come up with a name that means something?
#6:
I mean you're right about this, it's not in the coding standards, but all test methods start with test (
public function testSomething). It's also possible to use another name and annotate with @test, but I don't think we do that anywhere? At least I couldn't find something.Comment #13
quietone commentedOn the other hand, if dataproviders are required to be documented then the name doesn't really matter.
Comment #14
quietone commentedThe link in #2 is copied from a comment made in a Coding Standards meeting. I did some searching and found this, https://getrector.com/find-rule?query=removedata,