Tests: Use the same incrementor for all fields belonging to a given text fixture.

[35244] changed the way that `WP_UnitTest_Generator_Sequence()` created an
incrementor for object fields (like 'post_name' and 'user_email'), by making
incrementor static across the entire run of the test suite. While this helped
to enforce uniqueness across the tests, it has the side effect of bumping the
incrementor between fields on the same object (so that, eg, the same post might
have `post_name` "post-12" but `post_title` "Post 13". By switching to a
technique that uses the same incrementor for each field belonging to a given
fixture, we conform better to the expectations of developers using
`WP_UnitTest_Factory`.

Fixes #35199.

git-svn-id: https://develop.svn.wordpress.org/trunk@37299 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-04-22 14:35:52 +00:00
parent f9e1cf5de0
commit 7c79386135
2 changed files with 33 additions and 5 deletions

View File

@ -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;
}

View File

@ -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;
}
}