Problem/Motivation

Follow-up from #3135538: Replace remaining assert* involving use of count() where relevant. In some cases we are doing assertSame(count($foo), count($bar)), we could instead use assertSameSize($foo, $bar).

See the API docs for assertSameSize()

Original issue summary, which suggested creating a new assertion method

Follow-up from [#3135538. IN some cases we are doing assertSame(count($foo), count($bar)), we could add a custom assertion to make this assertSameCount($foo, $bar).

Comments

catch created an issue. See original summary.

mondrake’s picture

mondrake’s picture

pavnish’s picture

Status: Active » Needs review
StatusFileSize
new22.87 KB

@catch and @mondrake Shall you please review this patch .All the usage of "assertSame" with counts replaced by the custom function
"AssertSameCount".

Thanks
Pavnish

mondrake’s picture

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

Thanks for starting this!

IMHO it's good to have that method in a trait, but that trait should be more 'general', and imported in the base test classes - UnitTestBase, KernelTestBase, BrowserTestBase, etc.

We need test coverage for the new method itself.

An elegant solution would be to implement a PHPUnit Constraint for the check, à la #3134671: Introduce a PHPUnit Constraint to check existence of a response header , instead of falling to assertSame.

pavnish’s picture

Status: Needs work » Needs review
StatusFileSize
new15.01 KB
new20.79 KB

@mondrake
Thanks for your valuable feedback and suggestion.
I have ported the patch along with the your suggestion please review this patch.
Please suggest if any changes required it would be appreciated.

Thanks
Pavnish

mondrake’s picture

Status: Needs review » Needs work

Thanks.

  1. +++ b/core/lib/Drupal/Core/Test/AssertSameTrait.php
    @@ -0,0 +1,23 @@
    +<?php
    +
    +namespace Drupal\Core\Test;
    +
    +/**
    + * Provides methods for AssertSame.
    + */
    +trait AssertSameTrait {
    

    Let's move this under Drupal/TestTools and tentatively call it AssertionTrait. This way we can add more assert methods in the trait when the need comes.

  2. +++ b/core/lib/Drupal/Core/Test/AssertSameTrait.php
    @@ -0,0 +1,23 @@
    +  /**
    +   * Asserts that two variables have same number of values.
    +   *
    +   * @param array/object $expected
    +   * @param array/object $actual
    +   * @param string $message
    +   *
    +   */
    +  protected function assertSameCount($expected, $actual, string $message = '') {
    +    return $this->assertSame(count($expected), count($actual), $message);
    +  }
    +
    
    +++ b/core/tests/Drupal/KernelTests/Core/Test/AssertSameTraitTest.php
    @@ -0,0 +1,29 @@
    +  /**
    +   * Tests that the Assert same trait functions.
    +   */
    +  public function testAssertSameTrait() {
    +    $expected = ['expected'];
    +    $actual = ['actual'];
    +    // Should return Null.
    +    $assertSameStatus = $this->assertSameCount($expected,$actual);
    +    $this->assertNull($assertSameStatus, 'Expected and Actual have same count.' );
    +  }
    +
    

    We need to look at how PHPUnit assert methods are written, ussing Constraint and Assert classes. Drupal\Tests\WebAssert::responseHeaderExists could be a reference point. Also, Drupal\FunctionalTests\WebAssertTest shows how to test a custom assertion by intercepting the failure exception.

  3. In addition, we should also implement the symmetrical method assertNotSameCount.
pavnish’s picture

Status: Needs work » Needs review
StatusFileSize
new6.48 KB
new21.15 KB

@mondrake
Thanks for the feedback and suggestions.
Point #7.1 and #7.2 addressed in this patch please review.
#7.3 :I think we should not work on assertNotSameCount method.I have tried to found the occurrence of assertNotSame with count Ex.
assertNotSame(count($foo), count($bar)) but i did not get any result.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tr’s picture

Do we really need to invent a new assertion method? What about assertSameSize(), which already exists?

mondrake’s picture

#11 interesting! This assertions is there, but totally un-documented in PHPUnit manual. It happened already once to me: https://github.com/sebastianbergmann/phpunit-documentation-english/issue...

longwave’s picture

mondrake’s picture

Shall we rescope this issue to use ::assertSameSize()?

tr’s picture

Issue tags: -Needs tests
StatusFileSize
new19.86 KB

Here's a patch to use assertSameSize() when comparing the count() of two arrays (or Countable or Traversable objects).

I removed the "Needs tests" tag because we're not adding new code anymore, just replacing the assertions.

Status: Needs review » Needs work

The last submitted patch, 15: 3156396-15-assert-same-size.patch, failed testing. View results

tr’s picture

Title: Add assertSameCount() » Use assertSameSize() when comparing the count() of two arrays
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new20.63 KB
new1.31 KB

I made an editing error in patch #15, resulting in a syntax error, and I also found one more assert that could be changed to use assertSameSize(). Here is a new patch and interdiff.

tr’s picture

StatusFileSize
new19.86 KB
new697 bytes

Please ignore #17, not my best work. That "one more assert that could be changed" wasn't in a test, so it broke everything.

Here's another patch, which is the same as #15 but with the syntax error fixed.

mondrake’s picture

Title: Use assertSameSize() when comparing the count() of two arrays » Use assertSameSize() to check same size of two countable variables
Category: Feature request » Task
StatusFileSize
new33.33 KB
new14.19 KB

There's a few more that could benefit readibility by being converted.

jungle’s picture

assertSameSize() exists since phpunit 3.6.0RC1, per the commit

It's ok back-porting down to 9.1.x or even 8.x, not 100% sure if there are more to do, so far so good, RTBC+1. Thanks!

longwave’s picture

Found two more that I think can be converted by searching for assert.*count.*count, a lot of false positives to sift through though!

core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFilterTest.php:    $this->assertCount($blocks_count, $visible_rows);
core/modules/migrate/tests/src/Unit/process/SubProcessTest.php:    $this->assertCount(count($process_configuration['process']), $new_value[42]);
mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs review » Needs work

On this

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
StatusFileSize
new35.39 KB
new2.07 KB

Addressed #21, thanks

longwave’s picture

Status: Needs review » Reviewed & tested by the community

There are perhaps more that we could convert, but they are going to be hard to find. This at least gives hints to others that read our tests that this method is available, so RTBC from me.

  • catch committed 1d5e219 on 9.3.x
    Issue #3156396 by TR, pavnish, mondrake, longwave, catch, jungle: Use...
catch’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!

  • catch committed 6123c0e on 9.2.x
    Issue #3156396 by TR, pavnish, mondrake, longwave, catch, jungle: Use...

Status: Fixed » Closed (fixed)

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