WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (17 months ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/taxonomy.php

    r48665 r48937  
    66class Tests_Taxonomy extends WP_UnitTestCase {
    77    function test_get_post_taxonomies() {
    8         $this->assertEquals( array( 'category', 'post_tag', 'post_format' ), get_object_taxonomies( 'post' ) );
     8        $this->assertSame( array( 'category', 'post_tag', 'post_format' ), get_object_taxonomies( 'post' ) );
    99    }
    1010
    1111    function test_get_link_taxonomies() {
    12         $this->assertEquals( array( 'link_category' ), get_object_taxonomies( 'link' ) );
     12        $this->assertSame( array( 'link_category' ), get_object_taxonomies( 'link' ) );
    1313    }
    1414
     
    1818    function test_get_unknown_taxonomies() {
    1919        // Taxonomies for an unknown object type.
    20         $this->assertEquals( array(), get_object_taxonomies( rand_str() ) );
    21         $this->assertEquals( array(), get_object_taxonomies( '' ) );
    22         $this->assertEquals( array(), get_object_taxonomies( 0 ) );
    23         $this->assertEquals( array(), get_object_taxonomies( null ) );
     20        $this->assertSame( array(), get_object_taxonomies( rand_str() ) );
     21        $this->assertSame( array(), get_object_taxonomies( '' ) );
     22        $this->assertSame( array(), get_object_taxonomies( 0 ) );
     23        $this->assertSame( array(), get_object_taxonomies( null ) );
    2424    }
    2525
     
    3030            $this->assertTrue( is_object( $tax ) );
    3131            $this->assertTrue( is_array( $tax->object_type ) );
    32             $this->assertEquals( array( 'post' ), $tax->object_type );
     32            $this->assertSame( array( 'post' ), $tax->object_type );
    3333        }
    3434    }
     
    3939        $taxes = get_the_taxonomies( $post_id );
    4040        $this->assertNotEmpty( $taxes );
    41         $this->assertEquals( array( 'category' ), array_keys( $taxes ) );
     41        $this->assertSame( array( 'category' ), array_keys( $taxes ) );
    4242
    4343        $id = self::factory()->tag->create();
     
    4747        $this->assertNotEmpty( $taxes );
    4848        $this->assertCount( 2, $taxes );
    49         $this->assertEquals( array( 'category', 'post_tag' ), array_keys( $taxes ) );
     49        $this->assertSame( array( 'category', 'post_tag' ), array_keys( $taxes ) );
    5050    }
    5151
     
    5757
    5858        $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) );
    59         $this->assertEquals( 'Categories: Uncategorized.', $taxes['category'] );
     59        $this->assertSame( 'Categories: Uncategorized.', $taxes['category'] );
    6060
    6161        $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '<span class="foo"><a href="%1$s">%2$s</a></span>' ) );
    6262        $link  = get_category_link( 1 );
    63         $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $taxes['category'] );
     63        $this->assertSame( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $taxes['category'] );
    6464    }
    6565
     
    9191            )
    9292        );
    93         $this->assertEquals( 'Categories: Uncategorized.', $output );
     93        $this->assertSame( 'Categories: Uncategorized.', $output );
    9494
    9595        $output = get_echo(
     
    103103        );
    104104        $link   = get_category_link( 1 );
    105         $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $output );
     105        $this->assertSame( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $output );
    106106    }
    107107
     
    112112            $this->assertTrue( is_object( $tax ) );
    113113            $this->assertTrue( is_array( $tax->object_type ) );
    114             $this->assertEquals( array( 'link' ), $tax->object_type );
     114            $this->assertSame( array( 'link' ), $tax->object_type );
    115115        }
    116116    }
     
    286286        $terms = get_objects_in_term( 1, 'invalid_taxonomy' );
    287287        $this->assertInstanceOf( 'WP_Error', $terms );
    288         $this->assertEquals( 'invalid_taxonomy', $terms->get_error_code() );
     288        $this->assertSame( 'invalid_taxonomy', $terms->get_error_code() );
    289289    }
    290290
    291291    public function test_get_objects_in_term_should_return_empty_array() {
    292         $this->assertEquals( array(), get_objects_in_term( 1, 'post_tag' ) );
     292        $this->assertSame( array(), get_objects_in_term( 1, 'post_tag' ) );
    293293    }
    294294
     
    430430            'cat_name' => 'Updated Name',
    431431        );
    432         $this->assertEquals( 1, wp_insert_category( $cat ) );
     432        $this->assertSame( 1, wp_insert_category( $cat ) );
    433433    }
    434434
     
    448448            'cat_name' => 'Error',
    449449        );
    450         $this->assertEquals( 0, wp_insert_category( $cat, false ) );
     450        $this->assertSame( 0, wp_insert_category( $cat, false ) );
    451451    }
    452452
     
    964964        $problematic_term = current( wp_list_pluck( $terms_obj, 'name' ) );
    965965
    966         $this->assertEquals( $problematic_term, $term_name );
     966        $this->assertSame( $problematic_term, $term_name );
    967967    }
    968968
Note: See TracChangeset for help on using the changeset viewer.