Problem/Motivation
As title
Proposed resolution
Example:
- $this->assertTrue(is_array($component));
+ $this->assertIsArray($component);
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | 3131816-8.9.x-33.patch | 14.22 KB | jungle |
| #33 | interdiff-29-33.txt | 902 bytes | jungle |
| #28 | interdiff_23-28.txt | 1 KB | spokje |
| #28 | 3131816-28.patch | 13.71 KB | spokje |
Comments
Comment #2
jungleComment #3
kapilv commentedComment #4
mondrake@kapilkumar0324 thanks - patch doesn't apply though, and
git grep -e '>assert.*is_array'yields 25+ results, so there are some that are not addressed hereComment #5
suresh prabhu parkala commentedPlease review!
Comment #6
spokjeDid the mentioned
git grep -e '>assert.*is_array'and tried to fix them all.Lets see what TestBot thinks.
Comment #7
spokje@Suresh Prabhu Parkala Oops, sorry! Crossing messages/patches there.
Comment #8
spokjeFixed typo in previous patch.
Comment #9
mondrake@kapilkumar0324 @Suresh you probably need to update your dev branch before creating the branch,
git pull origin, to avoid patch apply failures on the bot.Re #6:
should be split in 2 separate assertions:
- $this->assertTrue(!empty($available[$type]) && is_array($available[$type]));
+ $this->assertNotEmpty($available[$type]);
+ $this->assertIsArray($available[$type]);
+
same
same
+ $this->assertNotNull($value);
+ $this->assertIsArray($value);
+
same, split
same, split
Comment #10
spokje@mondrake Was actually already pondering that split. Thanks for that.
Attached patch incorporates comments in #9
Comment #11
spokjeComment #12
jungleChecked on my local with regex
assert.+is_array, no more assertions to replace.Just fixed 2 coding standard violations. If testing passes, this is RTBC.
Comment #13
mondrakeCool.
Comment #14
longwaveThis is in a loop, so $type is useful in the failure message? Without this the developer doesn't know which plugin type failed.
The other ones all look OK.
Comment #15
mondraketrying to put in practice my proposal in #3131946-8: [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages , these could be
In case of failure in PHPUnit, this will lead to the following messaging:
for the first assertion, supposing that
$this->definitions['myplugintype']is null:for the second assertion, supposing that
$this->definitions['myplugintype']is an array, but empty:Comment #16
spokjeThanks @longwave for the always valuable review and @mondrake for the code, I was scratching my head how to fix that.
New patch attached, based on #12, addressing #14 with #15
Comment #17
longwave#16 looks great!
Comment #18
jungleWell, I do not think the two assertion messages should be the same.
Changing the second one to
or any other better one.
Comment #19
jungleAddressing #18. Stay RTBC
Comment #20
xjmI double-checked that these methods don't exist in PHPUnit 6, which is presumably why we never used them before. For that reason, this issue can only be backported as far as 9.0.x.
Saving issue credit.
Comment #21
jungleRe #20, we made forwards-compatibility shim for it. See #3126787: [D8 only] Add forwards-compatibility shim for assertInternalType() replacements in phpunit 6&7, if 3126787 gets land first, this is ok to be backported to 8.x.
Comment #22
xjmThanks @jungle. +1 for that issue; I was worrying a bit about how many times a week we'd break HEAD by accidentally backporting new assertion types.
This looks good for the most part. Just a couple points of feedback relating to the assertion message cleanups:
Dropping the FormattableMarkup from the assertion message is sort of out of scope, but correct, so I'll let it slide since the patch is small enough.
However, I think we need to work on the assertion messages here. I would leave the
assertIsArray()with no custom assertion message, and then make the message for theassertNotEmpty()be:Plugin type '$type' should contain plugins.For these two lines, the assertion message was adding information useful to someone reading the test. Removing the message is OK, but can we instead add an inline comment above these two assertions? Something like:
Comment #23
spokjeReroll of #19 combined with addressing comments in #22
Comment #24
spokjeNot quite sure if I can put this back to RTBC, so just to be sure: NR
Comment #25
mondrakeIf it's an array, it's certainly not null, even if it's empty. So assertNotNull seems redundant here. Anyway /shrug.
Same here.
LGTM now.
Comment #26
mondrakeComment #27
xjm@mondrake is totally right, these two
assertNotNull()are totally redundant and can be removed now. Let's get rid of those quicklike. :)This is also now safe for 8.9.x due to the shim.
Thanks everyone!
Comment #28
spokjeHere's patch #23 with #25/#27 addressed
Comment #29
spokjeAnd a reroll of #28 against D8.9
Comment #30
mondrakeComment #31
spokjeIn D8.9
Drupal\Tests\Component\Annotation\Doctrine\DocParserTestextendsPHPUnit\Framework\TestCasewhich, inphpunit6.5 doesn't know aboutassertIsArraynor the shim.Went back to
this->assertTrue(is_array($foo));for that class only.Comment #32
jungleHi @Spokje, please try to use the trait,
Drupal\Tests\PhpunitCompatibilityTrait;, let's keep them being consistent!Comment #33
jungleUse the trait Drupal\Tests\PhpunitCompatibilityTrait / shim
Comment #34
mondrakeSo, #28 is for D9, #33 for D8.9.
Comment #35
longwaveRe #25/#27:
I disagree with the assertNotNull() here - "isset" is not the same as "not null". This would be more properly converted as
Still, I guess it doesn't really matter, because the test will error if the array key isn't set at the time we reach assertIsArray().
Comment #36
xjmSaving credit.
Comment #40
xjmCommitted #28 to 9.1.x and 9.0.x, and #33 to 8.9.x. Thanks!