Make WordPress Core

Changeset 37299


Ignore:
Timestamp:
04/22/2016 02:35:52 PM (8 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk/tests/phpunit/includes/factory
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php

    r36854 r37299  
    6363            $generation_definitions = $this->default_generation_definitions;
    6464
     65        // Use the same incrementor for all fields belonging to this object.
     66        $gen  = new WP_UnitTest_Generator_Sequence();
     67        $incr = $gen->get_incr();
     68
    6569        foreach( array_keys( $generation_definitions ) as $field_name ) {
    6670            if ( !isset( $args[$field_name] ) ) {
    6771                $generator = $generation_definitions[$field_name];
    68                 if ( is_scalar( $generator ) )
     72                if ( is_scalar( $generator ) ) {
    6973                    $args[$field_name] = $generator;
    70                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
     74                } elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    7175                    $callbacks[$field_name] = $generator;
    72                 } elseif ( is_object( $generator ) )
    73                     $args[$field_name] = $generator->next();
    74                 else
     76                } elseif ( is_object( $generator ) ) {
     77                    $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr );
     78                } else {
    7579                    return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
     80                }
    7681            }
    7782        }
     83
    7884        return $args;
    7985    }
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php

    r36347 r37299  
    2121        return $generated;
    2222    }
     23
     24    /**
     25     * Get the incrementor.
     26     *
     27     * @since 4.6.0
     28     *
     29     * @return int
     30     */
     31    public function get_incr() {
     32        return self::$incr;
     33    }
     34
     35    /**
     36     * Get the template string.
     37     *
     38     * @since 4.6.0
     39     *
     40     * @return string
     41     */
     42    public function get_template_string() {
     43        return $this->template_string;
     44    }
    2345}
Note: See TracChangeset for help on using the changeset viewer.