Skip to content

Commit

Permalink
Tests: Use data provider in Tests_Interactivity_API_wpInteractivityAP…
Browse files Browse the repository at this point in the history
…IFunctions.

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.
3. It makes it more straight forward to add additional tests.

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

Props jrf.
See #61530.

git-svn-id: https://develop.svn.wordpress.org/trunk@58739 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Jul 16, 2024
1 parent a6d5694 commit 81702ce
Showing 1 changed file with 103 additions and 40 deletions.
143 changes: 103 additions & 40 deletions tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,43 @@ public function test_process_directives_when_block_is_filtered() {
*
* @ticket 60356
*
* @covers wp_interactivity_data_wp_context
* @covers wp_interactivity_data_wp_context
* @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays
*
* @param array $context Context to encode.
* @param string $expected Expected function output.
*/
public function test_wp_interactivity_data_wp_context_with_different_arrays( $context, $expected ) {
$this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) );
}

/**
* Data provider.
*
* @return array
*/
public function test_wp_interactivity_data_wp_context_with_different_arrays() {
$this->assertSame( 'data-wp-context=\'{}\'', wp_interactivity_data_wp_context( array() ) );
$this->assertSame(
'data-wp-context=\'{"a":1,"b":"2","c":true}\'',
wp_interactivity_data_wp_context(
array(
public function data_wp_interactivity_data_wp_context_with_different_arrays() {
return array(
'empty array' => array(
'context' => array(),
'expected' => 'data-wp-context=\'{}\'',
),
'associative array with mixed values' => array(
'context' => array(
'a' => 1,
'b' => '2',
'c' => true,
)
)
);
$this->assertSame(
'data-wp-context=\'{"a":[1,2]}\'',
wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ) )
);
$this->assertSame(
'data-wp-context=\'[1,2]\'',
wp_interactivity_data_wp_context( array( 1, 2 ) )
),
'expected' => 'data-wp-context=\'{"a":1,"b":"2","c":true}\'',
),
'associative array with nested array as value' => array(
'context' => array( 'a' => array( 1, 2 ) ),
'expected' => 'data-wp-context=\'{"a":[1,2]}\'',
),
'array without keys, integer values' => array(
'context' => array( 1, 2 ),
'expected' => 'data-wp-context=\'[1,2]\'',
),
);
}

Expand All @@ -384,28 +400,48 @@ public function test_wp_interactivity_data_wp_context_with_different_arrays() {
*
* @ticket 60356
*
* @covers wp_interactivity_data_wp_context
* @covers wp_interactivity_data_wp_context
* @dataProvider data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace
*
* @param array $context Context to encode.
* @param string $store Store namespace.
* @param string $expected Expected function output.
*/
public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace( $context, $store, $expected ) {
$this->assertSame( $expected, wp_interactivity_data_wp_context( $context, $store ) );
}

/**
* Data provider.
*
* @return array
*/
public function test_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() {
$this->assertSame( 'data-wp-context=\'myPlugin::{}\'', wp_interactivity_data_wp_context( array(), 'myPlugin' ) );
$this->assertSame(
'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'',
wp_interactivity_data_wp_context(
array(
public function data_wp_interactivity_data_wp_context_with_different_arrays_and_a_namespace() {
return array(
'empty array' => array(
'context' => array(),
'store' => 'myPlugin',
'expected' => 'data-wp-context=\'myPlugin::{}\'',
),
'associative array with mixed values' => array(
'context' => array(
'a' => 1,
'b' => '2',
'c' => true,
),
'myPlugin'
)
);
$this->assertSame(
'data-wp-context=\'myPlugin::{"a":[1,2]}\'',
wp_interactivity_data_wp_context( array( 'a' => array( 1, 2 ) ), 'myPlugin' )
);
$this->assertSame(
'data-wp-context=\'myPlugin::[1,2]\'',
wp_interactivity_data_wp_context( array( 1, 2 ), 'myPlugin' )
'store' => 'myPlugin',
'expected' => 'data-wp-context=\'myPlugin::{"a":1,"b":"2","c":true}\'',
),
'associative array with nested array as value' => array(
'context' => array( 'a' => array( 1, 2 ) ),
'store' => 'myPlugin',
'expected' => 'data-wp-context=\'myPlugin::{"a":[1,2]}\'',
),
'array without keys, integer values' => array(
'context' => array( 1, 2 ),
'store' => 'myPlugin',
'expected' => 'data-wp-context=\'myPlugin::[1,2]\'',
),
);
}

Expand All @@ -417,13 +453,40 @@ public function test_wp_interactivity_data_wp_context_with_different_arrays_and_
*
* @ticket 60356
*
* @covers wp_interactivity_data_wp_context
* @covers wp_interactivity_data_wp_context
* @dataProvider data_wp_interactivity_data_wp_context_with_json_flags
*
* @param array $context Context to encode.
* @param string $expected Expected function output.
*/
public function test_wp_interactivity_data_wp_context_with_json_flags() {
$this->assertSame( 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'', wp_interactivity_data_wp_context( array( 'tag' => '<foo>' ) ) );
$this->assertSame( 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'', wp_interactivity_data_wp_context( array( 'apos' => "'bar'" ) ) );
$this->assertSame( 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'', wp_interactivity_data_wp_context( array( 'quot' => '"baz"' ) ) );
$this->assertSame( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) );
public function test_wp_interactivity_data_wp_context_with_json_flags( $context, $expected ) {
$this->assertSame( $expected, wp_interactivity_data_wp_context( $context ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_wp_interactivity_data_wp_context_with_json_flags() {
return array(
'value contains <> brackets' => array(
'context' => array( 'tag' => '<foo>' ),
'expected' => 'data-wp-context=\'{"tag":"\u003Cfoo\u003E"}\'',
),
'value contains single quote chars' => array(
'context' => array( 'apos' => "'bar'" ),
'expected' => 'data-wp-context=\'{"apos":"\u0027bar\u0027"}\'',
),
'value contains double quote chars' => array(
'context' => array( 'quot' => '"baz"' ),
'expected' => 'data-wp-context=\'{"quot":"\u0022baz\u0022"}\'',
),
'value contains & ampersand' => array(
'context' => array( 'amp' => 'T&T' ),
'expected' => 'data-wp-context=\'{"amp":"T\u0026T"}\'',
),
);
}

/**
Expand Down

0 comments on commit 81702ce

Please sign in to comment.