1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class Writing_On_GitHub_Base_Client_Test extends Writing_On_GitHub_TestCase { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @var string |
7
|
|
|
*/ |
8
|
|
|
const HOST_OPTION_VALUE = 'https://api.github.com'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
const REPO_OPTION_VALUE = 'woghtest/wogh-test'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const TOKEN_OPTION_VALUE = 'the-token'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected static $responses = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $validations = array(); |
29
|
|
|
|
30
|
|
|
public function setUp() { |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
WP_HTTP_TestCase::init(); |
34
|
|
|
update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
35
|
|
|
update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
36
|
|
|
update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
37
|
|
|
$this->http_responder = array( $this, 'mock_github_api' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This does some checks and fails the test if something is wrong |
42
|
|
|
* or returns intended mock data for the given endpoint + method. |
43
|
|
|
* |
44
|
|
|
* @return void|string |
45
|
|
|
*/ |
46
|
|
|
public function mock_github_api( $request, $url ) { |
47
|
|
|
$host_length = strlen( self::HOST_OPTION_VALUE ); |
48
|
|
|
|
49
|
|
|
if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
50
|
|
|
$this->assertTrue( false, 'Called wrong host.' ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( |
54
|
|
|
! isset( $request['headers']['Authorization'] ) || |
55
|
|
|
'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
56
|
|
|
) { |
57
|
|
|
$this->assertTrue( false, 'Missing authorization key.' ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$url = explode( '/', substr( $url, $host_length + 1 ) ); |
61
|
|
|
|
62
|
|
|
if ( 'repos' !== $url[0] ) { |
63
|
|
|
$this->assertTrue( false, 'Called wrong endpoint.' ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$repo = $url[1] . '/' . $url[2]; |
67
|
|
|
|
68
|
|
|
if ( self::REPO_OPTION_VALUE !== $repo ) { |
69
|
|
|
$this->assertTrue( false, 'Called wrong repo.' ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$parts = array_slice( $url, 4 ); |
73
|
|
|
array_unshift( $parts, strtolower( $request['method'] ) ); |
74
|
|
|
$endpoint = implode( '_', $parts ); |
75
|
|
|
$endpoint = str_replace( '?recursive=1', '', $endpoint ); |
76
|
|
|
$this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
77
|
|
|
|
78
|
|
|
return static::$responses[ $endpoint ]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
protected function set_get_refs_heads_master( $succeed ) { |
|
|
|
|
82
|
|
|
$this->set_endpoint( |
83
|
|
|
function ( $request ) { |
84
|
|
|
if ( '[]' === $request['body'] ) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
}, $succeed ? '200 OK' : '404 Not Found', $succeed |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
protected function set_get_commits( $succeed ) { |
|
|
|
|
94
|
|
|
$this->set_endpoint( |
95
|
|
|
function ( $request ) { |
96
|
|
|
if ( '[]' === $request['body'] ) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
}, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
protected function set_get_trees( $succeed, $sha = '' ) { |
|
|
|
|
106
|
|
|
$this->set_endpoint( |
107
|
|
|
function ( $request ) { |
108
|
|
|
if ( '[]' === $request['body'] ) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
}, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed, |
114
|
|
|
$sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function set_get_blobs( $succeed ) { |
119
|
|
|
$shas = array( |
120
|
|
|
'9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
121
|
|
|
'8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
122
|
|
|
'2d73165945b0ccbe4932f1363457986b0ed49f19', |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
foreach ( $shas as $sha ) { |
126
|
|
|
$this->set_endpoint( |
127
|
|
|
function ( $request ) { |
128
|
|
|
if ( '[]' === $request['body'] ) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
}, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function set_post_trees( $succeed ) { |
139
|
|
|
$this->set_endpoint( |
140
|
|
|
function ( $request ) { |
141
|
|
|
$body = json_decode( $request['body'], true ); |
142
|
|
|
|
143
|
|
|
if ( ! isset( $body['tree'] ) ) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ( 1 !== count( $body['tree'] ) ) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$blob = reset( $body['tree'] ); |
152
|
|
|
|
153
|
|
|
if ( |
154
|
|
|
! isset( $blob['path'] ) || |
155
|
|
|
! isset( $blob['type'] ) || |
156
|
|
|
! isset( $blob['content'] ) || |
157
|
|
|
! isset( $blob['mode'] ) |
158
|
|
|
) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return true; |
163
|
|
|
}, |
164
|
|
|
$succeed ? '201 Created' : '404 Not Found', |
165
|
|
|
$succeed |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function set_post_commits( $succeed, $anonymous = true ) { |
170
|
|
|
$this->set_endpoint( |
171
|
|
|
function ( $request ) use ( $anonymous ) { |
172
|
|
|
$body = json_decode( $request['body'], true ); |
173
|
|
|
|
174
|
|
|
if ( |
175
|
|
|
! isset( $body['tree'] ) || |
176
|
|
|
! isset( $body['message'] ) || |
177
|
|
|
! isset( $body['parents'] ) || |
178
|
|
|
! isset( $body['author'] ) |
179
|
|
|
) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ( 1 !== count( $body['parents'] ) ) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ( ! $anonymous ) { |
188
|
|
View Code Duplication |
if ( |
|
|
|
|
189
|
|
|
'James DiGioia' !== $body['author']['name'] || |
190
|
|
|
'[email protected]' !== $body['author']['email'] |
191
|
|
|
) { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
View Code Duplication |
} else { |
|
|
|
|
195
|
|
|
if ( |
196
|
|
|
'Anonymous' !== $body['author']['name'] || |
197
|
|
|
'[email protected]' !== $body['author']['email'] |
198
|
|
|
) { |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return true; |
204
|
|
|
}, |
205
|
|
|
$succeed ? '201 Created' : '404 Not Found', |
206
|
|
|
$succeed |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function set_patch_refs_heads_master( $succeed ) { |
211
|
|
|
$this->set_endpoint( |
212
|
|
|
function ( $request ) { |
213
|
|
|
$body = json_decode( $request['body'], true ); |
214
|
|
|
|
215
|
|
|
if ( ! isset( $body['sha'] ) ) { |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return true; |
220
|
|
|
}, |
221
|
|
|
$succeed ? '201 Created' : '404 Not Found', |
222
|
|
|
$succeed |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
227
|
|
|
list( , $caller ) = debug_backtrace( false ); |
228
|
|
|
$endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
229
|
|
|
|
230
|
|
|
static::$validations[ $endpoint ] = $validation; |
231
|
|
|
|
232
|
|
|
static::$responses[ $endpoint ] = array( |
233
|
|
|
'headers' => array( |
234
|
|
|
'status' => $status, |
235
|
|
|
), |
236
|
|
|
'body' => file_get_contents( |
237
|
|
|
$this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
238
|
|
|
), |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.