Problem/Motivation

As title

Proposed resolution

Remove redundant assertion messages at the same time.

Example:

-      $this->assertTrue(is_object($node), 'Node is an object');
+      $this->assertIsObject($node);

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jungle created an issue. See original summary.

jungle’s picture

Title: Replace assertions involving calls to is_object() assertIsObject()/assertIsObject() » Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject()
jungle’s picture

Issue summary: View changes

Updating IS and mentioning to remove assertion messages

jungle’s picture

Issue summary: View changes
jungle’s picture

Assigned: Unassigned » jungle
mero.S’s picture

Status: Active » Needs review
FileSize
5.93 KB

Please review patch.

jungle’s picture

FileSize
12.73 KB

@mero.S, I did assign to myself. 🤦 If you want to work on one issue, better to assign yourself first. I will still submit mine, interdiff and comments coming next.

jungle’s picture

FileSize
2.15 KB
jungle’s picture

Assigned: jungle » Unassigned
FileSize
8.36 KB

Wrong interdiff submitted in #8, sorry.

mero.S’s picture

Okay, I'm sorry)

jungle’s picture

@mero.S, no worries, anyway, thanks for working on this!

Review of #6

  1. The replacement is incomplete, see the interdiff in #9
  2. +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php
    @@ -369,7 +369,7 @@ protected function doTestTranslationDeletion() {
    -    if ($this->assertIsObject($entity)) {
    +    if (is_object($entity)) {
    

    Instead of replacing it, I did remove the assertion in if which makes more sense to me.

Note for reviewer(s): the assertion message below inside the foreach loop was kept on purpose, as no consent reached on such case yet.

+++ b/core/tests/Drupal/KernelTests/Core/Database/FetchTest.php
@@ -39,7 +39,7 @@ public function testQueryFetchObject() {
     foreach ($result as $record) {
       $records[] = $record;
-      $this->assertTrue(is_object($record), 'Record is an object.');
+      $this->assertIsObject($record);
       $this->assertIdentical($record->name, 'John', '25 year old is John.');

Regex used: assert.+is_object

mero.S’s picture

thank you for explanation)
I will find the next issue

jungle’s picture

Note for reviewer(s): the assertion message below inside the foreach loop was kept on purpose, as no consent reached on such case yet.

Wrong comment in #11, sorry again, in fact, 3 foreach loops in total were touched, one assertion message in one of them was kept on purpose only.

+++ b/core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php
@@ -274,7 +274,7 @@ public function testValueTypeIsKept() {
     foreach ($variables as $cid => $value) {
       $object = $backend->get($cid);
-      $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
+      $this->assertIsObject($object, sprintf("Backend returned an object for cache id %s.", $cid));

+++ b/core/tests/Drupal/KernelTests/Core/Database/FetchTest.php
@@ -24,7 +24,7 @@ public function testQueryFetchDefault() {
     foreach ($result as $record) {
       $records[] = $record;
-      $this->assertTrue(is_object($record), 'Record is an object.');
+      $this->assertIsObject($record);

@@ -39,7 +39,7 @@ public function testQueryFetchObject() {
     foreach ($result as $record) {
       $records[] = $record;
-      $this->assertTrue(is_object($record), 'Record is an object.');
+      $this->assertIsObject($record);

The last submitted patch, , failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 7: 3131818-7.patch, failed testing. View results

jungle’s picture

Status: Needs work » Needs review
FileSize
12.87 KB
887 bytes
+++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php
@@ -369,7 +369,7 @@ protected function doTestTranslationDeletion() {
-    if ($this->assertTrue(is_object($entity), 'Entity found')) {
+    if (is_object($entity)) {

Fixing testing failure in #7. Doubt the original code inside if never gets executed, as it's always false. -- assertTrue returns void/null?

mondrake’s picture

jungle’s picture

Status: Needs review » Postponed
+++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php
@@ -369,11 +369,10 @@ protected function doTestTranslationDeletion() {
+    $this->assertTrue(is_object($entity), 'Entity found');
+    $translations = $entity->getTranslationLanguages();
+    $this->assertCount(2, $translations, 'Translation successfully deleted.');
+    $this->assertArrayNotHasKey($langcode, $translations, 'Translation successfully deleted.');

Code snippet from #3129074. Thank you @mondrake! Then I'd postpone this to let #3129074 land first.

catch’s picture

Status: Postponed » Needs review

Unpostponing.

But also this can't be backported to 8.9.x until #3126787: [D8 only] Add forwards-compatibility shim for assertInternalType() replacements in phpunit 6&7 lands.

mondrake’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
jungle’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
9.63 KB

Rerolled from #16

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Supposing this is returning back green, it LGTM.

xjm’s picture

Saving credit.

  • xjm committed 2718a80 on 9.1.x
    Issue #3131818 by jungle, mero.S: Replace assertions involving calls to...

  • xjm committed aacc881 on 9.0.x
    Issue #3131818 by jungle, mero.S: Replace assertions involving calls to...
xjm’s picture

Title: Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject() » [backport] Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject()
Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Needs work

Committed to 9.1.x and 9.0.x, thanks!

Next we need an 8.9.x version of the patch to test with the shim.

jungle’s picture

Status: Needs work » Needs review
FileSize
9.66 KB
1.68 KB

Thanks all!

Rejected file: core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php

(rejected hunks)
@@ -144,7 +144,7 @@ public function testSetGet() {
     $this->assertFalse($backend->get('test2'), "Backend does not contain data for cache id test2.");
     $backend->set('test2', ['value' => 3], REQUEST_TIME + 3);
     $cached = $backend->get('test2');
-    $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
+    $this->assertIsObject($cached);
     $this->assertSame(['value' => 3], $cached->data);
     $this->assertTrue($cached->valid, 'Item is marked as valid.');
     $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
@@ -231,16 +231,16 @@ public function testDelete() {
 
     $this->assertFalse($backend->get('test1'), "Backend does not contain data for cache id test1.");
     $backend->set('test1', 7);
-    $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");
+    $this->assertIsObject($backend->get('test1'));
 
     $this->assertFalse($backend->get('test2'), "Backend does not contain data for cache id test2.");
     $backend->set('test2', 3);
-    $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");
+    $this->assertIsObject($backend->get('test2'));
 
     $backend->delete('test1');
     $this->assertFalse($backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
 
-    $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
+    $this->assertIsObject($backend->get('test2'));
 
     $backend->delete('test2');
     $this->assertFalse($backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Lgtm

xjm’s picture

Title: [backport] Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject() » Replace assertions involving calls to is_object() with assertIsObject()/assertIsObject()
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed the backport to 8.9.x. Thanks!

  • xjm committed e0b46df on 8.9.x
    Issue #3131818 by jungle, mero.S, xjm: Replace assertions involving...

Status: Fixed » Closed (fixed)

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