|
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. |
|
|
|
|
|
|
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. |
|
|
|
|
|
|
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
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.