Make WordPress Core


Ignore:
Timestamp:
06/28/2024 03:45:08 PM (6 months ago)
Author:
SergeyBiryukov
Message:

Tests: Use assertSame() in WP_Interactivity_API 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.

Follow-up to [57563], [57649], [57822], [57826], [57835], [58159], [58327].

See #61530.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php

    r58234 r58594  
    7777        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    7878        $p->next_tag( array( 'class_name' => 'interactive/block-1' ) );
    79         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     79        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    8080    }
    8181
     
    9898        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    9999        $p->next_tag( array( 'class_name' => 'interactive/block-1' ) );
    100         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     100        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    101101        $p->next_tag( array( 'class_name' => 'interactive/block-2' ) );
    102         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     102        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    103103        $p->next_tag( array( 'class_name' => 'non-interactive/block-3' ) );
    104104        $this->assertNull( $p->get_attribute( 'value' ) );
    105105        $p->next_tag( array( 'class_name' => 'interactive/block-4' ) );
    106         $this->assertEquals( '4', $p->get_attribute( 'value' ) );
     106        $this->assertSame( '4', $p->get_attribute( 'value' ) );
    107107    }
    108108
     
    123123        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    124124        $p->next_tag( array( 'class_name' => 'interactive/block-2' ) );
    125         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     125        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    126126    }
    127127
     
    144144        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    145145        $p->next_tag( array( 'class_name' => 'interactive/block-2' ) );
    146         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     146        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    147147        $p->next_tag( array( 'class_name' => 'interactive/block-3' ) );
    148         $this->assertEquals( '3', $p->get_attribute( 'value' ) );
     148        $this->assertSame( '3', $p->get_attribute( 'value' ) );
    149149    }
    150150
     
    169169        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    170170        $p->next_tag( array( 'class_name' => 'interactive/block-2' ) );
    171         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     171        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    172172        $p->next_tag( array( 'class_name' => 'interactive/block-4' ) );
    173         $this->assertEquals( '4', $p->get_attribute( 'value' ) );
     173        $this->assertSame( '4', $p->get_attribute( 'value' ) );
    174174    }
    175175
     
    191191        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    192192        $p->next_tag( array( 'class_name' => 'interactive/block-1' ) );
    193         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     193        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    194194        $p->next_tag( array( 'class_name' => 'non-interactive/block-2' ) );
    195195        $this->assertNull( $p->get_attribute( 'value' ) );
     
    213213        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    214214        $p->next_tag( array( 'class_name' => 'interactive/block-1' ) );
    215         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     215        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    216216        $p->next_tag( array( 'class_name' => 'non-interactive/block-2' ) );
    217         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     217        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    218218    }
    219219
     
    239239        $p               = new WP_HTML_Tag_Processor( $rendered_blocks );
    240240        $p->next_tag( array( 'class_name' => 'interactive/block-1' ) );
    241         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     241        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    242242        $p->next_tag( array( 'class_name' => 'interactive/block-2' ) );
    243         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     243        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    244244        $p->next_tag( array( 'class_name' => 'non-interactive/block-3' ) );
    245         $this->assertEquals( '2', $p->get_attribute( 'value' ) );
     245        $this->assertSame( '2', $p->get_attribute( 'value' ) );
    246246        $p->next_tag( array( 'class_name' => 'non-interactive/block-4' ) );
    247         $this->assertEquals( '1', $p->get_attribute( 'value' ) );
     247        $this->assertSame( '1', $p->get_attribute( 'value' ) );
    248248    }
    249249
     
    289289        $this->data_wp_test_processor_count = 0;
    290290        wp_interactivity_process_directives( $html );
    291         $this->assertEquals( 1, $this->data_wp_test_processor_count );
     291        $this->assertSame( 1, $this->data_wp_test_processor_count );
    292292
    293293        register_block_type(
     
    310310        do_blocks( $post_content );
    311311        unregister_block_type( 'test/custom-directive-block' );
    312         $this->assertEquals( 2, $this->data_wp_test_processor_count );
     312        $this->assertSame( 2, $this->data_wp_test_processor_count );
    313313        $directive_processors->setValue( null, $old_directive_processors );
    314314    }
     
    344344        remove_filter( 'render_block_data', 'test_render_block_data' );
    345345        unregister_block_type( 'test/custom-directive-block' );
    346         $this->assertEquals( 'test', $processor->get_attribute( 'value' ) );
     346        $this->assertSame( 'test', $processor->get_attribute( 'value' ) );
    347347    }
    348348
     
    356356     */
    357357    public function test_wp_interactivity_data_wp_context_with_different_arrays() {
    358         $this->assertEquals( 'data-wp-context=\'{}\'', wp_interactivity_data_wp_context( array() ) );
    359         $this->assertEquals(
     358        $this->assertSame( 'data-wp-context=\'{}\'', wp_interactivity_data_wp_context( array() ) );
     359        $this->assertSame(
    360360            'data-wp-context=\'{"a":1,"b":"2","c":true}\'',
    361361            wp_interactivity_data_wp_context(
     
    367367            )
    368368        );
    369         $this->assertEquals(
     369        $this->assertSame(
    370370            'data-wp-context=\'{"a":[1,2]}\'',
    371371            wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ) )
    372372        );
    373         $this->assertEquals(
     373        $this->assertSame(
    374374            'data-wp-context=\'[1,2]\'',
    375375            wp_interactivity_data_wp_context( array( 1, 2 ) )
     
    386386     */
    387387    public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() {
    388         $this->assertEquals( 'data-wp-context=\'myPlugin::{}\'', wp_interactivity_data_wp_context( array(), 'myPlugin' ) );
    389         $this->assertEquals(
     388        $this->assertSame( 'data-wp-context=\'myPlugin::{}\'', wp_interactivity_data_wp_context( array(), 'myPlugin' ) );
     389        $this->assertSame(
    390390            'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'',
    391391            wp_interactivity_data_wp_context(
     
    398398            )
    399399        );
    400         $this->assertEquals(
     400        $this->assertSame(
    401401            'data-wp-context=\'myPlugin::{"a":[1,2]}\'',
    402402            wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ), 'myPlugin' )
    403403        );
    404         $this->assertEquals(
     404        $this->assertSame(
    405405            'data-wp-context=\'myPlugin::[1,2]\'',
    406406            wp_interactivity_data_wp_context( array( 1, 2 ), 'myPlugin' )
     
    419419     */
    420420    public function test_wp_interactivity_data_wp_context_with_json_flags() {
    421         $this->assertEquals( 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', wp_interactivity_data_wp_context( array( 'tag' => '<foo>' ) ) );
    422         $this->assertEquals( 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', wp_interactivity_data_wp_context( array( 'apos' => "'bar'" ) ) );
    423         $this->assertEquals( 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', wp_interactivity_data_wp_context( array( 'quot' => '"baz"' ) ) );
    424         $this->assertEquals( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) );
     421        $this->assertSame( 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', wp_interactivity_data_wp_context( array( 'tag' => '<foo>' ) ) );
     422        $this->assertSame( 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', wp_interactivity_data_wp_context( array( 'apos' => "'bar'" ) ) );
     423        $this->assertSame( 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', wp_interactivity_data_wp_context( array( 'quot' => '"baz"' ) ) );
     424        $this->assertSame( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) );
    425425    }
    426426
     
    449449        $processor->next_tag( array( 'class_name' => 'test' ) );
    450450        unregister_block_type( 'test/custom-directive-block' );
    451         $this->assertEquals( '1', $processor->get_attribute( 'src' ) );
     451        $this->assertSame( '1', $processor->get_attribute( 'src' ) );
    452452    }
    453453
     
    489489        $second_input_value = $processor->get_attribute( 'value' );
    490490        unregister_block_type( 'test/custom-directive-block' );
    491         $this->assertEquals( 'inner', $first_input_value );
    492         $this->assertEquals( 'outer', $second_input_value );
     491        $this->assertSame( 'inner', $first_input_value );
     492        $this->assertSame( 'outer', $second_input_value );
    493493    }
    494494
Note: See TracChangeset for help on using the changeset viewer.