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 |
||
| 22 | class TestCase extends BaseTestCase |
||
| 23 | { |
||
| 24 | const X_HTTP_METHOD_OVERRIDE_MAP = [ |
||
| 25 | 'PUBLISH' => 'POST', |
||
| 26 | 'MOVE' => 'POST', |
||
| 27 | 'PATCH' => 'PATCH', |
||
| 28 | 'COPY' => 'POST', |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \Buzz\Client\BuzzClientInterface |
||
| 33 | */ |
||
| 34 | private $httpClient; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $httpHost; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | * Basic auth login:password |
||
| 44 | */ |
||
| 45 | private $httpAuth; |
||
| 46 | |||
| 47 | protected static $testSuffix; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $headers = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The username to use for login. |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $loginUsername; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The password to use for login. |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $loginPassword; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * If true, a login request is automatically done during setUp(). |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $autoLogin = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * List of REST contentId (/content/objects/12345) created by tests. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private static $createdContent = array(); |
||
| 78 | |||
| 79 | protected function setUp() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Instantiate Browser object. |
||
| 105 | * |
||
| 106 | * @return \Buzz\Client\BuzzClientInterface |
||
| 107 | */ |
||
| 108 | public function createBrowser(): BuzzClientInterface |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param \Psr\Http\Message\RequestInterface $request |
||
| 119 | * |
||
| 120 | * @return \Psr\Http\Message\ResponseInterface |
||
| 121 | * |
||
| 122 | * @throws \Psr\Http\Client\ClientException |
||
| 123 | */ |
||
| 124 | public function sendHttpRequest(RequestInterface $request): ResponseInterface |
||
| 128 | |||
| 129 | protected function getHttpHost() |
||
| 133 | |||
| 134 | protected function getLoginUsername() |
||
| 138 | |||
| 139 | protected function getLoginPassword() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get base URI for \Buzz\Browser based requests. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | protected function getBaseURI() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $method |
||
| 156 | * @param string $uri |
||
| 157 | * @param string $contentType |
||
| 158 | * @param string $acceptType |
||
| 159 | * @param string $body |
||
| 160 | * |
||
| 161 | * @param array $extraHeaders [key => value] array of extra headers |
||
| 162 | * |
||
| 163 | * @return \Psr\Http\Message\RequestInterface |
||
| 164 | */ |
||
| 165 | public function createHttpRequest( |
||
| 193 | |||
| 194 | protected function assertHttpResponseCodeEquals(ResponseInterface $response, $expected) |
||
| 216 | |||
| 217 | private function getHttpResponseCodeErrorMessage($errorMessage) |
||
| 235 | |||
| 236 | protected function assertHttpResponseHasHeader(ResponseInterface $response, $header, $expectedValue = null) |
||
| 244 | |||
| 245 | protected function generateMediaTypeString($typeString) |
||
| 249 | |||
| 250 | protected function getMediaFromTypeString($typeString) |
||
| 261 | |||
| 262 | protected function addCreatedElement($href) |
||
| 271 | |||
| 272 | public static function tearDownAfterClass() |
||
| 276 | |||
| 277 | private static function clearCreatedElement(array $contentArray) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $parentLocationId The REST id of the parent location |
||
| 286 | * |
||
| 287 | * @return array created Content, as an array |
||
| 288 | */ |
||
| 289 | protected function createFolder($string, $parentLocationId) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param $xml |
||
| 325 | * |
||
| 326 | * @return array Content key of the Content struct array |
||
| 327 | */ |
||
| 328 | protected function createContent($xml) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $contentHref |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | View Code Duplication | protected function getContentLocations($contentHref) |
|
| 373 | |||
| 374 | protected function addTestSuffix($string) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Sends a login request to the REST server. |
||
| 385 | * |
||
| 386 | * @return \stdClass an object with the name, identifier, csrftoken properties. |
||
| 387 | */ |
||
| 388 | protected function login() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $login |
||
| 399 | * @param string $password |
||
| 400 | * @param array $extraHeaders extra [key => value] headers to be passed with the authentication request. |
||
| 401 | * |
||
| 402 | * @return \Psr\Http\Message\RequestInterface |
||
| 403 | */ |
||
| 404 | protected function createAuthenticationHttpRequest(string $login, string $password, array $extraHeaders = []) |
||
| 415 | } |
||
| 416 |