WordPress.org

Make WordPress Core

Opened 9 months ago

Closed 3 months ago

#42065 closed enhancement (fixed)

Implement `assertNotWPError` in test suite

Reported by: danielbachhuber Owned by: johnbillion
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch
Focuses: Cc:

Description

WP_Error often returns useful information about why a function is failed.

However, an assertion like $this->assertNotInstanceOf( 'WP_Error', $file ); isn't helpful to the test runner because it doesn't communicate the information conveyed by the WP_Error object.

Currently, someone running the test has to add some var_dump() debug (or similar) to read the contents of the returned error object.

It would be helpful to have an assertNotWPError assertion that communicated the contents of the WP_Error object when it actually is one.

Inspired by #42064

Attachments (1)

42065.diff (14.3 KB) - added by birgire 3 months ago.

Download all attachments as: .zip

Change History (7)

#1 @johnbillion
9 months ago

It's already there but there are a bunch of places in core that don't use it.

#2 @danielbachhuber
9 months ago

@johnbillion Should've grepped. Can we clean those up?

#3 @johnbillion
9 months ago

  • Keywords needs-patch added
  • Summary changed from Introduce `assertNotWPError` in test suite to Implement `assertNotWPError` in test suite

Yep definitely.

@birgire
3 months ago

#4 @birgire
3 months ago

  • Keywords has-patch added; needs-patch removed

42065.diff changes:

$this->assertNotInstanceOf( 'WP_Error', ... )

to

$this->assertNotWPError( ... )

in the test files:

  • tests/phpunit/tests/post/formats.php
  • tests/phpunit/tests/customize/manager.php
  • tests/phpunit/tests/customize/nav-menu-setting.php
  • tests/phpunit/tests/image/editorImagick.php
  • tests/phpunit/tests/image/editorGd.php
  • tests/phpunit/tests/image/functions.php
  • tests/phpunit/tests/admin/includesPost.php
  • tests/phpunit/tests/ajax/CustomizeManager.php
  • tests/phpunit/includes/testcase-rest-post-type-controller.php

This is the method's definition:

function assertNotWPError( $actual, $message = '' ) {
	if ( is_wp_error( $actual ) && '' === $message ) {
		$message = $actual->get_error_message();
	}
	$this->assertNotInstanceOf( 'WP_Error', $actual, $message );
}

where we see how the WP_Error error message is passed on to the $message argument of the assertNotInstandeOf method.

Last edited 3 months ago by birgire (previous) (diff)

#5 @johnbillion
3 months ago

  • Milestone changed from Future Release to 5.0
  • Owner set to johnbillion
  • Status changed from new to reviewing

#6 @johnbillion
3 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 42863:

Build/Test Tools: Implement assertNotWPError() in appropriate places in the test suite.

Props birgire

Fixes #42065

Note: See TracTickets for help on using tickets.