Problem/Motivation

For example:

-      $this->assertTrue(array_search($item, $referenceables[$this->bundle]) !== FALSE);
+      $this->assertContains($item, $referenceables[$this->bundle]);

Proposed resolution

Recommended regex: assert.+array_search for searching.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

jungle created an issue. See original summary.

jungle’s picture

Issue summary: View changes
jungle’s picture

Status: Active » Needs review
StatusFileSize
new3.38 KB
jungle’s picture

Issue summary: View changes
StatusFileSize
new3.62 KB
new515 bytes

Removed one unused use statement

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

grep -Er '>assert.*array_search' .

yields two more results,

./core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php:      $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
./core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryDecoratorTest.php:      $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);

but those have their own reason.

LGTM

  • catch committed 6cf25ba on 9.1.x
    Issue #3131474 by jungle, mondrake: Replace assertions involving calls...

  • catch committed 280c02e on 9.0.x
    Issue #3131474 by jungle, mondrake: Replace assertions involving calls...
jungle’s picture

StatusFileSize
new2.07 KB
new5.02 KB

@catch, thanks for committing, a patch for 8.9.x. checked against 8.8.x as well, it applies, and no more assertion to replace, to me.

alexpott credited catch.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php
    @@ -89,10 +89,10 @@ private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRU
         if ($is_assigned) {
    -      $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.');
    +      $this->assertContains($rid, $account->getRoles());
         }
         else {
    -      $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.');
    +      $this->assertNotContains($rid, $account->getRoles());
         }
    

    Woah the original code is very confusing lol.

  2. +++ b/core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryDecoratorTest.php
    @@ -82,7 +82,7 @@ public function testGetDefinitions() {
    -      $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
    +      $this->assertContains($id, $this->expectedKeys, $definition['provider']);
    
    +++ b/core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php
    @@ -63,7 +63,7 @@ public function testGetDefinitions() {
    -      $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
    +      $this->assertContains($id, $this->expectedKeys, $definition['provider']);
    

    These conversions don't look right. I don't think they should be changed.

alexpott’s picture

Version: 9.1.x-dev » 9.0.x-dev
Status: Needs work » Fixed

I don't think this is worth doing this task on Drupal 8. There's enough other tasks to get in and now 8.9.x is beta officially we don't do tasks on 8.x - of course we can make exceptions for tests but this one does not feel worth it.

Fortunately the patch for D9 didn't have the incorrect conversion I spotted in #10.

xjm’s picture

Version: 9.0.x-dev » 8.9.x-dev

I actually would prefer to backport all these changes to 8.9.x once the BC shim is in, for parity between 8.9 and 9.0 and easier backports.

xjm’s picture

Status: Fixed » Postponed
xjm’s picture

Status: Postponed » Needs review

Oh, assertContains() doesn't appear to be in the backport set -- probably would need another patch on top of that. Which I think is worth doing. We have to maintain 8.9.x for the next year and a half so let's make it easy.

xjm’s picture

Status: Needs review » Postponed
Issue tags: +Needs followup

This (and all the issues like it) would be postponed on a followup from #3126787: [D8 only] Add forwards-compatibility shim for assertInternalType() replacements in phpunit 6&7 that provides the shim.

mondrake’s picture

Status: Postponed » Needs work

assertContains is a native PHPUnit method since at least version 4, so no need of fc shims here. NW though for #10 for the backport.

xjm’s picture

lol, thanks @mondrake for sorting that out. :)

jungle’s picture

Status: Needs work » Needs review
StatusFileSize
new3.63 KB
new1.39 KB

Addressing #10.2

Thanks!

jungle’s picture

Issue tags: -Needs followup

Removing tag as @mondrake said assertContains() is a native PHPUnit method.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

LGTM now

  • xjm committed c113c5c on 8.9.x
    Issue #3131474 by jungle, mondrake, alexpott: Replace assertions...

  • xjm committed 01bdc94 on 8.8.x
    Issue #3131474 by jungle, mondrake, alexpott: Replace assertions...
xjm’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Fixed

OK, those remaining few seem fine.

+++ b/core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php
@@ -89,10 +89,10 @@ private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRU
     if ($is_assigned) {
-      $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.');
+      $this->assertContains($rid, $account->getRoles());
     }
     else {
-      $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.');
+      $this->assertNotContains($rid, $account->getRoles());

Had to read the original code like three times. This is much better.

Committed the backport to 8.9.x and 8.8.x. Thanks! Sorry for the brief confusion; I was looking at too many different issues at once.

Status: Fixed » Closed (fixed)

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