| Total Complexity | 55 |
| Total Lines | 464 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 2 |
Complex classes like HTTPTest 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 HTTPTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class HTTPTest extends ServerTest |
||
| 15 | { |
||
| 16 | protected $expectHttp2 = false; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Returns all test methods from the base class, except the ones which failed already |
||
| 20 | * |
||
| 21 | * @todo (re)introduce skipping of tests which failed when executed individually even if test runs happen as separate processes |
||
| 22 | * @todo reintroduce skipping of tests within the loop |
||
| 23 | */ |
||
| 24 | public function getSingleHttpTestMethods() |
||
| 25 | { |
||
| 26 | $unsafeMethods = array( |
||
| 27 | 'testCatchExceptions', 'testUtf8Method', 'testServerComments', |
||
| 28 | 'testExoticCharsetsRequests', 'testExoticCharsetsRequests2', 'testExoticCharsetsRequests3', |
||
| 29 | 'testWrapInexistentUrl', |
||
| 30 | ); |
||
| 31 | |||
| 32 | $methods = array(); |
||
| 33 | foreach(get_class_methods('ServerTest') as $method) |
||
| 34 | { |
||
| 35 | if (strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods)) |
||
| 36 | { |
||
| 37 | if (!isset(self::$failed_tests[$method])) { |
||
| 38 | $methods[$method] = array($method); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | return $methods; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @dataProvider getSingleHttpTestMethods |
||
| 48 | * @param string $method |
||
| 49 | */ |
||
| 50 | public function testDeflate($method) |
||
| 51 | { |
||
| 52 | if (!function_exists('gzdeflate')) |
||
| 53 | { |
||
| 54 | $this->markTestSkipped('Zlib missing: cannot test deflate functionality'); |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | $this->client->accepted_compression = array('deflate'); |
||
| 59 | $this->client->request_compression = 'deflate'; |
||
| 60 | |||
| 61 | $this->$method(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @dataProvider getSingleHttpTestMethods |
||
| 66 | * @param string $method |
||
| 67 | */ |
||
| 68 | public function testGzip($method) |
||
| 69 | { |
||
| 70 | if (!function_exists('gzdeflate')) |
||
| 71 | { |
||
| 72 | $this->markTestSkipped('Zlib missing: cannot test gzip functionality'); |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->client->accepted_compression = array('gzip'); |
||
| 77 | $this->client->request_compression = 'gzip'; |
||
| 78 | |||
| 79 | $this->$method(); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function testKeepAlives() |
||
| 83 | { |
||
| 84 | if (!function_exists('curl_init')) |
||
| 85 | { |
||
| 86 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->method = 'http11'; |
||
| 91 | $this->client->method = 'http11'; |
||
| 92 | $this->client->keepalive = true; |
||
| 93 | |||
| 94 | // to successfully test keepalive, we have to reuse the same client for all tests, we can not recreate one on setup/teardown... |
||
| 95 | foreach ($this->getSingleHttpTestMethods() as $methods) { |
||
| 96 | $method = $methods[0]; |
||
| 97 | $this->$method(); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @dataProvider getSingleHttpTestMethods |
||
| 103 | * @param string $method |
||
| 104 | */ |
||
| 105 | public function testProxy($method) |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @dataProvider getSingleHttpTestMethods |
||
| 120 | * @param string $method |
||
| 121 | */ |
||
| 122 | public function testHttp11($method) |
||
| 123 | { |
||
| 124 | if (!function_exists('curl_init')) |
||
| 125 | { |
||
| 126 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->method = 'http11'; // not an error the double assignment! |
||
| 131 | $this->client->method = 'http11'; |
||
| 132 | $this->client->keepalive = false; |
||
| 133 | |||
| 134 | $this->$method(); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @dataProvider getSingleHttpTestMethods |
||
| 139 | * @param string $method |
||
| 140 | */ |
||
| 141 | public function testHttp10Curl($method) |
||
| 142 | { |
||
| 143 | if (!function_exists('curl_init')) |
||
| 144 | { |
||
| 145 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->method = 'http10'; // not an error the double assignment! |
||
| 150 | $this->client->method = 'http10'; |
||
| 151 | $this->client->keepalive = false; |
||
| 152 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS); |
||
| 153 | |||
| 154 | $this->$method(); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @dataProvider getSingleHttpTestMethods |
||
| 159 | * @param string $method |
||
| 160 | */ |
||
| 161 | public function testHttp11Gzip($method) |
||
| 162 | { |
||
| 163 | if (!function_exists('curl_init')) |
||
| 164 | { |
||
| 165 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | $this->method = 'http11'; // not an error the double assignment! |
||
| 169 | $this->client->method = 'http11'; |
||
| 170 | $this->client->keepalive = false; |
||
| 171 | $this->client->accepted_compression = array('gzip'); |
||
| 172 | $this->client->request_compression = 'gzip'; |
||
| 173 | |||
| 174 | $this->$method(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @dataProvider getSingleHttpTestMethods |
||
| 179 | * @param string $method |
||
| 180 | */ |
||
| 181 | public function testHttp11Deflate($method) |
||
| 182 | { |
||
| 183 | if (!function_exists('curl_init')) |
||
| 184 | { |
||
| 185 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
| 186 | return; |
||
| 187 | } |
||
| 188 | $this->method = 'http11'; // not an error the double assignment! |
||
| 189 | $this->client->method = 'http11'; |
||
| 190 | $this->client->keepalive = false; |
||
| 191 | $this->client->accepted_compression = array('deflate'); |
||
| 192 | $this->client->request_compression = 'deflate'; |
||
| 193 | |||
| 194 | $this->$method(); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @dataProvider getSingleHttpTestMethods |
||
| 199 | * @param string $method |
||
| 200 | */ |
||
| 201 | public function testHttp11Proxy($method) |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @dataProvider getSingleHttpTestMethods |
||
| 224 | * @param string $method |
||
| 225 | */ |
||
| 226 | public function testHttps($method) |
||
| 227 | { |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @dataProvider getSingleHttpTestMethods |
||
| 252 | * @param string $method |
||
| 253 | */ |
||
| 254 | public function testHttpsSocket($method) |
||
| 255 | { |
||
| 256 | if ($this->args['HTTPSSERVER'] == '') |
||
| 257 | { |
||
| 258 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https'); |
||
| 259 | return; |
||
| 260 | } |
||
| 261 | |||
| 262 | /// @todo investigate: can we make this work? |
||
| 263 | if (version_compare(PHP_VERSION, '5.6.0', '<')) |
||
| 264 | { |
||
| 265 | $this->markTestSkipped('HTTPS via Socket known to fail on php 5.5 and earlier'); |
||
| 266 | return; |
||
| 267 | } |
||
| 268 | |||
| 269 | /// @todo investigate: can we make this work? |
||
| 270 | if (version_compare(PHP_VERSION, '5.6.1', '>=') && version_compare(PHP_VERSION, '7.2', '<')) |
||
| 271 | { |
||
| 272 | if (is_readable('/etc/os-release')) { |
||
| 273 | $output = file_get_contents('/etc/os-release'); |
||
| 274 | preg_match('/VERSION="?([0-9]+)/', $output, $matches); |
||
| 275 | $ubuntuVersion = @$matches[1]; |
||
| 276 | } else { |
||
| 277 | exec('uname -a', $output, $retval); |
||
| 278 | preg_match('/ubunutu([0-9]+)/', $output[0], $matches); |
||
| 279 | $ubuntuVersion = @$matches[1]; |
||
| 280 | } |
||
| 281 | if ($ubuntuVersion >= 20) { |
||
| 282 | /// @todo investigate: can we make this work? |
||
| 283 | $this->markTestSkipped('HTTPS via Socket known to fail on php 5.6.1 to 7.1 on Ubuntu 20 and higher'); |
||
| 284 | return; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | $this->client->server = $this->args['HTTPSSERVER']; |
||
| 289 | $this->method = 'https'; |
||
| 290 | $this->client->method = 'https'; |
||
| 291 | $this->client->path = $this->args['HTTPSURI']; |
||
| 292 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
| 293 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
| 294 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
| 295 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER); |
||
| 296 | |||
| 297 | $this->$method(); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @dataProvider getSingleHttpTestMethods |
||
| 302 | * @param string $method |
||
| 303 | */ |
||
| 304 | public function testHttpsProxy($method) |
||
| 305 | { |
||
| 306 | if (!function_exists('curl_init')) |
||
| 307 | { |
||
| 308 | $this->markTestSkipped('CURL missing: cannot test https w. proxy'); |
||
| 309 | return; |
||
| 310 | } |
||
| 311 | else if ($this->args['PROXYSERVER'] == '') |
||
| 312 | { |
||
| 313 | $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https'); |
||
| 314 | return; |
||
| 315 | } |
||
| 316 | else if ($this->args['HTTPSSERVER'] == '') |
||
| 317 | { |
||
| 318 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy'); |
||
| 319 | return; |
||
| 320 | } |
||
| 321 | |||
| 322 | $this->client->server = $this->args['HTTPSSERVER']; |
||
| 323 | $this->method = 'https'; |
||
| 324 | $this->client->method = 'https'; |
||
| 325 | $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']); |
||
| 326 | $this->client->path = $this->args['HTTPSURI']; |
||
| 327 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
| 328 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
| 329 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
| 330 | |||
| 331 | $this->$method(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @dataProvider getSingleHttpTestMethods |
||
| 336 | * @param string $method |
||
| 337 | */ |
||
| 338 | public function testHttp2($method) |
||
| 339 | { |
||
| 340 | if (!function_exists('curl_init')) |
||
| 341 | { |
||
| 342 | $this->markTestSkipped('CURL missing: cannot test http/2'); |
||
| 343 | return; |
||
| 344 | } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE')) |
||
| 345 | { |
||
| 346 | $this->markTestSkipped('CURL http/2 support missing: cannot test http/2'); |
||
| 347 | return; |
||
| 348 | } |
||
| 349 | |||
| 350 | $this->method = 'h2c'; // not an error the double assignment! |
||
| 351 | $this->client->method = 'h2c'; |
||
| 352 | //$this->client->keepalive = false; // q: is this a good idea? |
||
| 353 | |||
| 354 | $this->expectHttp2 = true; |
||
| 355 | $this->$method(); |
||
| 356 | $this->expectHttp2 = false; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @dataProvider getSingleHttpTestMethods |
||
| 361 | * @param string $method |
||
| 362 | */ |
||
| 363 | public function testHttp2tls($method) |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @dataProvider getSingleHttpTestMethods |
||
| 394 | * @param string $method |
||
| 395 | */ |
||
| 396 | public function testUTF8Responses($method) |
||
| 397 | { |
||
| 398 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8')); |
||
| 399 | |||
| 400 | $this->$method(); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @dataProvider getSingleHttpTestMethods |
||
| 405 | * @param string $method |
||
| 406 | */ |
||
| 407 | public function testUTF8Requests($method) |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @dataProvider getSingleHttpTestMethods |
||
| 416 | * @param string $method |
||
| 417 | */ |
||
| 418 | public function testISOResponses($method) |
||
| 419 | { |
||
| 420 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1')); |
||
| 421 | |||
| 422 | $this->$method(); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @dataProvider getSingleHttpTestMethods |
||
| 427 | * @param string $method |
||
| 428 | */ |
||
| 429 | public function testISORequests($method) |
||
| 430 | { |
||
| 431 | $this->client->request_charset_encoding = 'ISO-8859-1'; |
||
| 432 | |||
| 433 | $this->$method(); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @dataProvider getSingleHttpTestMethods |
||
| 438 | * @param string $method |
||
| 439 | */ |
||
| 440 | public function testBasicAuth($method) |
||
| 441 | { |
||
| 442 | $this->client->setCredentials('test', 'test'); |
||
| 443 | $this->addQueryParams(array('FORCE_AUTH' => 'Basic')); |
||
| 444 | |||
| 445 | $this->$method(); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @dataProvider getSingleHttpTestMethods |
||
| 450 | * @param string $method |
||
| 451 | */ |
||
| 452 | public function testDigestAuth($method) |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param \PhpXmlRpc\Response $r |
||
| 470 | * @return void |
||
| 471 | */ |
||
| 472 | protected function validateResponse($r) |
||
| 478 | |||
| 479 | } |
||
| 480 | } |
||
| 481 | } |
||
| 482 |