Make WordPress Core

Changeset 53521


Ignore:
Timestamp:
06/18/2022 09:28:39 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add a helper method for for creating named data providers in WP_UnitTestCase_Base.

This introduces a new test helper function which allows for turning a single-level array containing text strings into a data provider with named data sets, where the value of the data set will also be used as the name of the data set.

The function contains safeguards to ensure that it is only used with data compatible with this principle and will throw generic PHP exceptions when the data is incompatible. These type of exceptions will be displayed before the tests even start running and will stop the test run when they occur.

While generally speaking, all test cases should extend the base WP_UnitTestCase_Base class, this is still made a public static method to allow for a test, which by exception directly extends the PHPUnit base TestCase or the PHPUnit_Adapter_TestCase, to also be able to use this method.

Typical usage of this method:

public function data_provider_for_test_name() {
	$array = array(
		'value1',
		'value2',
	);

	return $this->text_array_to_dataprovider( $array );
}

The returned result will look like:

array(
	'value1' => array( 'value1' ),
	'value2' => array( 'value2' ),
)

Props jrf, hellofromTonya, adamsilverstein.
See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r53467 r53521  
    876876            $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty.' );
    877877        }
     878    }
     879
     880    /**
     881     * Helper function to convert a single-level array containing text strings to a named data provider.
     882     *
     883     * The value of the data set will also be used as the name of the data set.
     884     *
     885     * Typical usage of this method:
     886     *
     887     *     public function data_provider_for_test_name() {
     888     *         $array = array(
     889     *             'value1',
     890     *             'value2',
     891     *         );
     892     *
     893     *         return $this->text_array_to_dataprovider( $array );
     894     *     }
     895     *
     896     * The returned result will look like:
     897     *
     898     *     array(
     899     *         'value1' => array( 'value1' ),
     900     *         'value2' => array( 'value2' ),
     901     *     )
     902     *
     903     * @since 6.1.0
     904     *
     905     * @param array $input Input array.
     906     * @return array Array which is usable as a test data provider with named data sets.
     907     */
     908    public static function text_array_to_dataprovider( $input ) {
     909        $data = array();
     910
     911        foreach ( $input as $value ) {
     912            if ( ! is_string( $value ) ) {
     913                throw new Exception(
     914                    'All values in the input array should be text strings. Fix the input data.'
     915                );
     916            }
     917
     918            if ( isset( $data[ $value ] ) ) {
     919                throw new Exception(
     920                    "Attempting to add a duplicate data set for value $value to the data provider. Fix the input data."
     921                );
     922            }
     923
     924            $data[ $value ] = array( $value );
     925        }
     926
     927        return $data;
    878928    }
    879929
Note: See TracChangeset for help on using the changeset viewer.