Make WordPress Core


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

    r35224 r35225  
    2222
    2323    function test_wp_update_comment() {
    24         $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) );
    25         $post2 = $this->factory->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) );
    26         $comments = $this->factory->comment->create_post_comments( $post->ID, 5 );
     24        $post = self::$factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) );
     25        $post2 = self::$factory->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) );
     26        $comments = self::$factory->comment->create_post_comments( $post->ID, 5 );
    2727        $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
    2828        $this->assertEquals( 1, $result );
     
    4040     */
    4141    function test_wp_update_comment_updates_comment_type() {
    42         $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     42        $comment_id = self::$factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
    4343
    4444        wp_update_comment( array( 'comment_ID' => $comment_id, 'comment_type' => 'pingback' ) );
     
    5252     */
    5353    function test_wp_update_comment_updates_user_id() {
    54         $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     54        $comment_id = self::$factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
    5555
    5656        wp_update_comment( array( 'comment_ID' => $comment_id, 'user_id' => 1 ) );
     
    6161
    6262    public function test_get_approved_comments() {
    63         $ca1 = $this->factory->comment->create( array(
     63        $ca1 = self::$factory->comment->create( array(
    6464            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
    6565        ) );
    66         $ca2 = $this->factory->comment->create( array(
     66        $ca2 = self::$factory->comment->create( array(
    6767            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
    6868        ) );
    69         $ca3 = $this->factory->comment->create( array(
     69        $ca3 = self::$factory->comment->create( array(
    7070            'comment_post_ID' => self::$post_id, 'comment_approved' => '0'
    7171        ) );
    72         $c2 = $this->factory->comment->create( array(
     72        $c2 = self::$factory->comment->create( array(
    7373            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback'
    7474        ) );
    75         $c3 = $this->factory->comment->create( array(
     75        $c3 = self::$factory->comment->create( array(
    7676            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback'
    7777        ) );
    78         $c4 = $this->factory->comment->create( array(
     78        $c4 = self::$factory->comment->create( array(
    7979            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario'
    8080        ) );
    81         $c5 = $this->factory->comment->create( array(
     81        $c5 = self::$factory->comment->create( array(
    8282            'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi'
    8383        ) );
     
    9393     */
    9494    public function test_get_approved_comments_with_post_id_0_should_return_empty_array() {
    95         $ca1 = $this->factory->comment->create( array(
     95        $ca1 = self::$factory->comment->create( array(
    9696            'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
    9797        ) );
     
    257257     */
    258258    public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() {
    259         $p = $this->factory->post->create( array(
     259        $p = self::$factory->post->create( array(
    260260            'post_author' => 0,
    261261        ) );
    262262
    263         $c = $this->factory->comment->create( array(
     263        $c = self::$factory->comment->create( array(
    264264            'comment_post_ID' => $p,
    265265        ) );
     
    272272     */
    273273    public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() {
    274         $c = $this->factory->comment->create( array(
     274        $c = self::$factory->comment->create( array(
    275275            'comment_post_ID' => self::$post_id,
    276276            'comment_approved' => 'spam',
     
    285285     */
    286286    public function test_wp_new_comment_with_meta() {
    287         $c = $this->factory->comment->create( array(
     287        $c = self::$factory->comment->create( array(
    288288            'comment_approved' => '1',
    289289            'comment_meta' => array(
     
    300300     */
    301301    public function test_wp_comment_get_children_should_fill_children() {
    302         $c1 = $this->factory->comment->create( array(
    303             'comment_post_ID' => self::$post_id,
    304             'comment_approved' => '1',
    305         ) );
    306 
    307         $c2 = $this->factory->comment->create( array(
     302        $c1 = self::$factory->comment->create( array(
     303            'comment_post_ID' => self::$post_id,
     304            'comment_approved' => '1',
     305        ) );
     306
     307        $c2 = self::$factory->comment->create( array(
    308308            'comment_post_ID' => self::$post_id,
    309309            'comment_approved' => '1',
     
    311311        ) );
    312312
    313         $c3 = $this->factory->comment->create( array(
     313        $c3 = self::$factory->comment->create( array(
    314314            'comment_post_ID' => self::$post_id,
    315315            'comment_approved' => '1',
     
    317317        ) );
    318318
    319         $c4 = $this->factory->comment->create( array(
     319        $c4 = self::$factory->comment->create( array(
    320320            'comment_post_ID' => self::$post_id,
    321321            'comment_approved' => '1',
     
    323323        ) );
    324324
    325         $c5 = $this->factory->comment->create( array(
    326             'comment_post_ID' => self::$post_id,
    327             'comment_approved' => '1',
    328         ) );
    329 
    330         $c6 = $this->factory->comment->create( array(
     325        $c5 = self::$factory->comment->create( array(
     326            'comment_post_ID' => self::$post_id,
     327            'comment_approved' => '1',
     328        ) );
     329
     330        $c6 = self::$factory->comment->create( array(
    331331            'comment_post_ID' => self::$post_id,
    332332            'comment_approved' => '1',
     
    348348     */
    349349    public function test_post_properties_should_be_lazyloaded() {
    350         $c = $this->factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     350        $c = self::$factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
    351351
    352352        $post = get_post( self::$post_id );
Note: See TracChangeset for help on using the changeset viewer.