Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#42065 closed enhancement (fixed)

Implement `assertNotWPError` in test suite

Reported by: danielbachhuber's profile danielbachhuber Owned by: johnbillion's profile johnbillion
Milestone: 5.1 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 7 years ago.

Download all attachments as: .zip

Change History (8)

#1 @johnbillion
7 years ago

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

#2 @danielbachhuber
7 years ago

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

#3 @johnbillion
7 years ago

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

Yep definitely.

@birgire
7 years ago

#4 @birgire
7 years 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 7 years ago by birgire (previous) (diff)

#5 @johnbillion
7 years ago

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

#6 @johnbillion
7 years 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

#7 @jorbin
6 years ago

  • Milestone changed from 5.0 to 5.1
Note: See TracTickets for help on using tickets.