| Total Complexity | 54 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Writing_On_GitHub_Base_Client_Test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Writing_On_GitHub_Base_Client_Test, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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() { |
||
|
|
|||
| 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' ); |
||
| 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 ) { |
||
| 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 = '' ) { |
||
| 273 | ), |
||
| 274 | ); |
||
| 275 | } |
||
| 276 | } |
||
| 277 |