Completed
Push — renovate/vercel-ncc-0.x ( fdb380...917c9e )
by
unknown
149:32 queued 140:15
created

WP_Test_Jetpack_Modules_Json_Api_Endpoints   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 19.12 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 13
loc 68
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set_globals() 0 5 1
A setUp() 13 13 2
A test_get_modules_v1_2() 0 37 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
 * Tests for the `sites/%s/jetpack/modules` endpoints.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
require_jetpack_file( 'class.json-api.php' );
9
require_jetpack_file( 'class.json-api-endpoints.php' );
10
11
/**
12
 * Tests for the `sites/%s/jetpack/modules` endpoints.
13
 */
14
class WP_Test_Jetpack_Modules_Json_Api_Endpoints extends WP_UnitTestCase {
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 `v1.2/modules/%s` endpoint.
43
	 */
44
	public function test_get_modules_v1_2() {
45
		global $blog_id;
46
47
		$endpoint = new Jetpack_JSON_API_Modules_List_V1_2_Endpoint(
48
			array(
49
				'description'             => 'Get the list of available Jetpack modules on your site',
50
				'method'                  => 'GET',
51
				'path'                    => '/sites/%s/jetpack/modules',
52
				'stat'                    => 'jetpack:modules',
53
				'min_version'             => '1.2',
54
				'path_labels'             => array(
55
					'$site' => '(int|string) The site ID, The site domain',
56
				),
57
				'response_format'         => array(
58
					'modules' => '(array) An array of module objects.',
59
				),
60
				'allow_jetpack_site_auth' => true,
61
				'example_request_data'    => array(
62
					'headers' => array(
63
						'authorization' => 'Bearer YOUR_API_TOKEN',
64
					),
65
				),
66
				'example_request'         => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/jetpack/modules',
67
			)
68
		);
69
70
		$response = $endpoint->callback( '', $blog_id );
71
72
		$this->assertArrayHasKey( 'modules', $response );
73
		$this->assertTrue( is_array( $response['modules'] ) );
74
75
		$module = array_pop( $response['modules'] );
76
		$this->assertArrayHasKey( 'name', $module );
77
		$this->assertArrayHasKey( 'description', $module );
78
		$this->assertArrayHasKey( 'activated', $module );
79
		$this->assertArrayHasKey( 'available', $module );
80
	}
81
}
82