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 |
||
| 7 | use GuzzleHttp\Exception\RequestException; |
||
| 8 | |||
| 9 | class WoWTest extends TestCase |
||
| 10 | { |
||
| 11 | protected $wow; |
||
| 12 | protected $realm = 'Arathor'; |
||
| 13 | protected $guild = 'Rise Legacy'; |
||
| 14 | protected $character = 'Hellodora'; |
||
| 15 | protected $achievementId = 2144; |
||
| 16 | protected $itemId = 18803; |
||
| 17 | protected $itemSet = 1060; |
||
| 18 | protected $abilityId = 640; |
||
| 19 | protected $speciesId = 258; |
||
| 20 | protected $petLevel = 25; |
||
| 21 | protected $petBreedId = 5; |
||
| 22 | protected $petQualityId = 4; |
||
| 23 | protected $pvpBracket = '2v2'; |
||
| 24 | protected $questId = 13146; |
||
| 25 | protected $recipeId = 33994; |
||
| 26 | protected $spellId = 133; |
||
| 27 | |||
| 28 | public function setUp() |
||
| 29 | { |
||
| 30 | parent::setUp(); |
||
| 31 | |||
| 32 | $this->wow = app(\Xklusive\BattlenetApi\Services\WowService::class); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** @test */ |
||
| 36 | public function api_can_fetch_wow_achievements() |
||
| 37 | { |
||
| 38 | $response = $this->wow->getAchievement($this->achievementId); |
||
| 39 | |||
| 40 | $this->assertInstanceOf(Collection::class, $response); |
||
| 41 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 42 | $this->assertEquals($this->achievementId, $response->get('id')); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** @test */ |
||
| 46 | public function api_can_fetch_auction_data() |
||
| 47 | { |
||
| 48 | $response = $this->wow->getAuctionDataStatus($this->realm); |
||
| 49 | |||
| 50 | $this->assertInstanceOf(Collection::class, $response); |
||
| 51 | $this->assertArrayHasKey('files', $response->toArray()); |
||
| 52 | $this->assertObjectHasAttribute('url', $response->get('files')[0]); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** @test */ |
||
| 56 | public function api_can_fetch_supported_bosses() |
||
| 57 | { |
||
| 58 | $response = $this->wow->getBossMasterList(); |
||
| 59 | |||
| 60 | $this->assertInstanceOf(Collection::class, $response); |
||
| 61 | $this->assertArrayHasKey('bosses', $response->toArray()); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** @test */ |
||
| 65 | public function api_can_fetch_a_boss() |
||
| 66 | { |
||
| 67 | $bosses = $this->wow->getBossMasterList(); |
||
| 68 | $bossId = collect($bosses->get('bosses'))->first()->id; |
||
| 69 | $response = $this->wow->getBoss($bossId); |
||
| 70 | |||
| 71 | $this->assertInstanceOf(Collection::class, $response); |
||
| 72 | $this->assertArrayHasKey('name', $response->toArray()); |
||
| 73 | $this->assertArrayHasKey('urlSlug', $response->toArray()); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** @test */ |
||
| 77 | public function api_can_fetch_realm_leader_board() |
||
| 78 | { |
||
| 79 | $response = $this->wow->getRealmLeaderboard($this->realm); |
||
| 80 | |||
| 81 | $this->assertInstanceOf(Collection::class, $response); |
||
| 82 | $this->assertArrayHasKey('challenge', $response->toArray()); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** @test */ |
||
| 86 | public function api_can_fetch_region_leader_board() |
||
| 87 | { |
||
| 88 | $response = $this->wow->getRegionLeaderboard(); |
||
| 89 | |||
| 90 | $this->assertInstanceOf(Collection::class, $response); |
||
| 91 | $this->assertArrayHasKey('challenge', $response->toArray()); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** @test */ |
||
| 95 | public function api_can_fetch_a_character() |
||
| 96 | { |
||
| 97 | $response = $this->wow->getCharacter($this->realm, $this->character); |
||
| 98 | |||
| 99 | $this->assertInstanceOf(Collection::class, $response); |
||
| 100 | $this->assertArrayHasKey('name', $response->toArray()); |
||
| 101 | $this->assertArrayHasKey('class', $response->toArray()); |
||
| 102 | $this->assertArrayHasKey('race', $response->toArray()); |
||
| 103 | $this->assertArrayHasKey('gender', $response->toArray()); |
||
| 104 | $this->assertArrayHasKey('level', $response->toArray()); |
||
| 105 | $this->assertEquals($this->character, $response->get('name')); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** @test */ |
||
| 109 | public function api_can_fetch_a_guild() |
||
| 110 | { |
||
| 111 | $response = $this->wow->getGuild($this->realm, $this->guild); |
||
| 112 | |||
| 113 | $this->assertInstanceOf(Collection::class, $response); |
||
| 114 | $this->assertArrayHasKey('name', $response->toArray()); |
||
| 115 | $this->assertArrayHasKey('level', $response->toArray()); |
||
| 116 | $this->assertArrayHasKey('side', $response->toArray()); |
||
| 117 | $this->assertArrayHasKey('emblem', $response->toArray()); |
||
| 118 | $this->assertEquals($this->guild, $response->get('name')); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** @test */ |
||
| 122 | public function api_can_fetch_guild_members_with_default_fields() |
||
| 123 | { |
||
| 124 | $response = $this->wow->getGuildMembers($this->realm, $this->guild); |
||
| 125 | |||
| 126 | $this->assertInstanceOf(Collection::class, $response); |
||
| 127 | $this->assertArrayHasKey('name', $response->toArray()); |
||
| 128 | $this->assertArrayHasKey('realm', $response->toArray()); |
||
| 129 | $this->assertArrayHasKey('members', $response->toArray()); |
||
| 130 | $this->assertObjectHasAttribute('character', $response->get('members')[0]); |
||
| 131 | $this->assertEquals($this->guild, $response->get('name')); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** @test */ |
||
| 135 | public function api_can_fetch_guild_members_with_different_fields() |
||
| 136 | { |
||
| 137 | $response = $this->wow->getGuildMembers($this->realm, $this->guild, ['query' => ['fields' => 'characters']]); |
||
| 138 | |||
| 139 | $this->assertInstanceOf(Collection::class, $response); |
||
| 140 | $this->assertArrayHasKey('name', $response->toArray()); |
||
| 141 | $this->assertArrayHasKey('realm', $response->toArray()); |
||
| 142 | $this->assertArrayHasKey('members', $response->toArray()); |
||
| 143 | $this->assertObjectHasAttribute('character', $response->get('members')[0]); |
||
| 144 | $this->assertEquals($this->guild, $response->get('name')); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** @test */ |
||
| 148 | public function api_can_fetch_an_item() |
||
| 149 | { |
||
| 150 | $response = $this->wow->getItem($this->itemId); |
||
| 151 | |||
| 152 | $this->assertInstanceOf(Collection::class, $response); |
||
| 153 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 154 | $this->assertEquals($this->itemId, $response->get('id')); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** @test */ |
||
| 158 | public function api_can_fetch_an_item_set() |
||
| 159 | { |
||
| 160 | $response = $this->wow->getItemSet($this->itemSet); |
||
| 161 | |||
| 162 | $this->assertInstanceOf(Collection::class, $response); |
||
| 163 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 164 | $this->assertEquals($this->itemSet, $response->get('id')); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** @test */ |
||
| 168 | public function api_can_fetch_mount_master_list() |
||
| 169 | { |
||
| 170 | // Currently not producing a proper json a result # see https://us.battle.net/forums/en/bnet/topic/20759527665#1 |
||
| 171 | $this->assertEquals(1, 1); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** @test */ |
||
| 175 | public function api_can_fetch_pet_master_list() |
||
| 176 | { |
||
| 177 | $response = $this->wow->getPetList(); |
||
| 178 | |||
| 179 | $this->assertInstanceOf(Collection::class, $response); |
||
| 180 | $this->assertArrayHasKey('pets', $response->toArray()); |
||
| 181 | $this->assertObjectHasAttribute('name', $response->get('pets')[0]); |
||
| 182 | $this->assertObjectHasAttribute('family', $response->get('pets')[0]); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** @test */ |
||
| 186 | public function api_can_fetch_pet_abilities() |
||
| 187 | { |
||
| 188 | $response = $this->wow->getPetAbility($this->abilityId); |
||
| 189 | |||
| 190 | $this->assertInstanceOf(Collection::class, $response); |
||
| 191 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 192 | $this->assertEquals($this->abilityId, $response->get('id')); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** @test */ |
||
| 196 | public function api_can_fetch_pet_species() |
||
| 197 | { |
||
| 198 | $response = $this->wow->getPetSpecies($this->speciesId); |
||
| 199 | |||
| 200 | $this->assertInstanceOf(Collection::class, $response); |
||
| 201 | $this->assertArrayHasKey('speciesId', $response->toArray()); |
||
| 202 | $this->assertEquals($this->speciesId, $response->get('speciesId')); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** @test */ |
||
| 206 | public function api_can_fetch_pet_stats() |
||
| 207 | { |
||
| 208 | $response = $this->wow->getPetStats($this->speciesId); |
||
| 209 | |||
| 210 | $this->assertInstanceOf(Collection::class, $response); |
||
| 211 | $this->assertArrayHasKey('speciesId', $response->toArray()); |
||
| 212 | $this->assertArrayHasKey('level', $response->toArray()); |
||
| 213 | $this->assertArrayHasKey('breedId', $response->toArray()); |
||
| 214 | $this->assertArrayHasKey('petQualityId', $response->toArray()); |
||
| 215 | $this->assertEquals($this->speciesId, $response->get('speciesId')); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** @test */ |
||
| 219 | public function api_can_fetch_pet_stats_on_specific_level() |
||
| 220 | { |
||
| 221 | $response = $this->wow->getPetStats($this->speciesId, ['level' => $this->petLevel, 'breedId' => $this->petBreedId, 'qualityId' =>$this->petQualityId]); |
||
| 222 | |||
| 223 | $this->assertInstanceOf(Collection::class, $response); |
||
| 224 | $this->assertArrayHasKey('speciesId', $response->toArray()); |
||
| 225 | $this->assertArrayHasKey('level', $response->toArray()); |
||
| 226 | $this->assertArrayHasKey('breedId', $response->toArray()); |
||
| 227 | $this->assertArrayHasKey('petQualityId', $response->toArray()); |
||
| 228 | $this->assertEquals($this->speciesId, $response->get('speciesId')); |
||
| 229 | $this->assertEquals($this->petLevel, $response->get('level')); |
||
| 230 | $this->assertEquals($this->petBreedId, $response->get('breedId')); |
||
| 231 | $this->assertEquals($this->petQualityId, $response->get('petQualityId')); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** @test */ |
||
| 235 | public function api_can_fetch_pvp_leaderboards() |
||
| 236 | { |
||
| 237 | $response = $this->wow->getLeaderboards($this->pvpBracket); |
||
| 238 | |||
| 239 | $this->assertInstanceOf(Collection::class, $response); |
||
| 240 | $this->assertArrayHasKey('rows', $response->toArray()); |
||
| 241 | $this->assertObjectHasAttribute('ranking', $response->get('rows')[0]); |
||
| 242 | $this->assertObjectHasAttribute('rating', $response->get('rows')[0]); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** @test */ |
||
| 246 | public function api_can_fetch_quest_data() |
||
| 247 | { |
||
| 248 | $response = $this->wow->getQuest($this->questId); |
||
| 249 | |||
| 250 | $this->assertInstanceOf(Collection::class, $response); |
||
| 251 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 252 | $this->assertEquals($this->questId, $response->get('id')); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** @test */ |
||
| 256 | public function api_can_fetch_realm_status() |
||
| 257 | { |
||
| 258 | $response = $this->wow->getRealmStatus(); |
||
| 259 | |||
| 260 | $this->assertInstanceOf(Collection::class, $response); |
||
| 261 | $this->assertArrayHasKey('realms', $response->toArray()); |
||
| 262 | $this->assertObjectHasAttribute('type', $response->get('realms')[0]); |
||
| 263 | $this->assertObjectHasAttribute('population', $response->get('realms')[0]); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** @test */ |
||
| 267 | public function api_can_fetch_a_recipe() |
||
| 268 | { |
||
| 269 | $response = $this->wow->getRecipe($this->recipeId); |
||
| 270 | |||
| 271 | $this->assertInstanceOf(Collection::class, $response); |
||
| 272 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 273 | $this->assertEquals($this->recipeId, $response->get('id')); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** @test */ |
||
| 277 | public function api_can_fetch_a_spell() |
||
| 278 | { |
||
| 279 | $response = $this->wow->getSpell($this->spellId); |
||
| 280 | |||
| 281 | $this->assertInstanceOf(Collection::class, $response); |
||
| 282 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 283 | $this->assertEquals($this->spellId, $response->get('id')); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** @test */ |
||
| 287 | public function api_can_fetch_zone_master_list() |
||
| 288 | { |
||
| 289 | $response = $this->wow->getZonesMasterList(); |
||
| 290 | |||
| 291 | $this->assertInstanceOf(Collection::class, $response); |
||
| 292 | $this->assertArrayHasKey('zones', $response->toArray()); |
||
| 293 | $this->assertObjectHasAttribute('id', $response->get('zones')[0]); |
||
| 294 | $this->assertObjectHasAttribute('name', $response->get('zones')[0]); |
||
| 295 | $this->assertObjectHasAttribute('expansionId', $response->get('zones')[0]); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** @test */ |
||
| 299 | public function api_can_fetch_zone_details() |
||
| 300 | { |
||
| 301 | $zoneId = $this->wow->getZonesMasterList()->get('zones')[0]->id; |
||
| 302 | $response = $this->wow->getZone($zoneId); |
||
| 303 | |||
| 304 | $this->assertInstanceOf(Collection::class, $response); |
||
| 305 | $this->assertArrayHasKey('id', $response->toArray()); |
||
| 306 | $this->assertEquals($zoneId, $response->get('id')); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** @test */ |
||
| 310 | public function api_can_fetch_battlegroup_data() |
||
| 311 | { |
||
| 312 | $response = $this->wow->getDataBattlegroups(); |
||
| 313 | |||
| 314 | $this->assertInstanceOf(Collection::class, $response); |
||
| 315 | $this->assertArrayHasKey('battlegroups', $response->toArray()); |
||
| 316 | $this->assertObjectHasAttribute('name', $response->get('battlegroups')[0]); |
||
| 317 | $this->assertObjectHasAttribute('slug', $response->get('battlegroups')[0]); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** @test */ |
||
| 321 | public function api_can_fetch_character_races() |
||
| 322 | { |
||
| 323 | $response = $this->wow->getDataCharacterRaces(); |
||
| 324 | |||
| 325 | $this->assertInstanceOf(Collection::class, $response); |
||
| 326 | $this->assertArrayHasKey('races', $response->toArray()); |
||
| 327 | $this->assertObjectHasAttribute('id', $response->get('races')[0]); |
||
| 328 | $this->assertObjectHasAttribute('name', $response->get('races')[0]); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** @test */ |
||
| 332 | public function api_can_fetch_character_classes() |
||
| 333 | { |
||
| 334 | $response = $this->wow->getDataCharacterClasses(); |
||
| 335 | |||
| 336 | $this->assertInstanceOf(Collection::class, $response); |
||
| 337 | $this->assertArrayHasKey('classes', $response->toArray()); |
||
| 338 | $this->assertObjectHasAttribute('id', $response->get('classes')[0]); |
||
| 339 | $this->assertObjectHasAttribute('name', $response->get('classes')[0]); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** @test */ |
||
| 343 | public function api_can_fetch_character_achievements_data() |
||
| 344 | { |
||
| 345 | $response = $this->wow->getDataCharacterAchievements(); |
||
| 346 | |||
| 347 | $this->assertInstanceOf(Collection::class, $response); |
||
| 348 | $this->assertArrayHasKey('achievements', $response->toArray()); |
||
| 349 | $this->assertObjectHasAttribute('id', $response->get('achievements')[0]); |
||
| 350 | $this->assertObjectHasAttribute('achievements', $response->get('achievements')[0]); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** @test */ |
||
| 354 | public function api_can_fetch_guild_rewards_data() |
||
| 355 | { |
||
| 356 | $response = $this->wow->getDataGuildRewards(); |
||
| 357 | |||
| 358 | $this->assertInstanceOf(Collection::class, $response); |
||
| 359 | $this->assertArrayHasKey('rewards', $response->toArray()); |
||
| 360 | $this->assertObjectHasAttribute('minGuildLevel', $response->get('rewards')[0]); |
||
| 361 | $this->assertObjectHasAttribute('achievement', $response->get('rewards')[0]); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** @test */ |
||
| 365 | public function api_can_fetch_guild_perks() |
||
| 366 | { |
||
| 367 | $response = $this->wow->getDataGuildPerks(); |
||
| 368 | |||
| 369 | $this->assertInstanceOf(Collection::class, $response); |
||
| 370 | $this->assertArrayHasKey('perks', $response->toArray()); |
||
| 371 | $this->assertObjectHasAttribute('guildLevel', $response->get('perks')[0]); |
||
| 372 | $this->assertObjectHasAttribute('spell', $response->get('perks')[0]); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** @test */ |
||
| 376 | public function api_can_fetch_guild_achievements_data() |
||
| 377 | { |
||
| 378 | $response = $this->wow->getDataGuildAchievements(); |
||
| 379 | |||
| 380 | $this->assertInstanceOf(Collection::class, $response); |
||
| 381 | $this->assertArrayHasKey('achievements', $response->toArray()); |
||
| 382 | $this->assertObjectHasAttribute('id', $response->get('achievements')[0]); |
||
| 383 | $this->assertObjectHasAttribute('achievements', $response->get('achievements')[0]); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** @test */ |
||
| 387 | public function api_can_fetch_item_classes_data() |
||
| 388 | { |
||
| 389 | $response = $this->wow->getDataItemClasses(); |
||
| 390 | |||
| 391 | $this->assertInstanceOf(Collection::class, $response); |
||
| 392 | $this->assertArrayHasKey('classes', $response->toArray()); |
||
| 393 | $this->assertObjectHasAttribute('name', $response->get('classes')[0]); |
||
| 394 | $this->assertObjectHasAttribute('class', $response->get('classes')[0]); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** @test */ |
||
| 398 | public function api_can_fetch_talent_data() |
||
| 399 | { |
||
| 400 | $response = $this->wow->getDataTalents(); |
||
| 401 | |||
| 402 | $this->assertInstanceOf(Collection::class, $response); |
||
| 403 | $this->assertObjectHasAttribute('specs', $response->first()); |
||
| 404 | $this->assertObjectHasAttribute('name', $response->first()->specs[0]); |
||
| 441 |