diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php index 8e984572a8..55548e5039 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php @@ -62,19 +62,25 @@ abstract class WP_UnitTest_Factory_For_Thing { if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions; + // Use the same incrementor for all fields belonging to this object. + $gen = new WP_UnitTest_Generator_Sequence(); + $incr = $gen->get_incr(); + foreach( array_keys( $generation_definitions ) as $field_name ) { if ( !isset( $args[$field_name] ) ) { $generator = $generation_definitions[$field_name]; - if ( is_scalar( $generator ) ) + if ( is_scalar( $generator ) ) { $args[$field_name] = $generator; - elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { + } elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { $callbacks[$field_name] = $generator; - } elseif ( is_object( $generator ) ) - $args[$field_name] = $generator->next(); - else + } 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 $args; } diff --git a/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php b/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php index 940edc4dca..d1831a9f85 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php @@ -20,4 +20,26 @@ class WP_UnitTest_Generator_Sequence { $this->next++; return $generated; } + + /** + * Get the incrementor. + * + * @since 4.6.0 + * + * @return int + */ + public function get_incr() { + return self::$incr; + } + + /** + * Get the template string. + * + * @since 4.6.0 + * + * @return string + */ + public function get_template_string() { + return $this->template_string; + } }