Problem/Motivation
In #3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf(), we standardized across standard PHPUnit's assert(Not)InstanceOf calls to check if an object is implementing a specific class.
Many of the conversion done there are from legacy Simpletest assertions, when it was normal to add a message to the assertion.
However:
1) In Simpletest, the message was a description of what the assertion was checking, so that in the Simpletest UI this could be visually presented as a result of the test run. In PHPUnit, on the contrary, the message is a description of the failure occurred while testing, so that it can be presented in the log and then test execution stops.
In the conversion, the messages should be 'negated', for example:
$this->assertInstanceOf(ViewExecutable::class, $factory->get($view), 'A ViewExecutable instance was returned from the factory.');
For Simpletest the message was OK, but in PHPUnit, if the assertion fails, it would be because the $factory->get($view) is NOT an instance of ViewExecutable::class, so the message should be, really, 'The object returned from the factory was not an instance of ViewExecutable.'
So to say that in HEAD currently the messages were not adjusted as part of conversions and are in many cases misleading in case of test failures.
2) The standard message generated by PHPUnit in case of failure of an assert(Not)InstanceOf is probably more than enough in 99% of the cases. GIven the case above, failure of
$this->assertInstanceOf(ViewExecutable::class, $factory->get($view));
will return something like
$ phpunit InstanceOfTest
PHPUnit 9.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) InstanceOfTest::testFailure
Failed asserting that 'WhateverClass" Object (...) is an instance of class "ViewExecutable".
/home/sb/InstanceOfTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Proposed resolution
Just remove all the $message arguments from assert(Not)InstanceOf calls and rely on standard PHPUnit messaging. Also it's not necessary to move the string to a comment since it's pretty clear from reading the code what is being checked.
Deviate from above only when it's clear a bespoke message would add value.
Discovery regex:
assert(?:Not)?InstanceOf\(.*,.*,.*[\'"]
Remaining tasks
Discuss
Review patch
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | 3128905-plus-3130811-19.patch | 203.03 KB | longwave |
| #18 | 3130811-18.patch | 10.31 KB | ravi.shankar |
| #13 | interdiff-11-13.txt | 595 bytes | jungle |
| #13 | 3130811-13.patch | 104.5 KB | jungle |
| #11 | 3130811-11.patch | 104.24 KB | mondrake |
Comments
Comment #2
mondrakeComment #3
jungleThanks, @mondrake for filing this!
Comment #4
mondrakeDiscovery regex:
assert(?:Not)?InstanceOf\(.*,.*,.*[\'"]Comment #5
mondrakeComment #6
jungle+1 to do this,
Comment #8
jungleComment #9
mondrakeFixed #8.
Comment #10
longwaveMissed a few:
core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php:
Arguable that these next ones can stay, however also arguable that the helper method is useless and can be refactored away:
core/modules/system/tests/src/Functional/Theme/TwigRegistryLoaderTest.php:
core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php:
Comment #11
mondrakeThank you, done the HandlerAllTest. I was very much tempted to slash the Twig helpers, then I realised it's going to be seen as a scope creep, probably. Better do that in a separate issue I guess.
Comment #12
longwaveOK, let's leave the Twig bits for a followup. Otherwise, this is RTBC.
Comment #13
jungleRemoved 1 unused use statement. Stay RTBC.
Comment #14
catchCommitted/pushed to 9.1.x and cherry-picked to 9.0.x.
Does not cherry-pick to 8.9.x, but we should do so to keep tests in sync.
Comment #17
longwave#3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf() was not backported yet, that is the main reason the cherry pick failed.
Comment #18
ravi.shankar commentedHere I have done this patch on Drupal 8.9.x.
Maybe this will work for Drupal 8.9.x.
Comment #19
longwaveRerolled this on top of the latest patch in #3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf().
Comment #20
mondrakeSo let's RTBC this one, and mark fixed #3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf()
Comment #21
catchCommitted/pushed to 8.9.x, thanks!