Skip to content

Commit 77640b9

Browse files
authored
Chore: Fix coding standard issues reported on wordpress-develop (#25)
1 parent 2314f52 commit 77640b9

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed

tests/unit/rest-api/wpRestAbilitiesListController.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function set_up(): void {
4343

4444
// Set up REST server
4545
global $wp_rest_server;
46-
$this->server = $wp_rest_server = new WP_REST_Server();
46+
$wp_rest_server = new WP_REST_Server();
47+
$this->server = $wp_rest_server;
48+
4749
do_action( 'rest_api_init' );
4850

4951
// Initialize abilities API
@@ -107,7 +109,7 @@ private function register_test_abilities(): void {
107109
case 'multiply':
108110
return $input['a'] * $input['b'];
109111
case 'divide':
110-
return $input['b'] !== 0 ? $input['a'] / $input['b'] : null;
112+
return 0 !== $input['b'] ? $input['a'] / $input['b'] : null;
111113
default:
112114
return null;
113115
}
@@ -194,7 +196,6 @@ public function test_get_items(): void {
194196
$this->assertIsArray( $data );
195197
$this->assertNotEmpty( $data );
196198

197-
198199
$this->assertCount( 50, $data, 'First page should return exactly 50 items (default per_page)' );
199200

200201
$ability_names = wp_list_pluck( $data, 'name' );
@@ -292,7 +293,7 @@ public function test_pagination_links(): void {
292293

293294
// Test last page (should have 'prev' link but no 'next')
294295
$total_abilities = count( wp_get_abilities() );
295-
$last_page = ceil( $total_abilities / 10 );
296+
$last_page = ceil( $total_abilities / 10 );
296297
$request->set_param( 'page', $last_page );
297298
$response = $this->server->dispatch( $request );
298299

@@ -378,7 +379,7 @@ public function test_context_parameter(): void {
378379
* Test schema retrieval.
379380
*/
380381
public function test_get_schema(): void {
381-
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/abilities' );
382+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/abilities' );
382383
$response = $this->server->dispatch( $request );
383384
$data = $response->get_data();
384385

@@ -406,17 +407,17 @@ public function test_ability_name_with_valid_special_characters(): void {
406407
wp_register_ability(
407408
'test-hyphen/ability',
408409
array(
409-
'label' => 'Test Hyphen Ability',
410-
'description' => 'Test ability with hyphen',
411-
'execute_callback' => function( $input ) {
410+
'label' => 'Test Hyphen Ability',
411+
'description' => 'Test ability with hyphen',
412+
'execute_callback' => function ( $input ) {
412413
return array( 'success' => true );
413414
},
414415
'permission_callback' => '__return_true',
415416
)
416417
);
417418

418419
// Test valid special characters (hyphen, forward slash)
419-
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test-hyphen/ability' );
420+
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test-hyphen/ability' );
420421
$response = $this->server->dispatch( $request );
421422
$this->assertEquals( 200, $response->get_status() );
422423
}
@@ -446,7 +447,7 @@ public function invalid_ability_names_provider(): array {
446447
* @param string $name Invalid ability name to test.
447448
*/
448449
public function test_ability_name_with_invalid_special_characters( string $name ): void {
449-
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/' . $name );
450+
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/' . $name );
450451
$response = $this->server->dispatch( $request );
451452
// Should return 404 as the regex pattern won't match
452453
$this->assertEquals( 404, $response->get_status() );
@@ -461,7 +462,7 @@ public function test_extremely_long_ability_names(): void {
461462
// Create a very long but valid ability name
462463
$long_name = 'test/' . str_repeat( 'a', 1000 );
463464

464-
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/' . $long_name );
465+
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/' . $long_name );
465466
$response = $this->server->dispatch( $request );
466467

467468
// Should return 404 as ability doesn't exist

tests/unit/rest-api/wpRestAbilitiesRunController.php

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function set_up(): void {
5454
parent::set_up();
5555

5656
global $wp_rest_server;
57-
$this->server = $wp_rest_server = new WP_REST_Server();
57+
$wp_rest_server = new WP_REST_Server();
58+
$this->server = $wp_rest_server;
59+
5860
do_action( 'rest_api_init' );
5961

6062
do_action( 'abilities_api_init' );
@@ -526,16 +528,18 @@ public function test_invalid_json_in_post_body(): void {
526528
*/
527529
public function test_get_request_with_nested_input_array(): void {
528530
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/query-params/run' );
529-
$request->set_query_params( array(
530-
'input' => array(
531-
'level1' => array(
532-
'level2' => array(
533-
'value' => 'nested',
531+
$request->set_query_params(
532+
array(
533+
'input' => array(
534+
'level1' => array(
535+
'level2' => array(
536+
'value' => 'nested',
537+
),
534538
),
539+
'array' => array( 1, 2, 3 ),
535540
),
536-
'array' => array( 1, 2, 3 ),
537-
),
538-
) );
541+
)
542+
);
539543

540544
$response = $this->server->dispatch( $request );
541545
$this->assertEquals( 200, $response->get_status() );
@@ -550,9 +554,11 @@ public function test_get_request_with_nested_input_array(): void {
550554
*/
551555
public function test_get_request_with_non_array_input(): void {
552556
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/query-params/run' );
553-
$request->set_query_params( array(
554-
'input' => 'not-an-array', // String instead of array
555-
) );
557+
$request->set_query_params(
558+
array(
559+
'input' => 'not-an-array', // String instead of array
560+
)
561+
);
556562

557563
$response = $this->server->dispatch( $request );
558564
// When input is not an array, WordPress returns 400 Bad Request
@@ -565,9 +571,13 @@ public function test_get_request_with_non_array_input(): void {
565571
public function test_post_request_with_non_array_input(): void {
566572
$request = new WP_REST_Request( 'POST', '/wp/v2/abilities/test/calculator/run' );
567573
$request->set_header( 'Content-Type', 'application/json' );
568-
$request->set_body( wp_json_encode( array(
569-
'input' => 'string-value', // String instead of array
570-
) ) );
574+
$request->set_body(
575+
wp_json_encode(
576+
array(
577+
'input' => 'string-value', // String instead of array
578+
)
579+
)
580+
);
571581

572582
$response = $this->server->dispatch( $request );
573583
// When input is not an array, WordPress returns 400 Bad Request
@@ -583,9 +593,9 @@ public function test_output_validation_failure_returns_error(): void {
583593
wp_register_ability(
584594
'test/strict-output',
585595
array(
586-
'label' => 'Strict Output',
587-
'description' => 'Ability with strict output schema',
588-
'output_schema' => array(
596+
'label' => 'Strict Output',
597+
'description' => 'Ability with strict output schema',
598+
'output_schema' => array(
589599
'type' => 'object',
590600
'properties' => array(
591601
'status' => array(
@@ -595,12 +605,12 @@ public function test_output_validation_failure_returns_error(): void {
595605
),
596606
'required' => array( 'status' ),
597607
),
598-
'execute_callback' => function( $input ) {
608+
'execute_callback' => function ( $input ) {
599609
// Return invalid output that doesn't match schema
600610
return array( 'wrong_field' => 'value' );
601611
},
602612
'permission_callback' => '__return_true',
603-
'meta' => array( 'type' => 'tool' ),
613+
'meta' => array( 'type' => 'tool' ),
604614
)
605615
);
606616

@@ -625,9 +635,9 @@ public function test_input_validation_failure_returns_error(): void {
625635
wp_register_ability(
626636
'test/strict-input',
627637
array(
628-
'label' => 'Strict Input',
629-
'description' => 'Ability with strict input schema',
630-
'input_schema' => array(
638+
'label' => 'Strict Input',
639+
'description' => 'Ability with strict input schema',
640+
'input_schema' => array(
631641
'type' => 'object',
632642
'properties' => array(
633643
'required_field' => array(
@@ -636,11 +646,11 @@ public function test_input_validation_failure_returns_error(): void {
636646
),
637647
'required' => array( 'required_field' ),
638648
),
639-
'execute_callback' => function( $input ) {
649+
'execute_callback' => function ( $input ) {
640650
return array( 'status' => 'success' );
641651
},
642652
'permission_callback' => '__return_true',
643-
'meta' => array( 'type' => 'tool' ),
653+
'meta' => array( 'type' => 'tool' ),
644654
)
645655
);
646656

@@ -665,18 +675,18 @@ public function test_ability_without_type_defaults_to_tool(): void {
665675
wp_register_ability(
666676
'test/no-type',
667677
array(
668-
'label' => 'No Type',
669-
'description' => 'Ability without type',
670-
'execute_callback' => function( $input ) {
678+
'label' => 'No Type',
679+
'description' => 'Ability without type',
680+
'execute_callback' => function ( $input ) {
671681
return array( 'executed' => true );
672682
},
673683
'permission_callback' => '__return_true',
674-
'meta' => array(), // No type specified
684+
'meta' => array(), // No type specified
675685
)
676686
);
677687

678688
// Should require POST (default tool behavior)
679-
$get_request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/no-type/run' );
689+
$get_request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/no-type/run' );
680690
$get_response = $this->server->dispatch( $get_request );
681691
$this->assertEquals( 405, $get_response->get_status() );
682692

@@ -699,7 +709,7 @@ public function test_permission_check_passes_when_callback_not_set(): void {
699709
array(
700710
'label' => 'No Permission Callback',
701711
'description' => 'Ability without permission callback',
702-
'execute_callback' => function( $input ) {
712+
'execute_callback' => function ( $input ) {
703713
return array( 'executed' => true );
704714
},
705715
'meta' => array( 'type' => 'tool' ),
@@ -730,31 +740,31 @@ public function test_empty_input_handling(): void {
730740
wp_register_ability(
731741
'test/resource-empty',
732742
array(
733-
'label' => 'Resource Empty',
734-
'description' => 'Resource with empty input',
735-
'execute_callback' => function( $input ) {
743+
'label' => 'Resource Empty',
744+
'description' => 'Resource with empty input',
745+
'execute_callback' => function ( $input ) {
736746
return array( 'input_was_empty' => empty( $input ) );
737747
},
738748
'permission_callback' => '__return_true',
739-
'meta' => array( 'type' => 'resource' ),
749+
'meta' => array( 'type' => 'resource' ),
740750
)
741751
);
742752

743753
wp_register_ability(
744754
'test/tool-empty',
745755
array(
746-
'label' => 'Tool Empty',
747-
'description' => 'Tool with empty input',
748-
'execute_callback' => function( $input ) {
756+
'label' => 'Tool Empty',
757+
'description' => 'Tool with empty input',
758+
'execute_callback' => function ( $input ) {
749759
return array( 'input_was_empty' => empty( $input ) );
750760
},
751761
'permission_callback' => '__return_true',
752-
'meta' => array( 'type' => 'tool' ),
762+
'meta' => array( 'type' => 'tool' ),
753763
)
754764
);
755765

756766
// Test GET with no input parameter
757-
$get_request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/resource-empty/run' );
767+
$get_request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/resource-empty/run' );
758768
$get_response = $this->server->dispatch( $get_request );
759769
$this->assertEquals( 200, $get_response->get_status() );
760770
$this->assertTrue( $get_response->get_data()['input_was_empty'] );
@@ -813,13 +823,13 @@ public function test_php_type_strings_in_input(): void {
813823
wp_register_ability(
814824
'test/echo',
815825
array(
816-
'label' => 'Echo',
817-
'description' => 'Echoes input',
818-
'execute_callback' => function( $input ) {
826+
'label' => 'Echo',
827+
'description' => 'Echoes input',
828+
'execute_callback' => function ( $input ) {
819829
return array( 'echo' => $input );
820830
},
821831
'permission_callback' => '__return_true',
822-
'meta' => array( 'type' => 'tool' ),
832+
'meta' => array( 'type' => 'tool' ),
823833
)
824834
);
825835

@@ -854,13 +864,13 @@ public function test_mixed_encoding_in_input(): void {
854864
wp_register_ability(
855865
'test/echo-encoding',
856866
array(
857-
'label' => 'Echo Encoding',
858-
'description' => 'Echoes input with encoding',
859-
'execute_callback' => function( $input ) {
867+
'label' => 'Echo Encoding',
868+
'description' => 'Echoes input with encoding',
869+
'execute_callback' => function ( $input ) {
860870
return array( 'echo' => $input );
861871
},
862872
'permission_callback' => '__return_true',
863-
'meta' => array( 'type' => 'tool' ),
873+
'meta' => array( 'type' => 'tool' ),
864874
)
865875
);
866876

@@ -916,15 +926,15 @@ public function test_invalid_http_methods( string $method ): void {
916926
array(
917927
'label' => 'Method Test',
918928
'description' => 'Test ability for HTTP method validation',
919-
'execute_callback' => function() {
929+
'execute_callback' => function () {
920930
return array( 'success' => true );
921931
},
922932
'permission_callback' => '__return_true', // No permission requirements
923933
'meta' => array( 'type' => 'tool' ),
924934
)
925935
);
926936

927-
$request = new WP_REST_Request( $method, '/wp/v2/abilities/test/method-test/run' );
937+
$request = new WP_REST_Request( $method, '/wp/v2/abilities/test/method-test/run' );
928938
$response = $this->server->dispatch( $request );
929939

930940
// Tool abilities should only accept POST, so these should return 405
@@ -937,10 +947,9 @@ public function test_invalid_http_methods( string $method ): void {
937947
* Test OPTIONS method handling.
938948
*/
939949
public function test_options_method_handling(): void {
940-
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/abilities/test/calculator/run' );
950+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/abilities/test/calculator/run' );
941951
$response = $this->server->dispatch( $request );
942952
// OPTIONS requests return 200 with allowed methods
943953
$this->assertEquals( 200, $response->get_status() );
944954
}
945-
946955
}

0 commit comments

Comments
 (0)