Closed (fixed)
Project:
Drupal core
Version:
11.x-dev
Component:
phpunit
Priority:
Normal
Category:
Plan
Assigned:
Unassigned
Reporter:
Created:
17 Apr 2020 at 11:43 UTC
Updated:
22 Jan 2024 at 11:17 UTC
Jump to comment: Most recent
Wherever possible, use specific PHPUnit default assertions, that also provide standard messaging in case of assertion failure. PHPUnit's list @ https://phpunit.readthedocs.io/en/8.5/assertions.html is the best starting point to understand what default PHPUnit provides.
However, beware of PHPUnit's own deprecations -- the most accurate list is PHPUnit 9 at the moment, https://phpunit.readthedocs.io/en/9.1/assertions.html. Drupal does not support PHPUnit 9 yet, but has already removed usage of methods deprecated in PHPUnit 8 in #3110543: [meta] Support PHPUnit 9 in Drupal 9.
For example: assertSame(*, count()) can be replaced by assertCount().
Add child issues for each case and replace them.
Comments
Comment #2
mondrakeThere's a bunch of calls like
These are hardly readable with all the double negations etc etc.
Now we can use
assertStringContainsStringor
assertStringNotContainsStringand the
assertStringContainsStringIgnoringCasecompanions and get rid of this mindblowing madnessEDIT: regex
Comment #3
mondrakeFiled #3128746: Replace assertions involving calls to strpos() with more accurate string assertions
Comment #4
dww#3113077: Replace assertContains() on strings with assertStringContainsString() or assertStringContainsStringIgnoringCase() already has a parent, so I marked it related to this, not a child.
Comment #5
mondrakeOther interesting regexes for potential issues:
=.*this->assert(?!Session)(Equal|True|NotRe|Resp): assignment of return values of assertions (possible in Simpletest, do not make sense in PHPUnit and likely signal conditional code)>assert.*\(.*is_.*\(: possibly replaced by assertNull, assertIsObject, assertIsString, etc.>assert(?:True|False)\(.*(?:==|===): possibly replaced by assertEquals, assertSameComment #6
mondrakeComment #7
mondrakeComment #8
mondrakeComment #9
longwaveComment #10
longwaveComment #11
mondrakeComment #12
mondrakeComment #13
mondrakeComment #14
mondrakeComment #15
mondrakeComment #16
mondrakeComment #17
mondrakeComment #18
mondrakeComment #19
mondrakeComment #20
mondrakeComment #21
jungleComment #22
jungleComment #23
jungleComment #24
jungleBTW, filed #3131326: [meta] Deprecate PHPUnit mocks in favour of using Prophecy, comments are welcome. thanks!
Comment #25
jungleFiled #3131343: Replace assertions involving calls to in_array() with assertContains()/assertNotContains() , not sure the suggested ones have better readability than the originals.
Comment #26
mondrakeComment #27
jungleComment #28
dww#3128814: Replace assert* involving count() and an equality operator with assertCount() is pending D8 backport/commit. Moved.
Comment #29
mondrakeComment #30
mondrakeComment #31
mondrakeComment #32
mondrakeComment #33
jungleComment #34
jungle6 is-ish ones filed.
Comment #35
jungleIn addition to #34, it's unnecessary to file new issues for the following as I checked, feel free to double check.
Reference: Implement specialized alternatives to assertInternalType() and assertNotInternalType() #3368
Comment #36
longwaveComment #37
dwwBased on a lengthy Slack thread this morning, I just opened #3131946: [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages as a related (though independent) effort. Please discuss there. Thanks!
Comment #38
quietone commentedRefresshing th IS.
Comment #39
mondrakeComment #40
mondrakeComment #41
mondrakeComment #42
longwaveComment #43
mondrakeComment #44
mondrakeComment #45
mondrakeComment #46
mondrakeComment #47
mondrakeComment #48
mondrakeComment #49
mondrakeComment #50
jungleComment #51
jungleComment #52
jungleComment #53
jungleComment #54
mondrakeComment #55
longwave#3131088: Replace assertions involving calls to file_exists with assertFileExists()/assertFileNotExists() or assertDirectoryExists()/assertDirectoryNotExists() introduced some bad code in 8.9.x; I opened #3133103: [8.9 only] InstallerCustomConfigDirectoryCreateTest has bad assertions to fix this.
Comment #56
longwaveProposed two new issues:
#3133288: [meta] Add custom PHPUnit assertions for common tests
#3133291: Add XPath existence assertions
Comment #57
mondrakeComment #58
mondrakeComment #59
mondrakeComment #60
mondrakeComment #61
mondrakeComment #62
mondrakeComment #63
mondrakeComment #64
daffie commentedWhat I am missing here is a guide for which kind of assertion to use. When I now write a test it is to me very confusing which kind of assertion is should use.
Comment #65
jungle#64, good suggestion!
Comment #66
mondrake@daffie re #64, I find PHPUnit's list https://phpunit.readthedocs.io/en/8.5/assertions.html the best starting point to understand what default PHPUnit provides. Using those when possible it's the right choice IMHO.
Comment #67
mondrakeComment #68
mondrakeUpdated IS for #66
Comment #69
mondrakeMore IS updates
Comment #70
mondrakeComment #71
mondrakeComment #72
mondrakeComment #73
mondrakeComment #74
mondrakeComment #75
mondrakeComment #76
mondrakeComment #77
jungleAdded a kanban board for this meta issue
See: https://contribkanban.com/node-board/cb78a137-ce0b-4092-8a20-62fd088ec27f
Everyone can view and edit it.
Right now, 1 Postponed, 2 NW, 5 needs review and 1 RTBC'd, 22 fixed, thanks, everyone!
Comment #78
mondrake#77 is great, I think the link to the kanban deserves better visibility, done here.
Comment #79
mondrakeComment #80
jungleComment #81
mondrakeComment #82
mondrakeComment #83
mondrakeComment #84
mondrakeComment #85
mondrakeComment #86
mondrakeComment #87
mondrakeComment #88
mondrakeComment #89
mondrakeComment #90
mondrakeComment #92
mondrakeComment #93
mondrakeComment #94
jungleComment #95
mondrakeComment #97
mondrakeComment #99
mondrakeComment #103
mondrakeTime to call this done? If there are other optimizations possible, phpstan-phpunit will probably spot them and we will open individual issues.
Comment #104
quietone commentedYes, I agree.
Thanks for keeping this on track and to everyone who worked on the child issues!
Comment #106
quietone commentedThis is tagged for documentation, #64. Using the advice in #66, I added a link to the list of assertions to PHPUnit in Drupal, which was the suggested place in #65 point 2.