Completed
Push — add/signature-error-reporting ( 95a087...072d13 )
by
unknown
18:31 queued 09:22
created

Test_Status   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 177
Duplicated Lines 34.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 61
loc 177
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A tearDown() 0 3 1
A test_is_development_mode_default() 11 11 1
A test_is_development_mode_filter_true() 10 10 1
A test_is_development_mode_filter_bool() 10 10 1
A test_is_development_mode_localhost() 12 12 1
A test_is_development_mode_constant() 0 17 1
A mock_function_with_args() 9 21 3
A mock_filters() 0 3 1
A mock_constants() 0 10 1
A mock_function() 9 9 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
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\Status;
6
use PHPUnit\Framework\TestCase;
7
use phpmock\Mock;
8
use phpmock\MockBuilder;
9
10
class Test_Status extends TestCase {
11
	/**
12
	 * Default site URL.
13
	 *
14
	 * @var string
15
	 */
16
	private $site_url = 'https://yourjetpack.blog';
17
18
	/**
19
	 * Test setup.
20
	 */
21
	public function setUp() {
22
		$this->status = new Status();
23
	}
24
25
	/**
26
	 * Test teardown.
27
	 */
28
	public function tearDown() {
29
		Mock::disableAll();
30
	}
31
32
	/**
33
	 * @covers Automattic\Jetpack\Status::is_development_mode
34
	 */
35 View Code Duplication
	public function test_is_development_mode_default() {
36
		$this->mock_function( 'site_url', $this->site_url );
37
		$filters_mock = $this->mock_filters( array(
38
			array( 'jetpack_development_mode', false, false ),
39
			array( 'jetpack_development_mode', true, true ),
40
		) );
41
42
		$this->assertFalse( $this->status->is_development_mode() );
43
44
		$filters_mock->disable();
45
	}
46
47
	/**
48
	 * @covers Automattic\Jetpack\Status::is_development_mode
49
	 */
50 View Code Duplication
	public function test_is_development_mode_filter_true() {
51
		$this->mock_function( 'site_url', $this->site_url );
52
		$filters_mock = $this->mock_filters( array(
53
			array( 'jetpack_development_mode', false, true ),
54
		) );
55
56
		$this->assertTrue( $this->status->is_development_mode() );
57
58
		$filters_mock->disable();
59
	}
60
61
	/**
62
	 * @covers Automattic\Jetpack\Status::is_development_mode
63
	 */
64 View Code Duplication
	public function test_is_development_mode_filter_bool() {
65
		$this->mock_function( 'site_url', $this->site_url );
66
		$filters_mock = $this->mock_filters( array(
67
			array( 'jetpack_development_mode', false, 0 ),
68
		) );
69
70
		$this->assertFalse( $this->status->is_development_mode() );
71
		
72
		$filters_mock->disable();
73
	}
74
75
	/**
76
	 * @covers Automattic\Jetpack\Status::is_development_mode
77
	 */
78 View Code Duplication
	public function test_is_development_mode_localhost() {
79
		$this->mock_function( 'site_url', 'localhost' );
80
		
81
		$filters_mock = $this->mock_filters( array(
82
			array( 'jetpack_development_mode', false, false ),
83
			array( 'jetpack_development_mode', true, true ),
84
		) );
85
86
		$this->assertTrue( $this->status->is_development_mode() );
87
88
		$filters_mock->disable();
89
	}
90
91
    /**
92
     * @covers Automattic\Jetpack\Status::is_development_mode
93
     *
94
     * @runInSeparateProcess
95
     */	
96
	public function test_is_development_mode_constant() {
97
		$this->mock_function( 'site_url', $this->site_url );
98
		$filters_mock = $this->mock_filters( array(
99
			array( 'jetpack_development_mode', false, false ),
100
			array( 'jetpack_development_mode', true, true ),
101
		) );
102
		$constants_mocks = $this->mock_constants( array(
103
			array( '\\JETPACK_DEV_DEBUG', true ),
104
		) );
105
106
		$this->assertTrue( $this->status->is_development_mode() );
107
108
		array_map( function( $mock ) {
109
			$mock->disable();
110
		}, $constants_mocks );
111
		$filters_mock->disable();
112
	}
113
114
	/**
115
	 * Mock a global function with particular arguments and make it return a certain value.
116
	 *
117
	 * @param string $function_name Name of the function.
118
	 * @param array  $args          Array of argument sets, last value of each set is used as a return value.
119
	 * @return phpmock\Mock The mock object.
120
	 */
121
	protected function mock_function_with_args( $function_name, $args = array() ) {
122
		$builder = new MockBuilder();
123
		$builder->setNamespace( __NAMESPACE__ )
124
			->setName( $function_name )
125
			->setFunction(
126 View Code Duplication
				function() use ( &$args ) {
127
					$current_args = func_get_args();
128
129
					foreach ( $args as $arg ) {
130
						if ( array_slice( $arg, 0, -1 ) === $current_args ) {
131
							return array_pop( $arg );
132
						}
133
					}
134
				}
135
			);
136
137
		$mock = $builder->build();
138
		$mock->enable();
139
140
		return $mock;
141
	}
142
143
	/**
144
	 * Mock a set of filters.
145
	 *
146
	 * @param array $args Array of filters with their arguments.
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
147
	 * @return phpmock\Mock The mock object.
148
	 */
149
	protected function mock_filters( $filters = array() ) {
150
		return $this->mock_function_with_args( 'apply_filters', $filters );
151
	}
152
153
	/**
154
	 * Mock a set of constants.
155
	 *
156
	 * @param array $args Array of sets with constants and their respective values.
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
157
	 * @return phpmock\Mock The mock object.
158
	 */
159
	protected function mock_constants( $constants = array() ) {
160
		$prepare_constant = function( $constant ) {
161
			return array( $constant[0], true );
162
		};
163
164
		return [
165
			$this->mock_function_with_args( 'defined', array_map( $prepare_constant, $constants ) ),
166
			$this->mock_function_with_args( 'constant', $constants )
167
		];
168
	}
169
170
	/**
171
	 * Mock a global function and make it return a certain value.
172
	 *
173
	 * @param string $function_name Name of the function.
174
	 * @param mixed  $return_value  Return value of the function.
175
	 * @return phpmock\Mock The mock object.
176
	 */
177 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
178
		$builder = new MockBuilder();
179
		$builder->setNamespace( __NAMESPACE__ )
180
			->setName( $function_name )
181
			->setFunction( function() use ( &$return_value ) {
182
				return $return_value;
183
			} );
184
		return $builder->build()->enable();
185
	}
186
}
187