Completed
Push — add/changelog-960 ( c446e0 )
by Jeremy
121:05 queued 110:36
created

WP_Test_Jetpack_Sync_Json_Api_Endpoints   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 101
Duplicated Lines 77.23 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 78
loc 101
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set_globals() 0 5 1
A setUp() 13 13 2
A test_modify_sync_health() 32 32 1
A test_modify_sync_health_error() 33 33 1

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Jetpack `sites/%s/sync` endpoint unit tests.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
require_jetpack_file( 'class.json-api-endpoints.php' );
9
10
/**
11
 * Jetpack `site/%s/sync` endpoint unit tests.
12
 */
13
class WP_Test_Jetpack_Sync_Json_Api_Endpoints extends WP_UnitTestCase {
14
15
	/**
16
	 * Inserts globals needed to initialize the endpoint.
17
	 */
18
	private function set_globals() {
19
		$_SERVER['REQUEST_METHOD'] = 'Get';
20
		$_SERVER['HTTP_HOST']      = '127.0.0.1';
21
		$_SERVER['REQUEST_URI']    = '/';
22
	}
23
24
	/**
25
	 * Prepare the environment for the test.
26
	 */
27 View Code Duplication
	public function setUp() {
28
		global $blog_id;
29
30
		if ( ! defined( 'WPCOM_JSON_API__BASE' ) ) {
31
			define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
32
		}
33
34
		parent::setUp();
35
36
		$this->set_globals();
37
38
		WPCOM_JSON_API::init()->token_details = array( 'blog_id' => $blog_id );
39
	}
40
41
	/**
42
	 * Unit test for the `/sites/%s/sync/health` endpoint with valid input.
43
	 */
44 View Code Duplication
	public function test_modify_sync_health() {
45
		global $blog_id;
46
47
		$endpoint = new Jetpack_JSON_API_Sync_Modify_Health_Endpoint(
48
			array(
49
				'description'             => 'Update sync health',
50
				'method'                  => 'POST',
51
				'group'                   => '__do_not_document',
52
				'path'                    => '/sites/%s/sync/health',
53
				'stat'                    => 'write-sync-health',
54
				'allow_jetpack_site_auth' => true,
55
				'path_labels'             => array(
56
					'$site' => '(int|string) The site ID, The site domain',
57
				),
58
				'request_format'          => array(
59
					'status' => '(string) Sync Health Status of site',
60
				),
61
				'response_format'         => array(
62
					'response' => '(string) Current Sync Health ',
63
				),
64
				'example_request'         => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/health',
65
			)
66
		);
67
68
		// set input.
69
		$endpoint->api->post_body    = '{ "status": "in_sync" }';
70
		$endpoint->api->content_type = 'application/json';
71
72
		$response = $endpoint->callback( 'sync/health', $blog_id );
73
74
		$this->assertEquals( 'in_sync', $response['success'] );
75
	}
76
77
	/**
78
	 * Unit test for the `/sites/%s/sync/health` endpoint with invalid input.
79
	 */
80 View Code Duplication
	public function test_modify_sync_health_error() {
81
		global $blog_id;
82
83
		$endpoint = new Jetpack_JSON_API_Sync_Modify_Health_Endpoint(
84
			array(
85
				'description'             => 'Update sync health',
86
				'method'                  => 'POST',
87
				'group'                   => '__do_not_document',
88
				'path'                    => '/sites/%s/sync/health',
89
				'stat'                    => 'write-sync-health',
90
				'allow_jetpack_site_auth' => true,
91
				'path_labels'             => array(
92
					'$site' => '(int|string) The site ID, The site domain',
93
				),
94
				'request_format'          => array(
95
					'status' => '(string) Sync Health Status of site',
96
				),
97
				'response_format'         => array(
98
					'response' => '(string) Current Sync Health ',
99
				),
100
				'example_request'         => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/health',
101
			)
102
		);
103
104
		// set input.
105
		$endpoint->api->post_body    = '{ "status": "bad_falue" }';
106
		$endpoint->api->content_type = 'application/json';
107
108
		$response = $endpoint->callback( 'sync/health', $blog_id );
109
110
		// Verify WP_Error returned on invalid stati.
111
		$this->assertInstanceOf( 'WP_Error', $response );
112
	}
113
}
114