Make WordPress Core


Ignore:
Timestamp:
12/28/2022 02:07:16 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to creating and updating objects in factory classes.

In various unit test factory classes, some of the create_object() and update_object() methods returned a WP_Error object on failure, while a few others were documented to do so, but did not in practice, instead returning the value 0 or false, or not accounting for a failure at all.

This commit aims to handle this in a consistent way by updating the methods to always return the object ID on success and a WP_Error object on failure.

Includes:

  • Updating and correcting the relevant documentation parts.
  • Adding missing documentation and @since tags in some classes.
  • Renaming some variables to clarify that it is the object ID which is passed around, not the object itself.

Follow-up to [760/tests], [838/tests], [922/tests], [948/tests], [985/tests], [27178], [32659], [34855], [37563], [40968], [44497], [46262].

See #56793.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php

    r46985 r55019  
    1010
    1111    /**
    12      * Creates a new factory, which will create objects of a specific Thing
    13      *
    14      * @param object $factory Global factory that can be used to create other objects on the system
    15      * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    16      * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    17      * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
     12     * Creates a new factory, which will create objects of a specific Thing.
     13     *
     14     * @since UT (3.7.0)
     15     *
     16     * @param object $factory                       Global factory that can be used to create other objects
     17     *                                              on the system.
     18     * @param array $default_generation_definitions Defines what default values should the properties
     19     *                                              of the object have. The default values can be generators --
     20     *                                              an object with the next() method.
     21     *                                              There are some default generators:
     22     *                                               - {@link WP_UnitTest_Generator_Sequence}
     23     *                                               - {@link WP_UnitTest_Generator_Locale_Name}
     24     *                                               - {@link WP_UnitTest_Factory_Callback_After_Create}
    1825     */
    1926    public function __construct( $factory, $default_generation_definitions = array() ) {
     
    2330
    2431    /**
    25      * Creates an object.
     32     * Creates an object and returns its ID.
     33     *
     34     * @since UT (3.7.0)
    2635     *
    2736     * @param array $args The arguments.
    2837     *
    29      * @return mixed The result. Can be anything.
     38     * @return int|WP_Error The object ID on success, WP_Error object on failure.
    3039     */
    3140    abstract public function create_object( $args );
     
    3443     * Updates an existing object.
    3544     *
    36      * @param int   $object The object ID.
    37      * @param array $fields The values to update.
    38      *
    39      * @return mixed The result. Can be anything.
    40      */
    41     abstract public function update_object( $object, $fields );
    42 
    43     /**
    44      * Creates an object.
    45      *
    46      * @param array $args                   Optional. The arguments for the object to create. Default is empty array.
    47      * @param null  $generation_definitions Optional. The default values for the object. Default is null.
    48      *
    49      * @return mixed The result. Can be anything.
     45     * @since UT (3.7.0)
     46     *
     47     * @param int   $object_id The object ID.
     48     * @param array $fields    The values to update.
     49     *
     50     * @return int|WP_Error The object ID on success, WP_Error object on failure.
     51     */
     52    abstract public function update_object( $object_id, $fields );
     53
     54    /**
     55     * Creates an object and returns its ID.
     56     *
     57     * @since UT (3.7.0)
     58     *
     59     * @param array $args                   Optional. The arguments for the object to create.
     60     *                                      Default empty array.
     61     * @param null  $generation_definitions Optional. The default values for the object.
     62     *                                      Default null.
     63     *
     64     * @return int|WP_Error The object ID on success, WP_Error object on failure.
    5065     */
    5166    public function create( $args = array(), $generation_definitions = null ) {
     
    5570
    5671        $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    57         $created        = $this->create_object( $generated_args );
    58         if ( ! $created || is_wp_error( $created ) ) {
    59             return $created;
     72        $object_id      = $this->create_object( $generated_args );
     73
     74        if ( ! $object_id || is_wp_error( $object_id ) ) {
     75            return $object_id;
    6076        }
    6177
    6278        if ( $callbacks ) {
    63             $updated_fields = $this->apply_callbacks( $callbacks, $created );
    64             $save_result    = $this->update_object( $created, $updated_fields );
     79            $updated_fields = $this->apply_callbacks( $callbacks, $object_id );
     80            $save_result    = $this->update_object( $object_id, $updated_fields );
     81
    6582            if ( ! $save_result || is_wp_error( $save_result ) ) {
    6683                return $save_result;
    6784            }
    6885        }
    69         return $created;
    70     }
    71 
    72     /**
    73      * Creates an object and returns its object.
    74      *
    75      * @param array $args                   Optional. The arguments for the object to create. Default is empty array.
    76      * @param null  $generation_definitions Optional. The default values for the object. Default is null.
    77      *
    78      * @return mixed The created object. Can be anything.
     86
     87        return $object_id;
     88    }
     89
     90    /**
     91     * Creates and returns an object.
     92     *
     93     * @since UT (3.7.0)
     94     *
     95     * @param array $args                   Optional. The arguments for the object to create.
     96     *                                      Default empty array.
     97     * @param null  $generation_definitions Optional. The default values for the object.
     98     *                                      Default null.
     99     *
     100     * @return mixed The created object. Can be anything. WP_Error object on failure.
    79101     */
    80102    public function create_and_get( $args = array(), $generation_definitions = null ) {
     
    91113     * Retrieves an object by ID.
    92114     *
     115     * @since UT (3.7.0)
     116     *
    93117     * @param int $object_id The object ID.
    94118     *
     
    100124     * Creates multiple objects.
    101125     *
     126     * @since UT (3.7.0)
     127     *
    102128     * @param int   $count                  Amount of objects to create.
    103      * @param array $args                   Optional. The arguments for the object to create. Default is empty array.
    104      * @param null  $generation_definitions Optional. The default values for the object. Default is null.
     129     * @param array $args                   Optional. The arguments for the object to create.
     130     *                                      Default empty array.
     131     * @param null  $generation_definitions Optional. The default values for the object.
     132     *                                      Default null.
    105133     *
    106134     * @return array
     
    108136    public function create_many( $count, $args = array(), $generation_definitions = null ) {
    109137        $results = array();
     138
    110139        for ( $i = 0; $i < $count; $i++ ) {
    111140            $results[] = $this->create( $args, $generation_definitions );
    112141        }
     142
    113143        return $results;
    114144    }
     
    118148     * possibly set callbacks on it.
    119149     *
    120      * @param array       $args                   Optional. The arguments to combine with defaults. Default is empty array.
    121      * @param array|null  $generation_definitions Optional. The defaults. Default is null.
    122      * @param array|null  $callbacks              Optional. Array with callbacks to apply on the fields. Default is null.
     150     * @since UT (3.7.0)
     151     *
     152     * @param array       $args                   Optional. The arguments to combine with defaults.
     153     *                                            Default empty array.
     154     * @param array|null  $generation_definitions Optional. The defaults. Default null.
     155     * @param array|null  $callbacks              Optional. Array with callbacks to apply on the fields.
     156     *                                            Default null.
    123157     *
    124158     * @return array|WP_Error Combined array on success. WP_Error when default value is incorrent.
     
    145179                    $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr );
    146180                } else {
    147                     return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
     181                    return new WP_Error(
     182                        'invalid_argument',
     183                        'Factory default value should be either a scalar or an generator object.'
     184                    );
    148185                }
    149186            }
     
    157194     * Applies the callbacks on the created object.
    158195     *
     196     * @since UT (3.7.0)
     197     *
    159198     * @param WP_UnitTest_Factory_Callback_After_Create[] $callbacks Array with callback functions.
    160      * @param mixed                                       $created   The object to apply callbacks for.
     199     * @param int                                         $object_id ID of the object to apply callbacks for.
    161200     *
    162201     * @return array The altered fields.
    163202     */
    164     public function apply_callbacks( $callbacks, $created ) {
     203    public function apply_callbacks( $callbacks, $object_id ) {
    165204        $updated_fields = array();
    166205
    167206        foreach ( $callbacks as $field_name => $generator ) {
    168             $updated_fields[ $field_name ] = $generator->call( $created );
    169         }
     207            $updated_fields[ $field_name ] = $generator->call( $object_id );
     208        }
     209
    170210        return $updated_fields;
    171211    }
     
    173213    /**
    174214     * Instantiates a callback objects for the given function name.
     215     *
     216     * @since UT (3.7.0)
    175217     *
    176218     * @param string $function The callback function.
     
    184226    /**
    185227     * Adds slashes to the given value.
     228     *
     229     * @since UT (3.7.0)
    186230     *
    187231     * @param array|object|string|mixed $value The value to add slashes to.
Note: See TracChangeset for help on using the changeset viewer.