Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/results.php

    r35186 r35225  
    457457         */
    458458        function test_query_author_vars() {
    459                 $author_1 = $this->factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
    460                 $post_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
    461 
    462                 $author_2 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
    463                 $post_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
    464 
    465                 $author_3 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
    466                 $post_3 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
    467 
    468                 $author_4 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
    469                 $post_4 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
     459                $author_1 = self::$factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
     460                $post_1 = self::$factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
     461
     462                $author_2 = self::$factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     463                $post_2 = self::$factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
     464
     465                $author_3 = self::$factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     466                $post_3 = self::$factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
     467
     468                $author_4 = self::$factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     469                $post_4 = self::$factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
    470470
    471471                $posts = $this->q->query( array(
     
    624624         */
    625625        function test_post_password() {
    626                 $one   = (string) $this->factory->post->create( array( 'post_password' => '' ) );
    627                 $two   = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
    628                 $three = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
     626                $one   = (string) self::$factory->post->create( array( 'post_password' => '' ) );
     627                $two   = (string) self::$factory->post->create( array( 'post_password' => 'burrito' ) );
     628                $three = (string) self::$factory->post->create( array( 'post_password' => 'burrito' ) );
    629629
    630630                $args = array( 'post__in' => array( $one, $two, $three ), 'fields' => 'ids' );
     
    666666                register_post_type( 'handbook', array( 'hierarchical' => true ) );
    667667
    668                 $post_1 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_type' => 'handbook' ) );
    669                 $post_2 = $this->factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) );
    670                 $post_3 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_2, 'post_type' => 'handbook' ) );
     668                $post_1 = self::$factory->post->create( array( 'post_title' => 'Getting Started', 'post_type' => 'handbook' ) );
     669                $post_2 = self::$factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) );
     670                $post_3 = self::$factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_2, 'post_type' => 'handbook' ) );
    671671
    672672                $result = $this->q->query( array( 'handbook' => 'getting-started', 'post_type' => 'handbook' ) );
     
    680680                register_post_type( 'handbook', array( 'hierarchical' => true ) );
    681681
    682                 $post_1 = $this->factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) );
    683                 $post_2 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_1, 'post_type' => 'handbook' ) );
     682                $post_1 = self::$factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) );
     683                $post_2 = self::$factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_1, 'post_type' => 'handbook' ) );
    684684
    685685                $this->assertContains( 'contributing-to-the-wordpress-codex/getting-started', get_permalink( $post_2 ) );
     
    691691        function test_title() {
    692692                $title = 'Tacos are Cool';
    693                 $post_id = $this->factory->post->create( array(
     693                $post_id = self::$factory->post->create( array(
    694694                        'post_title' => $title,
    695695                        'post_type' => 'post',
Note: See TracChangeset for help on using the changeset viewer.