Problem/Motivation
As per #2956556-23: class isn't set in FETCH_OBJECT when class_name isn't set
In Drupal\KernelTests\Core\Database\FetchTest we can see following within the tests:
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
$this->assertSame('Kay', $record->name, 'Kay is found.');
$this->assertSame('Web Developer', $record->job, 'A 26 year old Web Developer.');
}
$this->assertFalse(isset($record->classname), 'Classname field not found, as intended.');
}
assertTrue() returns void which means that the assertions within the conditional are never executed.
Proposed resolution
Remove the conditional around the assertTrue() for all the tests.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #2
longwaveThere are a handful of other cases, can we expand scope to include all of these at once?
Comment #3
johnwebdev commented#2 Good question. I guess we can, but we could also expand the scope of this issue to fix some of the assertion of this test, i.e.
Change:
To:
As we did for the new test in #2956556: class isn't set in FETCH_OBJECT when class_name isn't set.
What do you think?
Comment #4
johnwebdev commentedComment #5
mondrakeplease use
assertSame($expected, $actual)instead.assertIdenticalis deprecat-ish :)Comment #6
johnwebdev commented#5 See #3.
Comment #7
mondrake#6 I think it's better proceed by 'type of messed up assertion' here, because otherwise the scope will become giant in a blink. See more issues at #3128573: [meta] Replace assertions with more appropriate ones. There's one specifically for #3, see #3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf().
Comment #8
longwaveIn line with #7 I think if we expand scope here we should just remove the conditionals and leave the actual assertions as they are - for other, similar issues to resolve next. That gives us a fairly tightly scoped issue that we can hopefully get committed quite quickly.
Comment #9
johnwebdev commentedNW for #2. Should also update title+description of the issue.
Comment #10
mondrakeAgree with #8, that also means #5 is OOS here.
So if we do #2 in this one, it could be re-titled and re-scoped to "Remove assertions that are expecting a result to execute conditionally further assertions". Since in PHPUnit assertions do not return any value (differently from Simpletest), we may already being never executing some tests if we e.g. expect a TRUE from an assert*()...
Raising to major because of that.
EDIT - Xposted with #9.
Comment #11
suresh prabhu parkala commentedPlease review!
Comment #13
Lal_$this->assertSame() typo
Comment #14
Lal_Comment #15
Lal_Comment #16
mondrakeThank you @Lal_ and @Suresh. In fact per #8 and #10 we should not change any assertion here, only remove them from the
if () {}blocks, and inline further assertions.Comment #17
longwaveInterdiff from #3.
Note that this change is required or the test fails with
Comment #18
longwaveWhile we said we shouldn't change any assertions, this is probably better off as
$this->assertEmpty($violations)with no message, and then I think any violations would be automatically reported?Comment #19
mondrakeI think #18 makes sense. NW for that.
Note there will be other like-for-like cases when the return value of an assert* is assigned to a variable and then the variable is used in a conditional:
but it's tricky so it's probably better do it in a follow up.
Comment #20
kapilv commentedComment #21
mondrakeOn this.
Comment #22
kapilv commentedComment #23
kapilv commentedComment #24
mondrakeAddressed #18.
WRT #19, I found the following:
IMHO we should address them in this issue, but would like to get +1
Comment #25
mondrake@kapilkumar0324 please note that when a contributor self-assigns an issue to her/himself, it usually means that s/he is working on it and will post a patch soon (well, at least sooner or later) - it's a kind of etiquette to prevent other people duplicating her/his effort.
Removing 'Contrib database driver' tag as I cannot see the relation to this issue.
Comment #26
longwave#24 is RTBC
-1 to dealing with the assignments here, some of them look tricky and/or like they might have further refactors involved, it will be easier to review if we do these separately.
Comment #27
mondrake#26 yes, I also doubleclicked on them and my hair went up, so better tackle them separately.
Comment #28
kapilv commented@mondrake, sorry I will remember next time. Thanks
Comment #29
jungleBTW, just filed a new issue for this
Comment #30
jungleFiled #3131818: Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject() for this. meanwhile inspired by this one, filed 5 more is_ish ones.
Comment #31
longwaveWe aren't actually removing assertions here, if anything we are adding ones that were previously never executed!
Comment #32
longwaveOpened #3131900: Refactor assertions that assign return values to variables as a followup
Comment #33
longwaveSimpler title.
Comment #34
jungleThis patch will break other ongoing patches probably. And assertion messages did not remove.
Just postponed #3131818: Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject() to wait for this one landing first. meanwhile, #3131816: Replace assertions involving calls to is_array() with assertIsArray()/assertIsNotArray() was RTBC'ed. So probably, this one should be postponed or NW, to remove assertion messages and use more appropriate assertions.
Comment #35
longwaveAddressed #34 - tried to use the most appropriate assertion, added a couple where I think it makes sense, and removed all the messages.
Comment #36
jungleChecked the line before
if ($entity_id) {, there is an assertion for $entity_id already. so the change is good.+1 to this
LGTM. @longwave, thanks!
Comment #40
catchCommitted/pushed to 9.1.x and cherry-picked back to 8.9.x, thanks!
Comment #41
mondrakethe 8.9 commit needs reversal since the patch is using methods not present in PHPUnit 6, HEAD testing is broken
Comment #43
catchReverted the 8.9.x commit, moving to postponed.
Comment #44
catchPostponed on #3126787: [D8 only] Add forwards-compatibility shim for assertInternalType() replacements in phpunit 6&7.
Comment #45
mondrakeBloacker is in.
Comment #46
mondrake#35 is now green on D8.9, too.
Comment #48
xjmReverted the revert so things should work now. 🤞 Thanks!