Make WordPress Core

Changeset 55019


Ignore:
Timestamp:
12/28/2022 02:07:16 PM (18 months 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.

Location:
trunk/tests/phpunit
Files:
12 edited

Legend:

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

    r55016 r55019  
    15231523     *
    15241524     * @since 4.4.0
     1525     * @since 6.2.0 Returns a WP_Error object on failure.
    15251526     *
    15261527     * @param array $upload         Array of information about the uploaded file, provided by wp_upload_bits().
    15271528     * @param int   $parent_post_id Optional. Parent post ID.
    1528      * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     1529     * @return int|WP_Error The attachment ID on success, WP_Error object on failure.
    15291530     */
    15301531    public function _make_attachment( $upload, $parent_post_id = 0 ) {
     
    15481549        );
    15491550
    1550         $id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
    1551         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    1552         return $id;
     1551        $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true );
     1552
     1553        if ( is_wp_error( $attachment_id ) ) {
     1554            return $attachment_id;
     1555        }
     1556
     1557        wp_update_attachment_metadata(
     1558            $attachment_id,
     1559            wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
     1560        );
     1561
     1562        return $attachment_id;
    15531563    }
    15541564
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php

    r46986 r55019  
    1111     * WP_UnitTest_Factory_Callback_After_Create constructor.
    1212     *
     13     * @since UT (3.7.0)
     14     *
    1315     * @param callable $callback A callback function.
    1416     */
     
    2022     * Calls the set callback on a given object.
    2123     *
    22      * @param mixed $object The object to apply the callback on.
     24     * @since UT (3.7.0)
    2325     *
    24      * @return mixed The possibly altered object.
     26     * @param int $object_id ID of the object to apply the callback on.
     27     *
     28     * @return mixed Updated object field.
    2529     */
    26     public function call( $object ) {
    27         return call_user_func( $this->callback, $object );
     30    public function call( $object_id ) {
     31        return call_user_func( $this->callback, $object_id );
    2832    }
    2933}
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php

    r47122 r55019  
    11<?php
    22
     3/**
     4 * Unit test factory for attachments.
     5 *
     6 * Note: The below @method notations are defined solely for the benefit of IDEs,
     7 * as a way to indicate expected return values from the given factory methods.
     8 *
     9 * @method int|WP_Error     create( $args = array(), $generation_definitions = null )
     10 * @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
     12 */
    313class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    414
    515    /**
    616     * Create an attachment fixture.
     17     *
     18     * @since UT (3.7.0)
     19     * @since 6.2.0 Returns a WP_Error object on failure.
    720     *
    821     * @param array $args {
     
    1528     * @param array $legacy_args   Deprecated.
    1629     *
    17      * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     30     * @return int|WP_Error The attachment ID on success, WP_Error object on failure.
    1831     */
    1932    public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
     
    3447        );
    3548
    36         return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
     49        return wp_insert_attachment( $r, $r['file'], $r['post_parent'], true );
    3750    }
    3851
     
    4053     * Saves an attachment.
    4154     *
     55     * @since 4.4.0
     56     * @since 6.2.0 Returns a WP_Error object on failure.
     57     *
    4258     * @param string $file   The file name to create attachment object for.
    4359     * @param int    $parent ID of the post to attach the file to.
    4460     *
    45      * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     61     * @return int|WP_Error The attachment ID on success, WP_Error object on failure.
    4662     */
    4763    public function create_upload_object( $file, $parent = 0 ) {
     
    6985
    7086        // Save the data.
    71         $id = wp_insert_attachment( $attachment, $upload['file'], $parent );
    72         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     87        $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent, true );
    7388
    74         return $id;
     89        if ( is_wp_error( $attachment_id ) ) {
     90            return $attachment_id;
     91        }
     92
     93        wp_update_attachment_metadata(
     94            $attachment_id,
     95            wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
     96        );
     97
     98        return $attachment_id;
    7599    }
    76100}
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php

    r48121 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Site create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error    create( $args = array(), $generation_definitions = null )
     10 * @method WP_Site|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-bookmark.php

    r46586 r55019  
    99 * @since 4.6.0
    1010 *
    11  * @method int create( $args = array(), $generation_definitions = null )
    12  * @method object create_and_get( $args = array(), $generation_definitions = null )
    13  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     11 * @method int|WP_Error    create( $args = array(), $generation_definitions = null )
     12 * @method object|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     13 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
    1414 */
    1515class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
     
    2323    }
    2424
     25    /**
     26     * Creates a link object.
     27     *
     28     * @since 4.6.0
     29     * @since 6.2.0 Returns a WP_Error object on failure.
     30     *
     31     * @param array $args Arguments for the link object.
     32     *
     33     * @return int|WP_Error The link ID on success, WP_Error object on failure.
     34     */
    2535    public function create_object( $args ) {
    26         return wp_insert_link( $args );
     36        return wp_insert_link( $args, true );
    2737    }
    2838
     39    /**
     40     * Updates a link object.
     41     *
     42     * @since 4.6.0
     43     * @since 6.2.0 Returns a WP_Error object on failure.
     44     *
     45     * @param int   $link_id ID of the link to update.
     46     * @param array $fields  The fields to update.
     47     *
     48     * @return int|WP_Error The link ID on success, WP_Error object on failure.
     49     */
    2950    public function update_object( $link_id, $fields ) {
    3051        $fields['link_id'] = $link_id;
    31         return wp_update_link( $fields );
     52
     53        $result = wp_update_link( $fields );
     54
     55        if ( 0 === $result ) {
     56            return new WP_Error( 'link_update_error', __( 'Could not update link.' ) );
     57        }
     58
     59        return $result;
    3260    }
    3361
     62    /**
     63     * Retrieves a link by a given ID.
     64     *
     65     * @since 4.6.0
     66     *
     67     * @param int $link_id ID of the link to retrieve.
     68     *
     69     * @return object|null The link object on success, null on failure.
     70     */
    3471    public function get_object_by_id( $link_id ) {
    3572        return get_bookmark( $link_id );
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php

    r47017 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Comment create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error        create( $args = array(), $generation_definitions = null )
     10 * @method WP_Comment|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[]    create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
     
    2626     * Inserts a comment.
    2727     *
     28     * @since UT (3.7.0)
     29     * @since 6.2.0 Returns a WP_Error object on failure.
     30     *
     31     * @global wpdb $wpdb WordPress database abstraction object.
     32     *
    2833     * @param array $args The comment details.
    2934     *
    30      * @return int|false The comment's ID on success, false on failure.
     35     * @return int|WP_Error The comment ID on success, WP_Error object on failure.
    3136     */
    3237    public function create_object( $args ) {
    33         return wp_insert_comment( $this->addslashes_deep( $args ) );
     38        global $wpdb;
     39
     40        $comment_id = wp_insert_comment( $this->addslashes_deep( $args ) );
     41
     42        if ( false === $comment_id ) {
     43            return new WP_Error(
     44                'db_insert_error',
     45                __( 'Could not insert comment into the database.' ),
     46                $wpdb->last_error
     47            );
     48        }
     49
     50        return $comment_id;
    3451    }
    3552
     
    3754     * Updates a comment.
    3855     *
     56     * @since UT (3.7.0)
     57     * @since 6.2.0 Returns a WP_Error object on failure.
     58     *
    3959     * @param int   $comment_id The comment ID.
    4060     * @param array $fields     The comment details.
    4161     *
    42      * @return int The value 1 if the comment was updated, 0 if not updated.
     62     * @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
     63     *                      WP_Error object on failure.
    4364     */
    4465    public function update_object( $comment_id, $fields ) {
    4566        $fields['comment_ID'] = $comment_id;
    46         return wp_update_comment( $this->addslashes_deep( $fields ) );
     67        return wp_update_comment( $this->addslashes_deep( $fields ), true );
    4768    }
    4869
    4970    /**
    5071     * Creates multiple comments on a given post.
     72     *
     73     * @since UT (3.7.0)
    5174     *
    5275     * @param int   $post_id                ID of the post to create comments for.
     
    6588     * Retrieves a comment by a given ID.
    6689     *
     90     * @since UT (3.7.0)
     91     *
    6792     * @param int $comment_id ID of the comment to retrieve.
    6893     *
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php

    r46831 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Network create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error        create( $args = array(), $generation_definitions = null )
     10 * @method WP_Network|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[]    create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
     
    2424    }
    2525
     26    /**
     27     * Creates a network object.
     28     *
     29     * @since 3.9.0
     30     * @since 6.2.0 Returns a WP_Error object on failure.
     31     *
     32     * @param array $args Arguments for the network object.
     33     *
     34     * @return int|WP_Error The network ID on success, WP_Error object on failure.
     35     */
    2636    public function create_object( $args ) {
    2737        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     
    3343        }
    3444
    35         populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
     45        $result = populate_network(
     46            $args['network_id'],
     47            $args['domain'],
     48            $email,
     49            $args['title'],
     50            $args['path'],
     51            $args['subdomain_install']
     52        );
     53
     54        if ( is_wp_error( $result ) ) {
     55            return $result;
     56        }
     57
    3658        return (int) $args['network_id'];
    3759    }
    3860
     61    /**
     62     * Updates a network object. Not implemented.
     63     *
     64     * @since 3.9.0
     65     *
     66     * @param int   $network_id ID of the network to update.
     67     * @param array $fields  The fields to update.
     68     *
     69     * @return void
     70     */
    3971    public function update_object( $network_id, $fields ) {}
    4072
     73    /**
     74     * Retrieves a network by a given ID.
     75     *
     76     * @since 3.9.0
     77     *
     78     * @param int $network_id ID of the network to retrieve.
     79     *
     80     * @return WP_Network|null The network object on success, null on failure.
     81     */
    4182    public function get_object_by_id( $network_id ) {
    4283        return get_network( $network_id );
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php

    r49789 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Post create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error    create( $args = array(), $generation_definitions = null )
     10 * @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
     
    2727     * Creates a post object.
    2828     *
     29     * @since UT (3.7.0)
     30     * @since 6.2.0 Returns a WP_Error object on failure.
     31     *
    2932     * @param array $args Array with elements for the post.
    3033     *
    31      * @return int The post ID on success. The value 0 on failure.
     34     * @return int|WP_Error The post ID on success, WP_Error object on failure.
    3235     */
    3336    public function create_object( $args ) {
    34         return wp_insert_post( $args );
     37        return wp_insert_post( $args, true );
    3538    }
    3639
     
    3841     * Updates an existing post object.
    3942     *
     43     * @since UT (3.7.0)
     44     * @since 6.2.0 Returns a WP_Error object on failure.
     45     *
    4046     * @param int   $post_id ID of the post to update.
    4147     * @param array $fields  Post data.
    4248     *
    43      * @return int The post ID on success. The value 0 on failure.
     49     * @return int|WP_Error The post ID on success, WP_Error object on failure.
    4450     */
    4551    public function update_object( $post_id, $fields ) {
    4652        $fields['ID'] = $post_id;
    47         return wp_update_post( $fields );
     53        return wp_update_post( $fields, true );
    4854    }
    4955
    5056    /**
    5157     * Retrieves a post by a given ID.
     58     *
     59     * @since UT (3.7.0)
    5260     *
    5361     * @param int $post_id ID of the post to retrieve.
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php

    r51298 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_Term create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error          create( $args = array(), $generation_definitions = null )
     10 * @method WP_Term|WP_Error|null create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[]      create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
     
    2929     * Creates a term object.
    3030     *
    31      * @param array $args Array or string of arguments for inserting a term.
     31     * @since UT (3.7.0)
    3232     *
    33      * @return array|WP_Error
     33     * @param array $args Array of arguments for inserting a term.
     34     *
     35     * @return int|WP_Error The term ID on success, WP_Error object on failure.
    3436     */
    3537    public function create_object( $args ) {
    3638        $args         = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    3739        $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
     40
    3841        if ( is_wp_error( $term_id_pair ) ) {
    3942            return $term_id_pair;
    4043        }
     44
    4145        return $term_id_pair['term_id'];
    4246    }
     
    4549     * Updates the term.
    4650     *
    47      * @param int|object   $term   The term to update.
    48      * @param array|string $fields The context in which to relate the term to the object.
     51     * @since UT (3.7.0)
     52     * @since 6.2.0 Returns a WP_Error object on failure.
    4953     *
    50      * @return int The term ID.
     54     * @param int|object $term   The term to update.
     55     * @param array      $fields Array of arguments for updating a term.
     56     *
     57     * @return int|WP_Error The term ID on success, WP_Error object on failure.
    5158     */
    5259    public function update_object( $term, $fields ) {
    5360        $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
     61
    5462        if ( is_object( $term ) ) {
    5563            $taxonomy = $term->taxonomy;
    5664        }
     65
    5766        $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
     67
     68        if ( is_wp_error( $term_id_pair ) ) {
     69            return $term_id_pair;
     70        }
     71
    5872        return $term_id_pair['term_id'];
    5973    }
     
    6175    /**
    6276     * Attach terms to the given post.
     77     *
     78     * @since UT (3.7.0)
    6379     *
    6480     * @param int          $post_id  The post ID.
     
    8096     * Create a term and returns it as an object.
    8197     *
     98     * @since 4.3.0
     99     *
    82100     * @param array $args                   Array or string of arguments for inserting a term.
    83101     * @param null  $generation_definitions The default values.
     
    93111
    94112        $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
     113
    95114        return get_term( $term_id, $taxonomy );
    96115    }
     
    98117    /**
    99118     * Retrieves the term by a given ID.
     119     *
     120     * @since UT (3.7.0)
    100121     *
    101122     * @param int $term_id ID of the term to retrieve.
  • 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.
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php

    r46985 r55019  
    77 * as a way to indicate expected return values from the given factory methods.
    88 *
    9  * @method int create( $args = array(), $generation_definitions = null )
    10  * @method WP_User create_and_get( $args = array(), $generation_definitions = null )
    11  * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
     9 * @method int|WP_Error    create( $args = array(), $generation_definitions = null )
     10 * @method WP_User|WP_Error create_and_get( $args = array(), $generation_definitions = null )
     11 * @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
    1212 */
    1313class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
     
    2525     * Inserts an user.
    2626     *
     27     * @since UT (3.7.0)
     28     *
    2729     * @param array $args The user data to insert.
    2830     *
     
    3537    /**
    3638     * Updates the user data.
     39     *
     40     * @since UT (3.7.0)
    3741     *
    3842     * @param int   $user_id ID of the user to update.
     
    4953     * Retrieves the user for a given ID.
    5054     *
     55     * @since UT (3.7.0)
     56     *
    5157     * @param int $user_id ID of the user ID to retrieve.
    5258     *
  • trunk/tests/phpunit/tests/post/wpInsertPost.php

    r54889 r55019  
    11931193            )
    11941194        );
    1195         $this->assertSame( 0, $post_id );
     1195        $this->assertWPError( $post_id );
    11961196
    11971197        $post_id = self::factory()->post->create(
     
    12021202            )
    12031203        );
    1204         $this->assertSame( 0, $post_id );
     1204        $this->assertWPError( $post_id );
    12051205
    12061206        // Empty post_date_gmt without floating status
     
    12111211            )
    12121212        );
    1213         $this->assertSame( 0, $post_id );
     1213        $this->assertWPError( $post_id );
    12141214
    12151215        $post_id = self::factory()->post->create(
     
    12201220            )
    12211221        );
    1222         $this->assertSame( 0, $post_id );
     1222        $this->assertWPError( $post_id );
    12231223
    12241224        // Valid post_date_gmt
     
    12291229            )
    12301230        );
    1231         $this->assertSame( 0, $post_id );
     1231        $this->assertWPError( $post_id );
    12321232
    12331233        // Invalid post_date_gmt
     
    12381238            )
    12391239        );
    1240         $this->assertSame( 0, $post_id );
     1240        $this->assertWPError( $post_id );
    12411241    }
    12421242
Note: See TracChangeset for help on using the changeset viewer.