Looking over the unit tests for Drupal\Component\Utilities I noticed some of the documentation could use some cleanups. Mostly converting @see to @covers.
Couple other things:
- There was also a lot of code missing tests so I wrote a couple and even found a bug in
UrlHelper::externalIsLocalthat made it impossible to match urls unless there was no path given. - UserAgent didn't have any unit tests. It did have what amounted to a UnitTest in a WebTests in language though so I converted it.
- Number had two test files that didn't need to be split.
Beta phase evaluation
| Issue category | Task |
|---|---|
| Unfrozen changes | Unfrozen because it only changes internal unit tests |
| Prioritized changes | The main goal of this issue is cleanup of unit tests. |
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | interdiff.txt | 13.47 KB | mile23 |
| #21 | 2294503_20.patch | 34.61 KB | mile23 |
| #16 | interdiff.txt | 3.26 KB | mile23 |
| #16 | 2294503_16.patch | 48.08 KB | mile23 |
| #14 | 2294503_14.patch | 48.46 KB | mile23 |
Comments
Comment #2
mile23Potentially overlapping issue here: #2298667: Code review for report generation
Comment #3
anavarreComing here from #2076325: Drupal\Component\Utility\Url::externalIsLocal() is wrong in common cases. I wonder if this is the source of the below error that we're trying to address in this issue?
FYI, 2 days ago it was not failing that early (but failing, still). I don't see anything else in the commits but #2076325: Drupal\Component\Utility\Url::externalIsLocal() is wrong in common cases. that would have changed anything.
Comment #4
mile23Yah, it looks like a bad @coversDefaultClass made it through #2076325: Drupal\Component\Utility\Url::externalIsLocal() is wrong in common cases., but the code and the test are good.
The patch at the top no longer applies.
Comment #5
neclimdulNot sure how I let that through on that other issue, sorry. Re-roll and some minor doc fixes. Had to rebase on the getInfo() removal so no clean interdiff.
Comment #6
neclimdultestbot and review
Comment #7
mile23It looks like these tests are double- and triple-dipping for coverage. The test only directly calls the first one in all of those cases, and there aren't any tests for some of the other ones.
This is a functional test so it doesn't cover anything. Should be marked @coversNothing.
Can a setUp() really be considered to cover some code?
Some other @covers beyond that.
Comment #8
neclimdul1) Multiple coverage of internal methods as its called different ways.
2) This is the only testing we do of those methods and since they're all connected, it is coverage as far as I'm concerned. Breaking up coverage would be pretty tedious as well.
3) Yes, I've been told this is correct in other issues.
Comment #9
sunI don't understand why this patch performs the following change everywhere:
Can we keep the functional method annotations closer to the method definition, please? (i.e., leave them were they are)
Especially a specified
@dataProvidershould be as quickly visible as possible. By moving it up, you first have to scroll away that other, overly verbose PHPDoc garbage.Comment #10
anavarreJust to note that today I'm seeing another failure, a bit later in the tests.
Looks related to #2276219: Asset libraries should declare their license
Comment #11
mile23@anvarre: That's a bit out of scope for this issue, but it's true that if we're using @covers (and we are), then the @covers annotation should at least be accurate. :-) Also, generating a coverage report should be part of the continuous integration process so these errors are caught up-front.
Comment #12
neclimdulReroll.
It seems the @params and @return types would be better closer. They explain what the arguments to the function are, which in the case of dataproviders is actually more important to understanding the test then reading what function is providing it. We where all over in placement even within individual test files so as I was fixing some, I moved others to match.
The _actual_ reason I went with this other then skimming and it seeming to be the more common option, was the short description, @group, @covers, and @dataprovider all describe the test metadata and the short description is forced to the top and @group tends to be at the top so I put all this at the top.
We should probably discuss doc standards in a doc standards issue though if this needs to be a standard. And if this absolutely blocks this issue I'll undo the changes but obviously I'd rather just get it in rather then spending the time on it.
Comment #13
sunAFAIK, that issue is #2253915: [policy, no patch] Fix coding standards for PHPUnit tests
As also noted over there, I strongly disagree with applying our overly verbose PHPDoc standards to PHPUnit tests/methods. IMO, @param and @return shouldn't even exist; all unit test code should (or actually MUST) be of trivial complexity; if you need to explain anything, then your test has a fundamental problem. The vast majority of unit test methods should not have any PHPDoc at all; most of the rest should be happy with a single @dataProvider annotation, nothing else.
Your thinking of standardization/harmonization is very appreciated (because quality), but yeah, I think you've picked and applied/adopted the wrong baseline here. It would be great if we can undo those changes and defer the standards discussion to #2253915.
Comment #14
mile23Re-roll.
Comment #16
mile23And this should work.
The main problem was UserAgentTest, which contained a reference to \Drupal\Core\Language\Language, and the deprecated randomName().
I refactored the setUp() method into two methods which supply language codes and mappings. Using $this->someProperty for dependencies in unit tests is generally bad form.
Comment #17
mile23@neclimdul: I'd split this out into two more issues, one for UserAgentTest and one for HtmlTest, leaving the actual cleanup part here.
I'd do it but it's really your code. :-)
Comment #18
mile23Comment #19
jhedstromIdeally this would have been separate issues, but I don't think that's crucial.
Added a beta evaluation.
Comment #21
mile23Here's the cleanup without the extra tests (UserAgentTest and HtmlTest) or changes to phpunit.xml.dist.
Note that I removed the UserAgent test and put it back into LanguageBrowserDetectionUnitTest.
Followups to follow. :-)
Comment #22
mile23The new tests (UserAgentTest, HtmlTest) live here now: #2382011: Expand unit testing for Drupal\Component\Utility\UserAgent
Comment #23
jhedstrom#21 is good work towards standardizing how the phpunit tests are written, and is allowed under beta evaluation.
Comment #24
alexpottCommitted 65f1d49 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation for to the issue summary.