- Timestamp:
- 12/28/2022 02:07:16 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php
r46985 r55019 10 10 11 11 /** 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} 18 25 */ 19 26 public function __construct( $factory, $default_generation_definitions = array() ) { … … 23 30 24 31 /** 25 * Creates an object. 32 * Creates an object and returns its ID. 33 * 34 * @since UT (3.7.0) 26 35 * 27 36 * @param array $args The arguments. 28 37 * 29 * @return mixed The result. Can be anything.38 * @return int|WP_Error The object ID on success, WP_Error object on failure. 30 39 */ 31 40 abstract public function create_object( $args ); … … 34 43 * Updates an existing object. 35 44 * 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. 50 65 */ 51 66 public function create( $args = array(), $generation_definitions = null ) { … … 55 70 56 71 $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; 60 76 } 61 77 62 78 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 65 82 if ( ! $save_result || is_wp_error( $save_result ) ) { 66 83 return $save_result; 67 84 } 68 85 } 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. 79 101 */ 80 102 public function create_and_get( $args = array(), $generation_definitions = null ) { … … 91 113 * Retrieves an object by ID. 92 114 * 115 * @since UT (3.7.0) 116 * 93 117 * @param int $object_id The object ID. 94 118 * … … 100 124 * Creates multiple objects. 101 125 * 126 * @since UT (3.7.0) 127 * 102 128 * @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. 105 133 * 106 134 * @return array … … 108 136 public function create_many( $count, $args = array(), $generation_definitions = null ) { 109 137 $results = array(); 138 110 139 for ( $i = 0; $i < $count; $i++ ) { 111 140 $results[] = $this->create( $args, $generation_definitions ); 112 141 } 142 113 143 return $results; 114 144 } … … 118 148 * possibly set callbacks on it. 119 149 * 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. 123 157 * 124 158 * @return array|WP_Error Combined array on success. WP_Error when default value is incorrent. … … 145 179 $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr ); 146 180 } 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 ); 148 185 } 149 186 } … … 157 194 * Applies the callbacks on the created object. 158 195 * 196 * @since UT (3.7.0) 197 * 159 198 * @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. 161 200 * 162 201 * @return array The altered fields. 163 202 */ 164 public function apply_callbacks( $callbacks, $ created ) {203 public function apply_callbacks( $callbacks, $object_id ) { 165 204 $updated_fields = array(); 166 205 167 206 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 170 210 return $updated_fields; 171 211 } … … 173 213 /** 174 214 * Instantiates a callback objects for the given function name. 215 * 216 * @since UT (3.7.0) 175 217 * 176 218 * @param string $function The callback function. … … 184 226 /** 185 227 * Adds slashes to the given value. 228 * 229 * @since UT (3.7.0) 186 230 * 187 231 * @param array|object|string|mixed $value The value to add slashes to.
Note: See TracChangeset
for help on using the changeset viewer.