Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
3 | class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
||
4 | |||
5 | public function setUp() { |
||
33 | |||
34 | View Code Duplication | public function test_validate_schema_type_integer() { |
|
35 | |||
36 | $this->assertTrue( |
||
37 | rest_validate_request_arg( '123', $this->request, 'someinteger' ) |
||
38 | ); |
||
39 | |||
40 | $this->assertErrorResponse( |
||
41 | 'rest_invalid_param', |
||
42 | rest_validate_request_arg( 'abc', $this->request, 'someinteger' ) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | public function test_validate_schema_type_boolean() { |
||
64 | |||
65 | View Code Duplication | public function test_validate_schema_type_string() { |
|
66 | |||
67 | $this->assertTrue( |
||
68 | rest_validate_request_arg( '123', $this->request, 'somestring' ) |
||
69 | ); |
||
70 | |||
71 | $this->assertErrorResponse( |
||
72 | 'rest_invalid_param', |
||
73 | rest_validate_request_arg( array( 'foo' => 'bar' ), $this->request, 'somestring' ) |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | public function test_validate_schema_enum() { |
||
78 | |||
79 | $this->assertTrue( |
||
80 | rest_validate_request_arg( 'a', $this->request, 'someenum' ) |
||
81 | ); |
||
82 | |||
83 | $this->assertErrorResponse( |
||
84 | 'rest_invalid_param', |
||
85 | rest_validate_request_arg( 'd', $this->request, 'someenum' ) |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | public function test_validate_schema_format_email() { |
||
90 | |||
91 | $this->assertTrue( |
||
92 | rest_validate_request_arg( '[email protected]', $this->request, 'someemail' ) |
||
93 | ); |
||
94 | |||
95 | $this->assertErrorResponse( |
||
96 | 'rest_invalid_email', |
||
97 | rest_validate_request_arg( 'd', $this->request, 'someemail' ) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | public function test_validate_schema_format_date_time() { |
||
102 | |||
103 | $this->assertTrue( |
||
104 | rest_validate_request_arg( '2010-01-01T12:00:00', $this->request, 'somedate' ) |
||
105 | ); |
||
106 | |||
107 | $this->assertErrorResponse( |
||
108 | 'rest_invalid_date', |
||
109 | rest_validate_request_arg( '2010-18-18T12:00:00', $this->request, 'somedate' ) |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | View Code Duplication | public function test_get_endpoint_args_for_item_schema_description() { |
|
119 | |||
120 | View Code Duplication | public function test_get_endpoint_args_for_item_schema_arg_options() { |
|
121 | |||
122 | $controller = new WP_REST_Test_Controller(); |
||
123 | $args = $controller->get_endpoint_args_for_item_schema(); |
||
124 | |||
128 | |||
129 | public function test_get_endpoint_args_for_item_schema_default_value() { |
||
137 | |||
138 | public $rest_the_post_filter_apply_count = 0; |
||
139 | |||
140 | public function test_get_post() { |
||
153 | |||
154 | public function filter_rest_the_post_for_test_get_post( $post, $post_id ) { |
||
161 | } |
||
162 |