From 81702ce6de2aa83096f76e3680ea8773f237674d Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 16 Jul 2024 18:37:46 +0000 Subject: [PATCH] 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. 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 --- .../wpInteractivityAPIFunctions.php | 143 +++++++++++++----- 1 file changed, 103 insertions(+), 40 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php index f5089dd04b86e..7c933109154a4 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php @@ -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]\'', + ), ); } @@ -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]\'', + ), ); } @@ -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' => '' ) ) ); - $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' => '' ), + '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"}\'', + ), + ); } /**