Skip to content

Commit

Permalink
Refactor REST API request tests to use arrays
Browse files Browse the repository at this point in the history
Replaced instances of WP_REST_Request with array structures in unit tests. This simplifies the test cases and ensures compatibility with the rest_filter_response_fields function.
  • Loading branch information
paul bearne committed Jul 24, 2024
1 parent 748f3b3 commit 2bafdc8
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function test_options_request_not_options() {
public function test_rest_filter_response_fields_no_request_filter() {
$response = new WP_REST_Response();
$response->set_data( array( 'a' => true ) );
$request = new WP_REST_Request();
$request = array();

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame( array( 'a' => true ), $response->get_data() );
Expand All @@ -426,8 +426,9 @@ public function test_rest_filter_response_fields_single_field_filter() {
'c' => 2,
)
);
$request = new WP_REST_Request();
$request->set_param( '_fields', 'b' );
$request = array(
'_fields' => 'b',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame( array( 'b' => 1 ), $response->get_data() );
Expand Down Expand Up @@ -521,9 +522,10 @@ public function test_rest_filter_response_fields_has_all_embeds() {
),
)
);
$request = new WP_REST_Request();
$request->set_param( '_fields', 'b' );
$request->set_param( '_embed', '1' );
$request = array(
'_fields' => 'b',
'_embed' => '1',
);

$response = rest_filter_response_fields( $response, null, $request );
$expected = array(
Expand Down Expand Up @@ -597,9 +599,10 @@ public function test_rest_filter_response_fields_has_all_embeds_links_inside_eac
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'id,author' );
$request->set_param( '_embed', '1' );
$request = array(
'_fields' => 'id,author',
'_embed' => '1',
);

$response = rest_filter_response_fields( $response, null, $request );
$expected = array(
Expand Down Expand Up @@ -653,9 +656,9 @@ public function test_rest_filter_response_fields_multi_field_filter() {
'f' => 5,
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'b,c,e' );
$request = array(
'_fields' => 'b,c,e',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -685,8 +688,9 @@ public function test_rest_filter_response_fields_multi_field_filter_array() {
'f' => 5,
)
);
$request = new WP_REST_Request();
$request->set_param( '_fields', array( 'b', 'c', 'e' ) );
$request = array(
'_fields' => array( 'b', 'c', 'e' ),
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -723,9 +727,9 @@ public function test_rest_filter_response_fields_numeric_array() {
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'b,c' );
$request = array(
'_fields' => 'b,c',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -769,9 +773,9 @@ public function test_rest_filter_response_fields_nested_field_filter() {
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'b.1,c,d.5' );
$request = array(
'_fields' => 'b.1,c,d.5',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -810,9 +814,9 @@ public function test_rest_filter_response_fields_deeply_nested_field_filter() {
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'field.a.i,field.b.iv' );
$request = array(
'_fields' => 'field.a.i,field.b.iv',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -846,9 +850,9 @@ public function test_rest_filter_response_fields_top_level_key() {
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'meta' );
$request = array(
'_fields' => 'meta',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -878,9 +882,9 @@ public function test_rest_filter_response_fields_child_after_parent() {
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'meta,meta.key1' );
$request = array(
'_fields' => 'meta,meta.key1',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down Expand Up @@ -910,9 +914,9 @@ public function test_rest_filter_response_fields_include_all_specified_siblings(
),
)
);

$request = new WP_REST_Request();
$request->set_param( '_fields', 'meta.key1,meta.key2' );
$request = array(
'_fields' => 'meta.key1,meta.key2',
);

$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
Expand Down

0 comments on commit 2bafdc8

Please sign in to comment.