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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55019 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-12-28 14:07:16 +00:00
parent ac9573bd67
commit d0fc6ddc71
12 changed files with 311 additions and 91 deletions

View File

@ -1522,10 +1522,11 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* Creates an attachment post from an uploaded file.
*
* @since 4.4.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $upload Array of information about the uploaded file, provided by wp_upload_bits().
* @param int $parent_post_id Optional. Parent post ID.
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function _make_attachment( $upload, $parent_post_id = 0 ) {
$type = '';
@ -1547,9 +1548,18 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
'guid' => $upload['url'],
);
$id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return $id;
$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true );
if ( is_wp_error( $attachment_id ) ) {
return $attachment_id;
}
wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
);
return $attachment_id;
}
/**

View File

@ -10,6 +10,8 @@ class WP_UnitTest_Factory_Callback_After_Create {
/**
* WP_UnitTest_Factory_Callback_After_Create constructor.
*
* @since UT (3.7.0)
*
* @param callable $callback A callback function.
*/
public function __construct( $callback ) {
@ -19,11 +21,13 @@ class WP_UnitTest_Factory_Callback_After_Create {
/**
* Calls the set callback on a given object.
*
* @param mixed $object The object to apply the callback on.
* @since UT (3.7.0)
*
* @return mixed The possibly altered object.
* @param int $object_id ID of the object to apply the callback on.
*
* @return mixed Updated object field.
*/
public function call( $object ) {
return call_user_func( $this->callback, $object );
public function call( $object_id ) {
return call_user_func( $this->callback, $object_id );
}
}

View File

@ -1,10 +1,23 @@
<?php
/**
* Unit test factory for attachments.
*
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
/**
* Create an attachment fixture.
*
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $args {
* Array of arguments. Accepts all arguments that can be passed to
* wp_insert_attachment(), in addition to the following:
@ -14,7 +27,7 @@ class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
* @param int $legacy_parent Deprecated.
* @param array $legacy_args Deprecated.
*
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
// Backward compatibility for legacy argument format.
@ -33,16 +46,19 @@ class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
$args
);
return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
return wp_insert_attachment( $r, $r['file'], $r['post_parent'], true );
}
/**
* Saves an attachment.
*
* @since 4.4.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param string $file The file name to create attachment object for.
* @param int $parent ID of the post to attach the file to.
*
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function create_upload_object( $file, $parent = 0 ) {
$contents = file_get_contents( $file );
@ -68,9 +84,17 @@ class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
);
// Save the data.
$id = wp_insert_attachment( $attachment, $upload['file'], $parent );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent, true );
return $id;
if ( is_wp_error( $attachment_id ) ) {
return $attachment_id;
}
wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
);
return $attachment_id;
}
}

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Site create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Site|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {

View File

@ -8,9 +8,9 @@
*
* @since 4.6.0
*
* @method int create( $args = array(), $generation_definitions = null )
* @method object create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method object|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
@ -22,15 +22,52 @@ class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
);
}
/**
* Creates a link object.
*
* @since 4.6.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $args Arguments for the link object.
*
* @return int|WP_Error The link ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
return wp_insert_link( $args );
return wp_insert_link( $args, true );
}
/**
* Updates a link object.
*
* @since 4.6.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param int $link_id ID of the link to update.
* @param array $fields The fields to update.
*
* @return int|WP_Error The link ID on success, WP_Error object on failure.
*/
public function update_object( $link_id, $fields ) {
$fields['link_id'] = $link_id;
return wp_update_link( $fields );
$result = wp_update_link( $fields );
if ( 0 === $result ) {
return new WP_Error( 'link_update_error', __( 'Could not update link.' ) );
}
return $result;
}
/**
* Retrieves a link by a given ID.
*
* @since 4.6.0
*
* @param int $link_id ID of the link to retrieve.
*
* @return object|null The link object on success, null on failure.
*/
public function get_object_by_id( $link_id ) {
return get_bookmark( $link_id );
}

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Comment create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Comment|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
@ -25,30 +25,53 @@ class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
/**
* Inserts a comment.
*
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args The comment details.
*
* @return int|false The comment's ID on success, false on failure.
* @return int|WP_Error The comment ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
return wp_insert_comment( $this->addslashes_deep( $args ) );
global $wpdb;
$comment_id = wp_insert_comment( $this->addslashes_deep( $args ) );
if ( false === $comment_id ) {
return new WP_Error(
'db_insert_error',
__( 'Could not insert comment into the database.' ),
$wpdb->last_error
);
}
return $comment_id;
}
/**
* Updates a comment.
*
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param int $comment_id The comment ID.
* @param array $fields The comment details.
*
* @return int The value 1 if the comment was updated, 0 if not updated.
* @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
* WP_Error object on failure.
*/
public function update_object( $comment_id, $fields ) {
$fields['comment_ID'] = $comment_id;
return wp_update_comment( $this->addslashes_deep( $fields ) );
return wp_update_comment( $this->addslashes_deep( $fields ), true );
}
/**
* Creates multiple comments on a given post.
*
* @since UT (3.7.0)
*
* @param int $post_id ID of the post to create comments for.
* @param int $count Total amount of comments to create.
* @param array $args The comment details.
@ -64,6 +87,8 @@ class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
/**
* Retrieves a comment by a given ID.
*
* @since UT (3.7.0)
*
* @param int $comment_id ID of the comment to retrieve.
*
* @return WP_Comment|null WP_Comment object on success, null on failure.

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Network create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Network|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
@ -23,6 +23,16 @@ class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
);
}
/**
* Creates a network object.
*
* @since 3.9.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $args Arguments for the network object.
*
* @return int|WP_Error The network ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
@ -32,12 +42,43 @@ class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
$email = get_userdata( $args['user'] )->user_email;
}
populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
$result = populate_network(
$args['network_id'],
$args['domain'],
$email,
$args['title'],
$args['path'],
$args['subdomain_install']
);
if ( is_wp_error( $result ) ) {
return $result;
}
return (int) $args['network_id'];
}
/**
* Updates a network object. Not implemented.
*
* @since 3.9.0
*
* @param int $network_id ID of the network to update.
* @param array $fields The fields to update.
*
* @return void
*/
public function update_object( $network_id, $fields ) {}
/**
* Retrieves a network by a given ID.
*
* @since 3.9.0
*
* @param int $network_id ID of the network to retrieve.
*
* @return WP_Network|null The network object on success, null on failure.
*/
public function get_object_by_id( $network_id ) {
return get_network( $network_id );
}

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Post create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
@ -26,30 +26,38 @@ class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
/**
* Creates a post object.
*
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $args Array with elements for the post.
*
* @return int The post ID on success. The value 0 on failure.
* @return int|WP_Error The post ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
return wp_insert_post( $args );
return wp_insert_post( $args, true );
}
/**
* Updates an existing post object.
*
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param int $post_id ID of the post to update.
* @param array $fields Post data.
*
* @return int The post ID on success. The value 0 on failure.
* @return int|WP_Error The post ID on success, WP_Error object on failure.
*/
public function update_object( $post_id, $fields ) {
$fields['ID'] = $post_id;
return wp_update_post( $fields );
return wp_update_post( $fields, true );
}
/**
* Retrieves a post by a given ID.
*
* @since UT (3.7.0)
*
* @param int $post_id ID of the post to retrieve.
*
* @return WP_Post|null WP_Post object on success, null on failure.

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Term create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_Term|WP_Error|null create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
@ -28,39 +28,55 @@ class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
/**
* Creates a term object.
*
* @param array $args Array or string of arguments for inserting a term.
* @since UT (3.7.0)
*
* @return array|WP_Error
* @param array $args Array of arguments for inserting a term.
*
* @return int|WP_Error The term ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
$args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
$term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
if ( is_wp_error( $term_id_pair ) ) {
return $term_id_pair;
}
return $term_id_pair['term_id'];
}
/**
* Updates the term.
*
* @param int|object $term The term to update.
* @param array|string $fields The context in which to relate the term to the object.
* @since UT (3.7.0)
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @return int The term ID.
* @param int|object $term The term to update.
* @param array $fields Array of arguments for updating a term.
*
* @return int|WP_Error The term ID on success, WP_Error object on failure.
*/
public function update_object( $term, $fields ) {
$fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
if ( is_object( $term ) ) {
$taxonomy = $term->taxonomy;
}
$term_id_pair = wp_update_term( $term, $taxonomy, $fields );
if ( is_wp_error( $term_id_pair ) ) {
return $term_id_pair;
}
return $term_id_pair['term_id'];
}
/**
* Attach terms to the given post.
*
* @since UT (3.7.0)
*
* @param int $post_id The post ID.
* @param string|array $terms An array of terms to set for the post, or a string of terms
* separated by commas. Hierarchical taxonomies must always pass IDs rather
@ -79,6 +95,8 @@ class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
/**
* Create a term and returns it as an object.
*
* @since 4.3.0
*
* @param array $args Array or string of arguments for inserting a term.
* @param null $generation_definitions The default values.
*
@ -92,12 +110,15 @@ class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
}
$taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
return get_term( $term_id, $taxonomy );
}
/**
* Retrieves the term by a given ID.
*
* @since UT (3.7.0)
*
* @param int $term_id ID of the term to retrieve.
*
* @return WP_Term|WP_Error|null WP_Term on success. WP_Error if taxonomy does not exist. Null for miscellaneous failure.

View File

@ -9,12 +9,19 @@ abstract class WP_UnitTest_Factory_For_Thing {
public $factory;
/**
* Creates a new factory, which will create objects of a specific Thing
* Creates a new factory, which will create objects of a specific Thing.
*
* @param object $factory Global factory that can be used to create other objects on the system
* @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
* can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
* {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
* @since UT (3.7.0)
*
* @param object $factory Global factory that can be used to create other objects
* on the system.
* @param array $default_generation_definitions Defines what default values should the properties
* of the object have. The default values can be generators --
* an object with the next() method.
* There are some default generators:
* - {@link WP_UnitTest_Generator_Sequence}
* - {@link WP_UnitTest_Generator_Locale_Name}
* - {@link WP_UnitTest_Factory_Callback_After_Create}
*/
public function __construct( $factory, $default_generation_definitions = array() ) {
$this->factory = $factory;
@ -22,31 +29,39 @@ abstract class WP_UnitTest_Factory_For_Thing {
}
/**
* Creates an object.
* Creates an object and returns its ID.
*
* @since UT (3.7.0)
*
* @param array $args The arguments.
*
* @return mixed The result. Can be anything.
* @return int|WP_Error The object ID on success, WP_Error object on failure.
*/
abstract public function create_object( $args );
/**
* Updates an existing object.
*
* @param int $object The object ID.
* @param array $fields The values to update.
* @since UT (3.7.0)
*
* @return mixed The result. Can be anything.
* @param int $object_id The object ID.
* @param array $fields The values to update.
*
* @return int|WP_Error The object ID on success, WP_Error object on failure.
*/
abstract public function update_object( $object, $fields );
abstract public function update_object( $object_id, $fields );
/**
* Creates an object.
* Creates an object and returns its ID.
*
* @param array $args Optional. The arguments for the object to create. Default is empty array.
* @param null $generation_definitions Optional. The default values for the object. Default is null.
* @since UT (3.7.0)
*
* @return mixed The result. Can be anything.
* @param array $args Optional. The arguments for the object to create.
* Default empty array.
* @param null $generation_definitions Optional. The default values for the object.
* Default null.
*
* @return int|WP_Error The object ID on success, WP_Error object on failure.
*/
public function create( $args = array(), $generation_definitions = null ) {
if ( is_null( $generation_definitions ) ) {
@ -54,28 +69,35 @@ abstract class WP_UnitTest_Factory_For_Thing {
}
$generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
$created = $this->create_object( $generated_args );
if ( ! $created || is_wp_error( $created ) ) {
return $created;
$object_id = $this->create_object( $generated_args );
if ( ! $object_id || is_wp_error( $object_id ) ) {
return $object_id;
}
if ( $callbacks ) {
$updated_fields = $this->apply_callbacks( $callbacks, $created );
$save_result = $this->update_object( $created, $updated_fields );
$updated_fields = $this->apply_callbacks( $callbacks, $object_id );
$save_result = $this->update_object( $object_id, $updated_fields );
if ( ! $save_result || is_wp_error( $save_result ) ) {
return $save_result;
}
}
return $created;
return $object_id;
}
/**
* Creates an object and returns its object.
* Creates and returns an object.
*
* @param array $args Optional. The arguments for the object to create. Default is empty array.
* @param null $generation_definitions Optional. The default values for the object. Default is null.
* @since UT (3.7.0)
*
* @return mixed The created object. Can be anything.
* @param array $args Optional. The arguments for the object to create.
* Default empty array.
* @param null $generation_definitions Optional. The default values for the object.
* Default null.
*
* @return mixed The created object. Can be anything. WP_Error object on failure.
*/
public function create_and_get( $args = array(), $generation_definitions = null ) {
$object_id = $this->create( $args, $generation_definitions );
@ -90,6 +112,8 @@ abstract class WP_UnitTest_Factory_For_Thing {
/**
* Retrieves an object by ID.
*
* @since UT (3.7.0)
*
* @param int $object_id The object ID.
*
* @return mixed The object. Can be anything.
@ -99,17 +123,23 @@ abstract class WP_UnitTest_Factory_For_Thing {
/**
* Creates multiple objects.
*
* @since UT (3.7.0)
*
* @param int $count Amount of objects to create.
* @param array $args Optional. The arguments for the object to create. Default is empty array.
* @param null $generation_definitions Optional. The default values for the object. Default is null.
* @param array $args Optional. The arguments for the object to create.
* Default empty array.
* @param null $generation_definitions Optional. The default values for the object.
* Default null.
*
* @return array
*/
public function create_many( $count, $args = array(), $generation_definitions = null ) {
$results = array();
for ( $i = 0; $i < $count; $i++ ) {
$results[] = $this->create( $args, $generation_definitions );
}
return $results;
}
@ -117,9 +147,13 @@ abstract class WP_UnitTest_Factory_For_Thing {
* Combines the given arguments with the generation_definitions (defaults) and applies
* possibly set callbacks on it.
*
* @param array $args Optional. The arguments to combine with defaults. Default is empty array.
* @param array|null $generation_definitions Optional. The defaults. Default is null.
* @param array|null $callbacks Optional. Array with callbacks to apply on the fields. Default is null.
* @since UT (3.7.0)
*
* @param array $args Optional. The arguments to combine with defaults.
* Default empty array.
* @param array|null $generation_definitions Optional. The defaults. Default null.
* @param array|null $callbacks Optional. Array with callbacks to apply on the fields.
* Default null.
*
* @return array|WP_Error Combined array on success. WP_Error when default value is incorrent.
*/
@ -144,7 +178,10 @@ abstract class WP_UnitTest_Factory_For_Thing {
} elseif ( is_object( $generator ) ) {
$args[ $field_name ] = sprintf( $generator->get_template_string(), $incr );
} else {
return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
return new WP_Error(
'invalid_argument',
'Factory default value should be either a scalar or an generator object.'
);
}
}
}
@ -156,23 +193,28 @@ abstract class WP_UnitTest_Factory_For_Thing {
/**
* Applies the callbacks on the created object.
*
* @since UT (3.7.0)
*
* @param WP_UnitTest_Factory_Callback_After_Create[] $callbacks Array with callback functions.
* @param mixed $created The object to apply callbacks for.
* @param int $object_id ID of the object to apply callbacks for.
*
* @return array The altered fields.
*/
public function apply_callbacks( $callbacks, $created ) {
public function apply_callbacks( $callbacks, $object_id ) {
$updated_fields = array();
foreach ( $callbacks as $field_name => $generator ) {
$updated_fields[ $field_name ] = $generator->call( $created );
$updated_fields[ $field_name ] = $generator->call( $object_id );
}
return $updated_fields;
}
/**
* Instantiates a callback objects for the given function name.
*
* @since UT (3.7.0)
*
* @param string $function The callback function.
*
* @return WP_UnitTest_Factory_Callback_After_Create
@ -184,6 +226,8 @@ abstract class WP_UnitTest_Factory_For_Thing {
/**
* Adds slashes to the given value.
*
* @since UT (3.7.0)
*
* @param array|object|string|mixed $value The value to add slashes to.
*
* @return array|string The value with the possibly applied slashes.

View File

@ -6,9 +6,9 @@
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_User create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
* @method WP_User|WP_Error create_and_get( $args = array(), $generation_definitions = null )
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
@ -24,6 +24,8 @@ class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
/**
* Inserts an user.
*
* @since UT (3.7.0)
*
* @param array $args The user data to insert.
*
* @return int|WP_Error The user ID on success, WP_Error object on failure.
@ -35,6 +37,8 @@ class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
/**
* Updates the user data.
*
* @since UT (3.7.0)
*
* @param int $user_id ID of the user to update.
* @param array $fields The user data to update.
*
@ -48,6 +52,8 @@ class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
/**
* Retrieves the user for a given ID.
*
* @since UT (3.7.0)
*
* @param int $user_id ID of the user ID to retrieve.
*
* @return WP_User The user object.

View File

@ -1192,7 +1192,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_status' => 'draft',
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
$post_id = self::factory()->post->create(
array(
@ -1201,7 +1201,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_status' => 'draft',
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
// Empty post_date_gmt without floating status
$post_id = self::factory()->post->create(
@ -1210,7 +1210,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_status' => 'publish',
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
$post_id = self::factory()->post->create(
array(
@ -1219,7 +1219,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_status' => 'publish',
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
// Valid post_date_gmt
$post_id = self::factory()->post->create(
@ -1228,7 +1228,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_date_gmt' => $post_date_gmt,
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
// Invalid post_date_gmt
$post_id = self::factory()->post->create(
@ -1237,7 +1237,7 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase {
'post_date_gmt' => $invalid_date,
)
);
$this->assertSame( 0, $post_id );
$this->assertWPError( $post_id );
}
/**