Passed
Branch ci (072b24)
by litefeel
05:02
created

Writing_On_GitHub_Base_Client_Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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 string
22
     */
23
    const BRANCH_OPTION_VALUE = 'master';
24
25
    /**
26
     * @var array
27
     */
28
    protected static $responses = array();
29
30
    /**
31
     * @var array
32
     */
33
    protected static $validations = array();
34
35
    public function setUp() {
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
36
        parent::setUp();
37
38
        WP_HTTP_TestCase::init();
39
        update_option( 'wogh_repository', self::REPO_OPTION_VALUE );
40
        update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE );
41
        update_option( 'wogh_host', self::HOST_OPTION_VALUE );
42
        update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE );
43
        $this->http_responder = array( $this, 'mock_github_api' );
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this, 'mock_github_api') of type array<integer,Writing_On...ase_Client_Test|string> is incompatible with the declared type false|callable of property $http_responder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * This does some checks and fails the test if something is wrong
48
     * or returns intended mock data for the given endpoint + method.
49
     *
50
     * @return void|string
51
     */
52
    public function mock_github_api( $request, $url ) {
53
        $host_length = strlen( self::HOST_OPTION_VALUE );
54
55
        if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) {
56
            $this->assertTrue( false, 'Called wrong host.' );
57
        }
58
59
        if (
60
            ! isset( $request['headers']['Authorization'] ) ||
61
            'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization']
62
        ) {
63
            $this->assertTrue( false, 'Missing authorization key.' );
64
        }
65
66
        $url = explode( '/', substr( $url, $host_length + 1 ) );
67
68
        if ( 'repos' !== $url[0] ) {
69
            $this->assertTrue( false, 'Called wrong endpoint.' );
70
        }
71
72
        $repo = $url[1] . '/' . $url[2];
73
74
        if ( self::REPO_OPTION_VALUE !== $repo ) {
75
            $this->assertTrue( false, 'Called wrong repo.' );
76
        }
77
78
        // git api
79
        $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 );
80
        array_unshift( $parts, strtolower( $request['method'] ) );
81
        $endpoint = implode( '_', $parts );
82
        $endpoint = str_replace( '?recursive=1', '', $endpoint );
83
        $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' );
84
85
        return static::$responses[ $endpoint ];
86
    }
87
88
    protected function set_get_refs_heads_master( $succeed ) {
89
        $this->set_endpoint(
90
            function ( $request ) {
91
                if ( '[]' === $request['body'] ) {
92
                    return false;
93
                }
94
95
                return true;
96
            }, $succeed ? '200 OK' : '404 Not Found', $succeed
97
        );
98
    }
99
100
    protected function set_get_commits( $succeed ) {
101
        $this->set_endpoint(
102
            function ( $request ) {
103
                if ( '[]' === $request['body'] ) {
104
                    return false;
105
                }
106
107
                return true;
108
            }, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e'
109
        );
110
    }
111
112
    protected function set_get_trees( $succeed, $sha = '' ) {
113
        $this->set_endpoint(
114
            function ( $request ) {
115
                if ( '[]' === $request['body'] ) {
116
                    return false;
117
                }
118
119
                return true;
120
            }, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed,
121
            $sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626'
122
        );
123
    }
124
125
    protected function set_get_blobs( $succeed ) {
126
        $shas = array(
127
            '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25',
128
            '8d9b2e6fd93761211dc03abd71f4a9189d680fd0',
129
            '2d73165945b0ccbe4932f1363457986b0ed49f19',
130
        );
131
132
        foreach ( $shas as $sha ) {
133
            $this->set_endpoint(
134
                function ( $request ) {
135
                    if ( '[]' === $request['body'] ) {
136
                        return false;
137
                    }
138
139
                    return true;
140
                }, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha
141
            );
142
        }
143
    }
144
145
    protected function set_post_trees( $succeed ) {
146
        $this->set_endpoint(
147
            function ( $request ) {
148
                $body = json_decode( $request['body'], true );
149
150
                if ( ! isset( $body['tree'] ) ) {
151
                    return false;
152
                }
153
154
                if ( 1 !== count( $body['tree'] ) ) {
155
                    return false;
156
                }
157
158
                $blob = reset( $body['tree'] );
159
160
                if (
161
                    ! isset( $blob['path'] ) ||
162
                    ! isset( $blob['type'] ) ||
163
                    ! isset( $blob['content'] ) ||
164
                    ! isset( $blob['mode'] )
165
                ) {
166
                    return false;
167
                }
168
169
                return true;
170
            },
171
            $succeed ? '201 Created' : '404 Not Found',
172
            $succeed
173
        );
174
    }
175
176
    protected function set_post_commits( $succeed, $anonymous = true ) {
177
        $this->set_endpoint(
178
            function ( $request ) use ( $anonymous ) {
179
                $body = json_decode( $request['body'], true );
180
181
                if (
182
                    ! isset( $body['tree'] ) ||
183
                    ! isset( $body['message'] ) ||
184
                    ! isset( $body['parents'] ) ||
185
                    ! isset( $body['author'] )
186
                ) {
187
                    return false;
188
                }
189
190
                if ( 1 !== count( $body['parents'] ) ) {
191
                    return false;
192
                }
193
194
                if ( ! $anonymous ) {
195
                    if (
196
                        'James DiGioia' !== $body['author']['name'] ||
197
                        '[email protected]' !== $body['author']['email']
198
                    ) {
199
                        return false;
200
                    }
201
                } else {
202
                    if (
203
                        'Anonymous' !== $body['author']['name'] ||
204
                        '[email protected]' !== $body['author']['email']
205
                    ) {
206
                        return false;
207
                    }
208
                }
209
210
                return true;
211
            },
212
            $succeed ? '201 Created' : '404 Not Found',
213
            $succeed
214
        );
215
    }
216
217
    protected function set_get_compare( $succeed ) {
218
        $this->set_endpoint(
219
            function ( $request ) {
220
                if ( '[]' === $request['body'] ) {
221
                    return false;
222
                }
223
                return true;
224
            },
225
            $succeed ? '200 OK' : '404 Not Found',
226
            $succeed,
227
            '861f87e8851b8debb78db548269d29f8da4d94ac...master'
228
        );
229
    }
230
231
    protected function set_delete_contents( $succeed, $filepath ) {
232
        $this->set_endpoint(
233
            function ( $request ) {
234
                if ( '[]' === $request['body'] ) {
235
                    return false;
236
                }
237
                return true;
238
            },
239
            $succeed ? '200 OK' : '404 Not Found',
240
            $succeed,
241
            $filepath
242
        );
243
    }
244
245
    protected function set_patch_refs_heads_master( $succeed ) {
246
        $this->set_endpoint(
247
            function ( $request ) {
248
                $body = json_decode( $request['body'], true );
249
250
                if ( ! isset( $body['sha'] ) ) {
251
                    return false;
252
                }
253
254
                return true;
255
            },
256
            $succeed ? '201 Created' : '404 Not Found',
257
            $succeed
258
        );
259
    }
260
261
    private function set_endpoint( $validation, $status, $succeed, $sha = '' ) {
262
        list( , $caller ) = debug_backtrace( false );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $options of debug_backtrace(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

262
        list( , $caller ) = debug_backtrace( /** @scrutinizer ignore-type */ false );
Loading history...
263
        $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' );
264
265
        static::$validations[ $endpoint ] = $validation;
266
267
        static::$responses[ $endpoint ] = array(
268
            'headers' => array(
269
                'status' => $status,
270
            ),
271
            'body'    => file_get_contents(
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
272
                $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json'
273
            ),
274
        );
275
    }
276
}
277