Problem/Motivation

Wherever possible, use specific PHPUnit default assertions, that also provide standard messaging in case of assertion failure. PHPUnit's list @ https://phpunit.readthedocs.io/en/8.5/assertions.html is the best starting point to understand what default PHPUnit provides.

However, beware of PHPUnit's own deprecations -- the most accurate list is PHPUnit 9 at the moment, https://phpunit.readthedocs.io/en/9.1/assertions.html. Drupal does not support PHPUnit 9 yet, but has already removed usage of methods deprecated in PHPUnit 8 in #3110543: [meta] Support PHPUnit 9 in Drupal 9.

For example: assertSame(*, count()) can be replaced by assertCount().

Issues open:

Done:

Proposed resolution

Add child issues for each case and replace them.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

jungle created an issue. See original summary.

mondrake’s picture

There's a bunch of calls like

 $this->assertTrue(strpos(strtolower(Html::decodeEntities($haystack)), $needle) === FALSE, $message, $group);
$this->assertNotIdentical(FALSE, strpos($query, $expected), 'The flattened query contains the comment string.');
 $this->assertNotIdentical(strpos($rendered_js, $expected_1), FALSE, 'Rendered external JavaScript with correct defer and random attributes.');

These are hardly readable with all the double negations etc etc.

Now we can use

assertStringContainsString

or

assertStringNotContainsString

and the assertStringContainsStringIgnoringCase companions and get rid of this mindblowing madness

EDIT: regex

/assert.*\(.*strpos/
dww’s picture

mondrake’s picture

Other interesting regexes for potential issues:

=.*this->assert(?!Session)(Equal|True|NotRe|Resp): assignment of return values of assertions (possible in Simpletest, do not make sense in PHPUnit and likely signal conditional code)

>assert.*\(.*is_.*\(: possibly replaced by assertNull, assertIsObject, assertIsString, etc.

>assert(?:True|False)\(.*(?:==|===): possibly replaced by assertEquals, assertSame

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
longwave’s picture

Issue summary: View changes
longwave’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

jungle’s picture

Filed #3131343: Replace assertions involving calls to in_array() with assertContains()/assertNotContains() , not sure the suggested ones have better readability than the originals.

mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
dww’s picture

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes

6 is-ish ones filed.

jungle’s picture

In addition to #34, it's unnecessary to file new issues for the following as I checked, feel free to double check.

  1. assertIsBool()
  2. assertIsFloat()
  3. assertIsInt()
  4. assertIsScalar()
  5. assertIsIterable()

Reference: Implement specialized alternatives to assertInternalType() and assertNotInternalType() #3368

longwave’s picture

Issue summary: View changes
dww’s picture

Based on a lengthy Slack thread this morning, I just opened #3131946: [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages as a related (though independent) effort. Please discuss there. Thanks!

quietone’s picture

Refresshing th IS.

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
longwave’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
longwave’s picture

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
daffie’s picture

Issue tags: +Needs documentation

What I am missing here is a guide for which kind of assertion to use. When I now write a test it is to me very confusing which kind of assertion is should use.

jungle’s picture

#64, good suggestion!

  1. Add PHPUnit Coding standards to https://www.drupal.org/docs/develop/standards?
  2. Or add PHPUnit best practices under https://www.drupal.org/docs/8/testing/phpunit-in-drupal-8 ,
  3. Or add a section to core/tests/README.md?
  4. Or add examples to the phpunit_example module of the examples project?
  5. Or mixed with the above 4?
mondrake’s picture

@daffie re #64, I find PHPUnit's list https://phpunit.readthedocs.io/en/8.5/assertions.html the best starting point to understand what default PHPUnit provides. Using those when possible it's the right choice IMHO.

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes

Updated IS for #66

mondrake’s picture

Issue summary: View changes

More IS updates

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes

Added a kanban board for this meta issue

See: https://contribkanban.com/node-board/cb78a137-ce0b-4092-8a20-62fd088ec27f

Everyone can view and edit it.

Right now, 1 Postponed, 2 NW, 5 needs review and 1 RTBC'd, 22 fixed, thanks, everyone!

mondrake’s picture

Issue summary: View changes

#77 is great, I think the link to the kanban deserves better visibility, done here.

mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

mondrake’s picture

Issue summary: View changes

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.

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes

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.

mondrake’s picture

Issue summary: View changes

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Issue summary: View changes

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Status: Active » Reviewed & tested by the community

Time to call this done? If there are other optimizations possible, phpstan-phpunit will probably spot them and we will open individual issues.

quietone’s picture

Status: Reviewed & tested by the community » Fixed

Yes, I agree.

Thanks for keeping this on track and to everyone who worked on the child issues!

Status: Fixed » Closed (fixed)

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

quietone’s picture

Issue tags: -Needs documentation

This is tagged for documentation, #64. Using the advice in #66, I added a link to the list of assertions to PHPUnit in Drupal, which was the suggested place in #65 point 2.