Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (10 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/link/getAdjacentPostLink.php

    r31097 r35225  
    1111    public function setUp(){
    1212        parent::setUp();
    13         $this->cat_id = $this->factory->category->create( array( 'name' => 'other' ) );
     13        $this->cat_id = self::$factory->category->create( array( 'name' => 'other' ) );
    1414        $this->post_ids = array();
    15         $this->post_ids[] = $this->factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 05:32:29', 'category_id' => 1 ) );
    16         $this->post_ids[] = $this->factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 04:32:29', 'category_id' => $this->cat_id ) );
    17         $this->post_ids[] = $this->factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 03:32:29', 'category_id' => 1 ) );
    18         $this->post_ids[] = $this->factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 02:32:29', 'category_id' => $this->cat_id ) );
    19         $this->post_ids[] = $this->factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 01:32:29', 'category_id' => 1 ) );
     15        $this->post_ids[] = self::$factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 05:32:29', 'category_id' => 1 ) );
     16        $this->post_ids[] = self::$factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 04:32:29', 'category_id' => $this->cat_id ) );
     17        $this->post_ids[] = self::$factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 03:32:29', 'category_id' => 1 ) );
     18        $this->post_ids[] = self::$factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 02:32:29', 'category_id' => $this->cat_id ) );
     19        $this->post_ids[] = self::$factory->post->create( array( 'post_type' => 'post', 'post_date' => '2014-10-26 01:32:29', 'category_id' => 1 ) );
    2020
    2121        //set current post (has 2 on each end)
     
    2626    public function test_get_next_post_link_default() {
    2727        $actual = get_next_post_link();
    28         $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">Post title 2</a> &raquo;';
     28        $title = get_post( $this->post_ids[1] )->post_title;
     29        $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">' . $title . '</a> &raquo;';
    2930        $this->assertSame( $expected, $actual );
    3031    }
     
    3233    public function test_get_previous_post_link_default() {
    3334        $actual = get_previous_post_link();
    34         $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">Post title 4</a>';
     35        $title = get_post( $this->post_ids[3] )->post_title;
     36        $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $title . '</a>';
    3537        $this->assertSame( $expected, $actual );
    3638    }
     
    3840    public function test_get_next_post_link_same_category() {
    3941        $actual = get_next_post_link( '%link &raquo;', '%title', true );
    40         $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">Post title 2</a> &raquo;';
     42        $title = get_post( $this->post_ids[1] )->post_title;
     43        $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">' . $title . '</a> &raquo;';
    4144        $this->assertSame( $expected, $actual );
    4245    }
     
    4447    public function test_get_previous_post_link_same_category() {
    4548        $actual = get_previous_post_link( '&laquo; %link', '%title', true );
    46         $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">Post title 4</a>';
     49        $title = get_post( $this->post_ids[3] )->post_title;
     50        $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $title . '</a>';
    4751        $this->assertSame( $expected, $actual );
    4852    }
     
    5054    public function test_get_next_post_link_exclude_category() {
    5155        $actual = get_next_post_link( '%link &raquo;', '%title', false, $this->cat_id );
    52         $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">Post title 2</a> &raquo;';
     56        $title = get_post( $this->post_ids[1] )->post_title;
     57        $expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">' . $title . '</a> &raquo;';
    5358        $this->assertSame( $expected, $actual );
    5459    }
     
    5661    public function test_get_previous_post_link_exclude_category() {
    5762        $actual = get_previous_post_link( '&laquo; %link', '%title', false, $this->cat_id );
    58         $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">Post title 4</a>';
     63        $title = get_post( $this->post_ids[3] )->post_title;
     64        $expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $title . '</a>';
    5965        $this->assertSame( $expected, $actual );
    6066    }
Note: See TracChangeset for help on using the changeset viewer.