Problem/Motivation

Per title. Regex for searching: >assert(Sa|Eq|Id|Tr|Fa).*\((count\(|.*, count\()

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Working on this.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Active » Needs review
FileSize
56.84 KB

Let's see. Each case is kind of its own, here. In general, I made comparison stricter using assertSame when both expected and actual use a count(). In such cases I preferred to keep the comparison rather than converting to assertCount and then have the expected value in a count(). Readibility, but I guess it can be seen as a matter of preference.

Status: Needs review » Needs work

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

mondrake’s picture

Status: Needs work » Needs review
FileSize
57.22 KB
4.63 KB

Addressing failures.

jungle’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Thanks, @mondrake! I was about to review this, but the patch did not apply on my local.

nitesh624’s picture

Assigned: Unassigned » nitesh624

Assigning to me. I will be working on this

mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

@jungle the patch is applying, and tests run on DrupalCI for 9.1.x. Maybe your git HEAD was not clean?

jungle’s picture

Status: Needs review » Needs work

Sorry! @mondrake! NW for 4 coding standards messages, see https://www.drupal.org/pift-ci-job/1697536

And it looks like that it's hard to check whether there are remaining ones to fix.

mondrake’s picture

Status: Needs work » Needs review
FileSize
57.02 KB
2.68 KB

Thanks @jungle. Fixed CS. It's good if it's hard to find more to fix... it means we did most - and if others pop up fine, will probably deserve their own issue.

nitesh624’s picture

Assigned: nitesh624 » Unassigned
nitesh624’s picture

Status: Needs review » Reviewed & tested by the community

#10 patch applied successfully on my local and looks good to me. RTBC1 from my side

xjm’s picture

Status: Reviewed & tested by the community » Needs review

Thanks @mondrake.

@nitesh624, The automated testing infrastructure tells us whether the patch applies, so we do not need people to review that. It is also not sufficient criteria for the issue to be marked "Reviewed and Tested by the Community". "Looks good to me" also isn't enough information for a review credit. Next time, I'd suggest specifically describing your thoughts about the patch, including exactly what you did to review it and what you think of the kinds of changes in the patch. Thanks!

So I'm "hmmm" on this change set.

  1. In some places, we're changing stuff from assertEquals() to assertSame().
  2. In other places, we're changing things to assertEquals().
  3. And in the rest, we're actually using assertCount().

Can we clarify the "why" in each of the three cases cases?

I do see for point 1 that assertCount() is not sufficient for cases where we're comparing the count of two items. In those places it looks like an assertSameCount() method might be nice, actually. What do you think? We could split the cases where we're comparing two counts out into their own patch to explore that.

mondrake’s picture

Status: Needs review » Postponed

Introducing an assertSameCount would be a good idea, actually. The correct way would be to

  1. Introduce an implementation of PHPUnit Constraint to count both expected and actual, à la #3134671: Introduce a PHPUnit Constraint to check existence of a response header .
  2. Add two wrapper methods into a trait somewhere, say assertSameCount and assertNotSameCount, that invoke the Constraint, à la #3133355: Introduce PHPUnit-compliant WebAssert::responseHeaderExists() method, and its negative.
  3. Benefit of the two new methods.

Personally I will hold until those two issues are in, since they will make a reference to all future implementations. Would be lovely to see what @longwave thinks of this.

Also, see #3139405: Replace usages of AssertLegacyTrait::assert(No)UniqueText, that is deprecated.

longwave’s picture

Not sure whether we really need assertSameCount() or not. Would we even need a custom constraint or could we just wrap assertSame? And if we did add it, would people discover and use it in the future, or would we just be refactoring for the sake of it and then it would never get used again? The header one is useful because there is no existing easy method for it; assertSameCount() can be emulated by writing the assertion slightly differently, as already seen here.

Also, some of these cases feel like the tests could be refactored to be simpler, but doing that here is out of scope:

  1. +++ b/core/modules/book/tests/src/Functional/BookTest.php
    @@ -305,13 +305,13 @@ public function testGetTableOfContents() {
    -    $this->assertEqual(count($options), count($expected_nids));
    +    $this->assertSame(count($expected_nids), count($options));
         $diff = array_diff($expected_nids, array_keys($options));
         $this->assertTrue(empty($diff), 'Found all expected option keys');
    

    This seems like it would be better done as a single assertEquals().

  2. +++ b/core/modules/book/tests/src/Functional/BookTest.php
    @@ -305,13 +305,13 @@ public function testGetTableOfContents() {
    -    $this->assertEqual(count($options), count($expected_nids));
    +    $this->assertSame(count($expected_nids), count($options));
         $diff = array_diff($expected_nids, array_keys($options));
         $this->assertTrue(empty($diff), 'Found all expected option keys after excluding Node 3');
    

    Same.

  3. +++ b/core/modules/views/tests/src/Kernel/Handler/SortDateTest.php
    @@ -190,7 +190,7 @@ public function testDateOrdering() {
    -        $this->assertEqual(count($this->dataSet()), count($view->result), 'The number of returned rows match.');
    +        $this->assertSame(count($this->dataSet()), count($view->result), 'The number of returned rows match.');
             $this->assertIdenticalResultset($view, $this->expectedResultSet($granularity, $reverse), [
    

    Is the count check redundant here? It feels like IdenticalResultSet should also check the counts!

  4. +++ b/core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php
    @@ -446,7 +446,7 @@ protected function assertMenuLink($id, array $expected_properties, array $parent
         // We need both these checks since the 2nd will pass if there are extra
         // IDs loaded in $found_children.
    -    $this->assertEqual(count($children), count($found_children), "Found expected number of children for $id");
    +    $this->assertSame(count($children), count($found_children), "Found expected number of children for $id");
         $this->assertEqual(array_intersect($children, $found_children), $children, 'Child IDs match');
    

    Again, can this just be done as a single assertEquals()?

mondrake’s picture

Status: Postponed » Needs review
FileSize
3.46 KB
57.26 KB

@longwave thank you. In fact I see #15 in scope here: after #3126965: [backport] Replace assert* involving count() and an integer literal with assertCount() and #3128814: Replace assert* involving count() and an equality operator with assertCount() that did the bulk of the changes, this is to try and address the longtail. So, not just mere string replacements but rather thought over refactors. Then whether this one has to be split further, let's discuss.

I take yours as a -1 for assert(Not)SameCount methods, fair enough.

Done all of #15 points here.

longwave’s picture

I would call it a -0.5 for assertSameCount, I might be convinced the other way but I am leaning towards "no" for now :)

mondrake’s picture

#17 Ha! It should be an int, not a float :P

Status: Needs review » Needs work

The last submitted patch, 16: 3135538-16.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
776 bytes
57.27 KB

Addressing the test failure of #16 using assertEqualsCanonicalizing that presorts the arrays for comparison.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

All these changes look sensible now.

catch’s picture

Status: Reviewed & tested by the community » Fixed

I've opened #3156396: Use assertSameSize() to check same size of two countable variables for assertSameCount() - IMO this would be fine to add, but also using more appropriate assertions in this patch seems good in its own right too. Don't think it hurts to do this in two steps.

Committed d1033a9 and pushed to 9.1.x. Thanks!

  • catch committed a050029 on 9.1.x
    Issue #3135538 by mondrake, longwave, jungle: Replace remaining assert*...

Status: Fixed » Closed (fixed)

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