Make WordPress Core

Changeset 58739


Ignore:
Timestamp:
07/16/2024 06:37:46 PM (2 years ago)
Author:
hellofromTonya
Message:

Tests: Use data provider in Tests_Interactivity_API_wpInteractivityAPIFunctions.

Refactors the following tests to use a data provider with named test cases:

  • test_wp_interactivity_data_wp_context_with_different_arrays()
  • test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace()
  • test_wp_interactivity_data_wp_context_with_json_flags()

This is better as:

  1. One failing test will not block the other tests from running.
  2. Each test is now referenced by name in any error message, making it more straight forward to see which test failed.
  3. The test no longer contains multiple assertions.
  4. It makes it more straight forward to add additional tests.

Follow-up to [58594], [58234], [57762], [57743], [57742], [57563].

Props jrf.
See #61530.

File:
1 edited

Legend:

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

    r58738 r58739  
    355355         * @ticket 60356
    356356         *
    357          * @covers wp_interactivity_data_wp_context
    358          */
    359         public function test_wp_interactivity_data_wp_context_with_different_arrays() {
    360                 $this->assertSame( 'data-wp-context=\'{}\'', wp_interactivity_data_wp_context( array() ) );
    361                 $this->assertSame(
    362                         'data-wp-context=\'{"a":1,"b":"2","c":true}\'',
    363                         wp_interactivity_data_wp_context(
    364                                 array(
     357         * @covers       wp_interactivity_data_wp_context
     358         * @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays
     359         *
     360         * @param array  $context  Context to encode.
     361         * @param string $expected Expected function output.
     362         */
     363        public function test_wp_interactivity_data_wp_context_with_different_arrays( $context, $expected ) {
     364                $this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) );
     365        }
     366
     367        /**
     368         * Data provider.
     369         *
     370         * @return array
     371         */
     372        public function data_wp_interactivity_data_wp_context_with_different_arrays() {
     373                return array(
     374                        'empty array'                                  => array(
     375                                'context'  => array(),
     376                                'expected' => 'data-wp-context=\'{}\'',
     377                        ),
     378                        'associative array with mixed values'          => array(
     379                                'context'  => array(
    365380                                        'a' => 1,
    366381                                        'b' => '2',
    367382                                        'c' => true,
    368                                 )
    369                         )
    370                 );
    371                 $this->assertSame(
    372                         'data-wp-context=\'{"a":[1,2]}\'',
    373                         wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ) )
    374                 );
    375                 $this->assertSame(
    376                         'data-wp-context=\'[1,2]\'',
    377                         wp_interactivity_data_wp_context( array( 1, 2 ) )
     383                                ),
     384                                'expected' => 'data-wp-context=\'{"a":1,"b":"2","c":true}\'',
     385                        ),
     386                        'associative array with nested array as value' => array(
     387                                'context'  => array( 'a' => array( 1, 2 ) ),
     388                                'expected' => 'data-wp-context=\'{"a":[1,2]}\'',
     389                        ),
     390                        'array without keys, integer values'           => array(
     391                                'context'  => array( 1, 2 ),
     392                                'expected' => 'data-wp-context=\'[1,2]\'',
     393                        ),
    378394                );
    379395        }
     
    385401         * @ticket 60356
    386402         *
    387          * @covers wp_interactivity_data_wp_context
    388          */
    389         public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() {
    390                 $this->assertSame( 'data-wp-context=\'myPlugin::{}\'', wp_interactivity_data_wp_context( array(), 'myPlugin' ) );
    391                 $this->assertSame(
    392                         'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'',
    393                         wp_interactivity_data_wp_context(
    394                                 array(
     403         * @covers       wp_interactivity_data_wp_context
     404         * @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace
     405         *
     406         * @param array  $context  Context to encode.
     407         * @param string $store    Store namespace.
     408         * @param string $expected Expected function output.
     409         */
     410        public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace( $context, $store, $expected ) {
     411                $this->assertSame( $expected, wp_interactivity_data_wp_context( $context, $store ) );
     412        }
     413
     414        /**
     415         * Data provider.
     416         *
     417         * @return array
     418         */
     419        public function data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() {
     420                return array(
     421                        'empty array'                                  => array(
     422                                'context'  => array(),
     423                                'store'    => 'myPlugin',
     424                                'expected' => 'data-wp-context=\'myPlugin::{}\'',
     425                        ),
     426                        'associative array with mixed values'          => array(
     427                                'context'  => array(
    395428                                        'a' => 1,
    396429                                        'b' => '2',
    397430                                        'c' => true,
    398431                                ),
    399                                 'myPlugin'
    400                         )
    401                 );
    402                 $this->assertSame(
    403                         'data-wp-context=\'myPlugin::{"a":[1,2]}\'',
    404                         wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ), 'myPlugin' )
    405                 );
    406                 $this->assertSame(
    407                         'data-wp-context=\'myPlugin::[1,2]\'',
    408                         wp_interactivity_data_wp_context( array( 1, 2 ), 'myPlugin' )
     432                                'store'    => 'myPlugin',
     433                                'expected' => 'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'',
     434                        ),
     435                        'associative array with nested array as value' => array(
     436                                'context'  => array( 'a' => array( 1, 2 ) ),
     437                                'store'    => 'myPlugin',
     438                                'expected' => 'data-wp-context=\'myPlugin::{"a":[1,2]}\'',
     439                        ),
     440                        'array without keys, integer values'           => array(
     441                                'context'  => array( 1, 2 ),
     442                                'store'    => 'myPlugin',
     443                                'expected' => 'data-wp-context=\'myPlugin::[1,2]\'',
     444                        ),
    409445                );
    410446        }
     
    418454         * @ticket 60356
    419455         *
    420          * @covers wp_interactivity_data_wp_context
    421          */
    422         public function test_wp_interactivity_data_wp_context_with_json_flags() {
    423                 $this->assertSame( 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', wp_interactivity_data_wp_context( array( 'tag' => '<foo>' ) ) );
    424                 $this->assertSame( 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', wp_interactivity_data_wp_context( array( 'apos' => "'bar'" ) ) );
    425                 $this->assertSame( 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', wp_interactivity_data_wp_context( array( 'quot' => '"baz"' ) ) );
    426                 $this->assertSame( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) );
     456         * @covers       wp_interactivity_data_wp_context
     457         * @dataProvider data_wp_interactivity_data_wp_context_with_json_flags
     458         *
     459         * @param array  $context  Context to encode.
     460         * @param string $expected Expected function output.
     461         */
     462        public function test_wp_interactivity_data_wp_context_with_json_flags( $context, $expected ) {
     463                $this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) );
     464        }
     465
     466        /**
     467         * Data provider.
     468         *
     469         * @return array
     470         */
     471        public function data_wp_interactivity_data_wp_context_with_json_flags() {
     472                return array(
     473                        'value contains <> brackets'        => array(
     474                                'context'  => array( 'tag' => '<foo>' ),
     475                                'expected' => 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'',
     476                        ),
     477                        'value contains single quote chars' => array(
     478                                'context'  => array( 'apos' => "'bar'" ),
     479                                'expected' => 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'',
     480                        ),
     481                        'value contains double quote chars' => array(
     482                                'context'  => array( 'quot' => '"baz"' ),
     483                                'expected' => 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'',
     484                        ),
     485                        'value contains & ampersand'        => array(
     486                                'context'  => array( 'amp' => 'T&T' ),
     487                                'expected' => 'data-wp-context=\'{"amp":"T\u0026T"}\'',
     488                        ),
     489                );
    427490        }
    428491
Note: See TracChangeset for help on using the changeset viewer.