From 7c7938613530516f3faadbc9b1aee520bea25824 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 22 Apr 2016 14:35:52 +0000 Subject: [PATCH] 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 --- .../class-wp-unittest-factory-for-thing.php | 16 +++++++++----- .../class-wp-unittest-generator-sequence.php | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) 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; + } }