Changeset 44497
- Timestamp:
- 01/09/2019 05:43:14 AM (6 years ago)
- Location:
- trunk/tests/phpunit/includes/factory
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php
r36347 r44497 2 2 3 3 class WP_UnitTest_Factory_Callback_After_Create { 4 5 /** 6 * @var callable 7 */ 4 8 var $callback; 5 9 10 /** 11 * WP_UnitTest_Factory_Callback_After_Create constructor. 12 * 13 * @param callable $callback A callback function. 14 */ 6 15 function __construct( $callback ) { 7 16 $this->callback = $callback; 8 17 } 9 18 19 /** 20 * Calls the set callback on given object. 21 * 22 * @param mixed $object The object to apply the callback on. 23 * 24 * @return mixed The possibly altered object. 25 */ 10 26 function call( $object ) { 11 27 return call_user_func( $this->callback, $object ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
r43571 r44497 13 13 * } 14 14 * @param int $legacy_parent Deprecated. 15 * @param array $legacy_args Deprecated 15 * @param array $legacy_args Deprecated. 16 * 17 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 16 18 */ 17 19 function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) { … … 35 37 } 36 38 39 /** 40 * Saves an attachment. 41 * 42 * @param string $file The file name to create attachment object for. 43 * @param int $parent The post id to attach the file to. 44 * 45 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 46 */ 37 47 function create_upload_object( $file, $parent = 0 ) { 38 48 $contents = file_get_contents( $file ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php
r42343 r44497 24 24 } 25 25 26 /** 27 * Creates a blog object. 28 * 29 * @param array $args Arguments for the site object. 30 * 31 * @return int|WP_Error Returns WP_Error object on failure, the site ID on success. 32 */ 26 33 function create_object( $args ) { 27 34 global $wpdb; … … 39 46 } 40 47 48 /** 49 * Updates a blog object. Not implemented. 50 * 51 * @param int $blog_id The blog id to update. 52 * @param array $fields The fields to update. 53 * 54 * @return void 55 */ 41 56 function update_object( $blog_id, $fields ) {} 42 57 58 /** 59 * Retrieves a site by given blog id. 60 * 61 * @param int $blog_id The blog id to retrieve. 62 * 63 * @return null|WP_Site The site object or null if not found. 64 */ 43 65 function get_object_by_id( $blog_id ) { 44 66 return get_site( $blog_id ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php
r42343 r44497 23 23 } 24 24 25 /** 26 * Inserts a comment. 27 * 28 * @param array $args The comment details. 29 * 30 * @return false|int The comment's ID on success, false on failure. 31 */ 25 32 function create_object( $args ) { 26 33 return wp_insert_comment( $this->addslashes_deep( $args ) ); 27 34 } 28 35 36 /** 37 * Updates a comment. 38 * 39 * @param int $comment_id The comment id. 40 * @param array $fields The comment details. 41 * 42 * @return int When updated 1, not update 0. 43 */ 29 44 function update_object( $comment_id, $fields ) { 30 45 $fields['comment_ID'] = $comment_id; … … 32 47 } 33 48 49 /** 50 * Creates multiple comments on given post. 51 * 52 * @param int $post_id The post id to create comments for. 53 * @param int $count Total amount of comments to create. 54 * @param array $args The comment details. 55 * @param null $generation_definitions Default values. 56 * 57 * @return int[] Array with the comment ids. 58 */ 34 59 function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { 35 60 $args['comment_post_ID'] = $post_id; … … 37 62 } 38 63 64 /** 65 * Returns a comment. 66 * 67 * @param int $comment_id The comment id. 68 * 69 * @return null|WP_Comment WP_Comment when found, null when not found. 70 */ 39 71 function get_object_by_id( $comment_id ) { 40 72 return get_comment( $comment_id ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php
r42343 r44497 24 24 } 25 25 26 /** 27 * Creates a post object. 28 * 29 * @param array $args Array with elements for the post. 30 * 31 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 32 */ 26 33 function create_object( $args ) { 27 34 return wp_insert_post( $args ); 28 35 } 29 36 37 /** 38 * Updates an existing post object. 39 * 40 * @param int $post_id The post id to update. 41 * @param array $fields Post data. 42 * 43 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. 44 */ 30 45 function update_object( $post_id, $fields ) { 31 46 $fields['ID'] = $post_id; … … 33 48 } 34 49 50 /** 51 * Retrieves a object by an id. 52 * 53 * @param int $post_id The post id to update. 54 * 55 * @return null|WP_Post WP_Post on success or null on failure. 56 */ 35 57 function get_object_by_id( $post_id ) { 36 58 return get_post( $post_id ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php
r42343 r44497 25 25 } 26 26 27 /** 28 * Creates a term object. 29 * 30 * @param array $args Array or string of arguments for inserting a term. 31 * 32 * @return array|WP_Error 33 */ 27 34 function create_object( $args ) { 28 35 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); … … 34 41 } 35 42 43 /** 44 * Updates the term. 45 * 46 * @param int|object $term The term to update. 47 * @param array|string $fields The context in which to relate the term to the object. 48 * 49 * @return int The term id. 50 */ 36 51 function update_object( $term, $fields ) { 37 52 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); … … 43 58 } 44 59 60 /** 61 * Attach terms on the given post. 62 * 63 * @param int $post_id The Post ID. 64 * @param string|array $terms An array of terms to set for the post, or a string of terms 65 * separated by commas. Hierarchical taxonomies must always pass IDs rather 66 * than names so that children with the same names but different parents 67 * aren't confused. 68 * @param string $taxonomy Taxonomy name. 69 * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, 70 * replace the terms with the new terms. Default true. 71 * 72 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 73 */ 45 74 function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { 46 75 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); … … 48 77 49 78 /** 50 * @return array|null|WP_Error|WP_Term 79 * Create a term and returns it as a object. 80 * 81 * @param array $args Array or string of arguments for inserting a term. 82 * @param null $generation_definitions The default values. 83 * 84 * @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure. 51 85 */ 52 86 function create_and_get( $args = array(), $generation_definitions = null ) { … … 56 90 } 57 91 92 /** 93 * Retrieves the term by given term id. 94 * 95 * @param int $term_id The term id to retrieve. 96 * 97 * @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure. 98 */ 58 99 function get_object_by_id( $term_id ) { 59 100 return get_term( $term_id, $this->taxonomy ); -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php
r42343 r44497 22 22 } 23 23 24 /** 25 * Creates an object. 26 * 27 * @param array $args The arguments. 28 * 29 * @return mixed The result. Can be anything. 30 */ 24 31 abstract function create_object( $args ); 32 33 /** 34 * Updates an existing object. 35 * 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 */ 25 41 abstract function update_object( $object, $fields ); 26 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. 50 */ 27 51 function create( $args = array(), $generation_definitions = null ) { 28 52 if ( is_null( $generation_definitions ) ) { … … 46 70 } 47 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. 79 */ 48 80 function create_and_get( $args = array(), $generation_definitions = null ) { 49 81 $object_id = $this->create( $args, $generation_definitions ); … … 51 83 } 52 84 85 /** 86 * Retrieves an object by ID. 87 * 88 * @param int $object_id The object id. 89 * 90 * @return mixed The object. Can be anything. 91 */ 53 92 abstract function get_object_by_id( $object_id ); 54 93 94 /** 95 * Creates multiple objects. 96 * 97 * @param int $count Amount of objects to create. 98 * @param array $args Optional. The arguments for the object to create. Default is empty array. 99 * @param null $generation_definitions Optional. The default values for the object. Default is null. 100 * 101 * @return array 102 */ 55 103 function create_many( $count, $args = array(), $generation_definitions = null ) { 56 104 $results = array(); … … 61 109 } 62 110 111 /** 112 * Combines the given argument with the generation_definitions (defaults) and applies 113 * possibly set callbacks on it. 114 * 115 * @param array $args Optional. The arguments to combine with defaults. Default is empty array. 116 * @param array|null $generation_definitions Optional. The defaults. Default is null. 117 * @param array|null $callbacks Optional. Array with callbacks to apply on the fields. Default is null. 118 * 119 * @return array|WP_Error Combined array on success. WP_Error when default value is incorrent. 120 */ 63 121 function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { 64 122 $callbacks = array(); … … 89 147 } 90 148 149 150 /** 151 * Applies the callbacks on the created object. 152 * 153 * @param WP_UnitTest_Factory_Callback_After_Create[] $callbacks Array with callback functions. 154 * @param mixed $created The object to apply callbacks for. 155 * 156 * @return array The altered fields. 157 */ 91 158 function apply_callbacks( $callbacks, $created ) { 92 159 $updated_fields = array(); 160 93 161 foreach ( $callbacks as $field_name => $generator ) { 94 162 $updated_fields[ $field_name ] = $generator->call( $created ); … … 97 165 } 98 166 167 /** 168 * Instantiates a callback objects for the given function name. 169 * 170 * @param string $function The callback function. 171 * 172 * @return WP_UnitTest_Factory_Callback_After_Create 173 */ 99 174 function callback( $function ) { 100 175 return new WP_UnitTest_Factory_Callback_After_Create( $function ); 101 176 } 102 177 178 /** 179 * Adds slashes to the given value. 180 * 181 * @param array|object|string|mixed $value The value to add slashes to. 182 * 183 * @return array|string The value with the possibly applied slashes. 184 */ 103 185 function addslashes_deep( $value ) { 104 186 if ( is_array( $value ) ) { -
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php
r42343 r44497 22 22 } 23 23 24 /** 25 * Inserts an user. 26 * 27 * @param array $args The user data to insert. 28 * 29 * @return int|WP_Error 30 */ 24 31 function create_object( $args ) { 25 32 return wp_insert_user( $args ); 26 33 } 27 34 35 /** 36 * Updates the user data. 37 * 38 * @param int $user_id The user id to update. 39 * @param array $fields The user data to update. 40 * 41 * @return int|WP_Error User id on success. WP_Error on failure. 42 */ 28 43 function update_object( $user_id, $fields ) { 29 44 $fields['ID'] = $user_id; … … 31 46 } 32 47 48 /** 49 * Retrieves the user for given user id. 50 * 51 * @param int $user_id The user id to get. 52 * 53 * @return WP_User The user. 54 */ 33 55 function get_object_by_id( $user_id ) { 34 56 return new WP_User( $user_id );
Note: See TracChangeset
for help on using the changeset viewer.