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/post/wpUniquePostSlug.php

    r34810 r35225  
    99         */
    1010        public function test_non_latin_slugs() {
    11                 $author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
     11                $author_id = self::$factory->user->create( array( 'role' => 'editor' ) );
    1212
    1313                $inputs = array(
     
    5050                        'post_status' => 'publish',
    5151                );
    52                 $one = $this->factory->post->create( $args );
     52                $one = self::$factory->post->create( $args );
    5353                $args['post_type'] = 'post-type-2';
    54                 $two = $this->factory->post->create( $args );
     54                $two = self::$factory->post->create( $args );
    5555
    5656                $this->assertEquals( 'some-slug', get_post( $one )->post_name );
     
    7575                        'post_status' => 'publish',
    7676                );
    77                 $one = $this->factory->post->create( $args );
     77                $one = self::$factory->post->create( $args );
    7878                $args['post_name'] = 'some-slug-2';
    79                 $two = $this->factory->post->create( $args );
     79                $two = self::$factory->post->create( $args );
    8080
    8181                $this->assertEquals( 'some-slug', get_post( $one )->post_name );
     
    9898                        'post_status' => 'publish',
    9999                );
    100                 $one = $this->factory->post->create( $args );
     100                $one = self::$factory->post->create( $args );
    101101
    102102                $args = array(
     
    105105                        'post_name' => 'image'
    106106                );
    107                 $attachment = $this->factory->attachment->create_object( 'image.jpg', $one, $args );
     107                $attachment = self::$factory->attachment->create_object( 'image.jpg', $one, $args );
    108108
    109109                $args = array(
     
    113113                        'post_parent' => $one
    114114                );
    115                 $two = $this->factory->post->create( $args );
     115                $two = self::$factory->post->create( $args );
    116116
    117117                $this->assertEquals( 'some-slug', get_post( $one )->post_name );
     
    129129         */
    130130        public function test_whitelisted_post_statuses_should_not_be_forced_to_be_unique( $status ) {
    131                 $p1 = $this->factory->post->create( array(
    132                         'post_type' => 'post',
    133                         'post_name' => 'foo',
    134                 ) );
    135 
    136                 $p2 = $this->factory->post->create( array(
     131                $p1 = self::$factory->post->create( array(
     132                        'post_type' => 'post',
     133                        'post_name' => 'foo',
     134                ) );
     135
     136                $p2 = self::$factory->post->create( array(
    137137                        'post_type' => 'post',
    138138                ) );
     
    152152
    153153        public function test_revisions_should_not_be_forced_to_be_unique() {
    154                 $p1 = $this->factory->post->create( array(
    155                         'post_type' => 'post',
    156                         'post_name' => 'foo',
    157                 ) );
    158 
    159                 $p2 = $this->factory->post->create( array(
     154                $p1 = self::$factory->post->create( array(
     155                        'post_type' => 'post',
     156                        'post_name' => 'foo',
     157                ) );
     158
     159                $p2 = self::$factory->post->create( array(
    160160                        'post_type' => 'post',
    161161                ) );
     
    172172                $this->set_permalink_structure( '/%postname%/' );
    173173
    174                 $p = $this->factory->post->create( array(
     174                $p = self::$factory->post->create( array(
    175175                        'post_type' => 'post',
    176176                        'post_name' => 'foo',
     
    187187                $this->set_permalink_structure( '/%postname%/' );
    188188
    189                 $p = $this->factory->post->create( array(
     189                $p = self::$factory->post->create( array(
    190190                        'post_type' => 'post',
    191191                        'post_name' => 'foo',
     
    203203                $this->set_permalink_structure( '/%year%/%postname%/' );
    204204
    205                 $p = $this->factory->post->create( array(
     205                $p = self::$factory->post->create( array(
    206206                        'post_type' => 'post',
    207207                        'post_name' => 'foo',
     
    218218                $this->set_permalink_structure( '/%year%/%postname%/' );
    219219
    220                 $p = $this->factory->post->create( array(
     220                $p = self::$factory->post->create( array(
    221221                        'post_type' => 'post',
    222222                        'post_name' => 'foo',
     
    233233                $this->set_permalink_structure( '/%year%/foo/%postname%/' );
    234234
    235                 $p = $this->factory->post->create( array(
     235                $p = self::$factory->post->create( array(
    236236                        'post_type' => 'post',
    237237                        'post_name' => 'foo',
     
    248248                $this->set_permalink_structure( '/%year%/%postname%/' );
    249249
    250                 $p = $this->factory->post->create( array(
     250                $p = self::$factory->post->create( array(
    251251                        'post_type' => 'post',
    252252                        'post_name' => 'foo',
     
    263263                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    264264
    265                 $p = $this->factory->post->create( array(
     265                $p = self::$factory->post->create( array(
    266266                        'post_type' => 'post',
    267267                        'post_name' => 'foo',
     
    278278                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    279279
    280                 $p = $this->factory->post->create( array(
     280                $p = self::$factory->post->create( array(
    281281                        'post_type' => 'post',
    282282                        'post_name' => 'foo',
     
    293293                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    294294
    295                 $p = $this->factory->post->create( array(
     295                $p = self::$factory->post->create( array(
    296296                        'post_type' => 'post',
    297297                        'post_name' => 'foo',
Note: See TracChangeset for help on using the changeset viewer.