Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class TestCase extends BaseTestCase |
||
| 19 | { |
||
| 20 | const X_HTTP_METHOD_OVERRIDE_MAP = [ |
||
| 21 | 'PUBLISH' => 'POST', |
||
| 22 | 'MOVE' => 'POST', |
||
| 23 | 'PATCH' => 'PATCH', |
||
| 24 | 'COPY' => 'POST', |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Buzz\Client\BuzzClientInterface |
||
| 29 | */ |
||
| 30 | private $httpClient; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $httpHost; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | * Basic auth login:password |
||
| 40 | */ |
||
| 41 | private $httpAuth; |
||
| 42 | |||
| 43 | protected static $testSuffix; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $headers = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The username to use for login. |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $loginUsername; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The password to use for login. |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $loginPassword; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * If true, a login request is automatically done during setUp(). |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $autoLogin = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * List of REST contentId (/content/objects/12345) created by tests. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private static $createdContent = array(); |
||
| 74 | |||
| 75 | protected function setUp() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param \Psr\Http\Message\RequestInterface $request |
||
| 98 | * |
||
| 99 | * @return \Psr\Http\Message\ResponseInterface |
||
| 100 | * |
||
| 101 | * @throws \Psr\Http\Client\ClientException |
||
| 102 | */ |
||
| 103 | public function sendHttpRequest(RequestInterface $request): ResponseInterface |
||
| 107 | |||
| 108 | protected function getHttpHost() |
||
| 112 | |||
| 113 | protected function getLoginUsername() |
||
| 117 | |||
| 118 | protected function getLoginPassword() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get base URI for \Buzz\Browser based requests. |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function getBaseURI() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $method |
||
| 135 | * @param string $uri |
||
| 136 | * @param string $contentType |
||
| 137 | * @param string $acceptType |
||
| 138 | * @param string $body |
||
| 139 | * |
||
| 140 | * @param array $extraHeaders [key => value] array of extra headers |
||
| 141 | * |
||
| 142 | * @return \Psr\Http\Message\RequestInterface |
||
| 143 | */ |
||
| 144 | public function createHttpRequest( |
||
| 172 | |||
| 173 | protected function assertHttpResponseCodeEquals(ResponseInterface $response, $expected) |
||
| 194 | |||
| 195 | private function getHttpResponseCodeErrorMessage($errorMessage) |
||
| 213 | |||
| 214 | protected function assertHttpResponseHasHeader(ResponseInterface $response, $header, $expectedValue = null) |
||
| 222 | |||
| 223 | protected function generateMediaTypeString($typeString) |
||
| 227 | |||
| 228 | protected function getMediaFromTypeString($typeString) |
||
| 239 | |||
| 240 | protected function addCreatedElement($href) |
||
| 249 | |||
| 250 | public static function tearDownAfterClass() |
||
| 254 | |||
| 255 | private static function clearCreatedElement(array $contentArray) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $parentLocationId The REST id of the parent location |
||
| 264 | * |
||
| 265 | * @return array created Content, as an array |
||
| 266 | */ |
||
| 267 | protected function createFolder($string, $parentLocationId) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param $xml |
||
| 303 | * |
||
| 304 | * @return array Content key of the Content struct array |
||
| 305 | */ |
||
| 306 | protected function createContent($xml) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $contentHref |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | View Code Duplication | protected function getContentLocations($contentHref) |
|
| 351 | |||
| 352 | protected function addTestSuffix($string) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Sends a login request to the REST server. |
||
| 363 | * |
||
| 364 | * @return \stdClass an object with the name, identifier, csrftoken properties. |
||
| 365 | */ |
||
| 366 | protected function login() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $login |
||
| 377 | * @param string $password |
||
| 378 | * @param array $extraHeaders extra [key => value] headers to be passed with the authentication request. |
||
| 379 | * |
||
| 380 | * @return \Psr\Http\Message\RequestInterface |
||
| 381 | */ |
||
| 382 | protected function createAuthenticationHttpRequest(string $login, string $password, array $extraHeaders = []) |
||
| 393 | } |
||
| 394 |