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

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
StatusFileSize
new103.06 KB
jungle’s picture

Status: Needs review » Needs work

Thanks, @mondrake for filing this!

PHP Parse error:  syntax error, unexpected ';', expecting ')' in /var/www/html/core/modules/views/tests/src/Kernel/Plugin/DisplayKernelTest.php on line 99
xargs: php: exited with status 255; aborting
PHP Parse error:  syntax error, unexpected ';', expecting ')' in /var/www/html/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php on line 446
mondrake’s picture

Title: Remove redundant $messages from assert(not)InstanceOf calls » Remove redundant $messages from assert(Not)InstanceOf calls
Issue summary: View changes

Discovery regex:

assert(?:Not)?InstanceOf\(.*,.*,.*[\'"]

mondrake’s picture

Status: Needs work » Needs review
StatusFileSize
new1.8 KB
new103.06 KB
jungle’s picture

+1 to do this,

  1. The assertion, assertInstanceOf / assertNotInstanceOf, is self explained. To me, the optional message is not necessary, rarely read it.
  2. And even if the message is important, as a fallback an inline comment is expected and better for eyes -- read from up to down, not left to right, to avoid horizontal scrolling.

Status: Needs review » Needs work

The last submitted patch, 5: 3130811-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jungle’s picture

  1. core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php, Line 6 Unused use statement
  2. The other failure seems irrelevant. A random failure probably.
mondrake’s picture

Title: Remove redundant $messages from assert(Not)InstanceOf calls » Remove redundant $message from assert(Not)InstanceOf calls
Status: Needs work » Needs review
StatusFileSize
new653 bytes
new103.35 KB

Fixed #8.

longwave’s picture

Status: Needs review » Needs work

Missed a few:

core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php:

            $this->assertInstanceOf(HandlerBase::class, $handler, new FormattableMarkup(
              '@type handler of class %class is an instance of HandlerBase',
              [
                '@type' => $type,
                '%class' => get_class($handler),
              ]));

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:

  public function assertTwigTemplate($value, $message = '') {
    $this->assertInstanceOf(TemplateWrapper::class, $value, $message);
  }

core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php:

  public function assertTwigTemplate($value, $message = '') {
    $this->assertInstanceOf(TemplateWrapper::class, $value, $message);
  }
mondrake’s picture

Status: Needs work » Needs review
StatusFileSize
new906 bytes
new104.24 KB

Thank 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.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

OK, let's leave the Twig bits for a followup. Otherwise, this is RTBC.

jungle’s picture

StatusFileSize
new104.5 KB
new595 bytes

Removed 1 unused use statement. Stay RTBC.

catch’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Committed/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.

  • catch committed 27515dc on 9.1.x
    Issue #3130811 by mondrake, jungle, longwave: Remove redundant $message...

  • catch committed 131684c on 9.0.x
    Issue #3130811 by mondrake, jungle, longwave: Remove redundant $message...
longwave’s picture

#3128905: Replace assert* involving an instanceof operator with assertInstanceOf()/assertNotInstanceOf() was not backported yet, that is the main reason the cherry pick failed.

ravi.shankar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new10.31 KB

Here I have done this patch on Drupal 8.9.x.
Maybe this will work for Drupal 8.9.x.

longwave’s picture

mondrake’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.9.x, thanks!

  • catch committed 286c1b5 on 8.9.x
    Issue #3130811 by mondrake, jungle, longwave, ravi.shankar: Remove...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.