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:
Complex classes like TheSportsDbTest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TheSportsDbTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class TheSportsDbTest extends \PHPUnit_Framework_TestCase { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var TheSportsDb |
||
| 21 | */ |
||
| 22 | protected $db; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Sets up the fixture, for example, opens a network connection. |
||
| 26 | * This method is called before a test is executed. |
||
| 27 | */ |
||
| 28 | protected function setUp() { |
||
| 29 | include __DIR__ . '/../../default_bootstrap.php'; |
||
| 30 | $this->db = $db; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Tears down the fixture, for example, closes a network connection. |
||
| 35 | * This method is called after a test is executed. |
||
| 36 | */ |
||
| 37 | protected function tearDown() { |
||
| 38 | $this->db = NULL; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @covers TheSportsDb\TheSportsDb::getSports |
||
| 43 | */ |
||
| 44 | public function testGetSports() { |
||
| 45 | $sports = $this->db->getSports(); |
||
| 46 | $this->assertNotEmpty($sports); |
||
| 47 | foreach ($sports as $sport) { |
||
| 48 | // Should be a sport. |
||
| 49 | $this->assertInstanceOf(SportInterface::class, $sport); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @covers TheSportsDb\TheSportsDb::getSport |
||
| 55 | */ |
||
| 56 | public function testGetSport() { |
||
| 57 | $sport = $this->db->getSport('Soccer'); |
||
| 58 | // Should be a sport. |
||
| 59 | $this->assertInstanceOf(SportInterface::class, $sport); |
||
| 60 | $this->assertEquals('Soccer', $sport->getName()); |
||
| 61 | |||
| 62 | // Try a fake sport. |
||
| 63 | $sport = $this->db->getSport('FakeSport123'); |
||
| 64 | // Should be a sport. |
||
| 65 | $this->assertInstanceOf(SportInterface::class, $sport); |
||
| 66 | $this->assertEquals('FakeSport123', $sport->getName()); |
||
| 67 | |||
| 68 | // Sport doesn't exist, so exception when we try to load its leagues. |
||
| 69 | $this->expectException(\Exception::class); |
||
| 70 | $this->expectExceptionMessage('Could not fully load sport with id FakeSport123.'); |
||
| 71 | $sport->getLeagues(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @covers TheSportsDb\TheSportsDb::getLeagues |
||
| 76 | */ |
||
| 77 | public function testGetLeagues() { |
||
| 78 | $leagues = $this->db->getLeagues(); |
||
| 79 | $this->assertNotEmpty($leagues); |
||
| 80 | foreach ($leagues as $league) { |
||
| 81 | // Should be a league. |
||
| 82 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @covers TheSportsDb\TheSportsDb::getLeague |
||
| 88 | */ |
||
| 89 | public function testGetLeague() { |
||
| 90 | $league = $this->db->getLeague(4328); |
||
| 91 | // Should be a sport. |
||
| 92 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 93 | $this->assertEquals('English Premier League', $league->getName()); |
||
| 94 | |||
| 95 | // Try a fake league. |
||
| 96 | $league = $this->db->getLeague('FakeLeague123'); |
||
| 97 | // Should be a league. |
||
| 98 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 99 | $this->assertEquals('FakeLeague123', $league->getId()); |
||
| 100 | |||
| 101 | // League doesn't exist, so exception when we try to load its name. |
||
| 102 | $this->expectException(\Exception::class); |
||
| 103 | $this->expectExceptionMessage('Could not fully load league with id FakeLeague123.'); |
||
| 104 | $league->getName(); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @covers TheSportsDb\TheSportsDb::getLeaguesByCountry |
||
| 109 | */ |
||
| 110 | public function testGetLeaguesByCountry() { |
||
| 111 | $leagues = $this->db->getLeaguesByCountry('Belgium'); |
||
| 112 | $this->assertNotEmpty($leagues); |
||
| 113 | foreach ($leagues as $league) { |
||
| 114 | // Should be a league. |
||
| 115 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 116 | $this->assertEquals('Belgium', $league->getCountry()); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Try a fake country. |
||
| 120 | $leagues = $this->db->getLeaguesByCountry('FakeCountry123'); |
||
| 121 | $this->assertEmpty($leagues); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @covers TheSportsDb\TheSportsDb::getLeaguesBySport |
||
| 126 | */ |
||
| 127 | public function testGetLeaguesBySport() { |
||
| 128 | $leagues = $this->db->getLeaguesBySport('Soccer'); |
||
| 129 | $this->assertNotEmpty($leagues); |
||
| 130 | foreach ($leagues as $league) { |
||
| 131 | // Should be a league. |
||
| 132 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 133 | $this->assertEquals('Soccer', $league->getSport()->getName()); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Try a fake sport. |
||
| 137 | $leagues = $this->db->getLeaguesByCountry('FakeSport123'); |
||
| 138 | $this->assertEmpty($leagues); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @covers TheSportsDb\TheSportsDb::getLeaguesByCountryAndSport |
||
| 143 | */ |
||
| 144 | public function testGetLeaguesByCountryAndSport() { |
||
| 145 | $leagues = $this->db->getLeaguesByCountryAndSport('Belgium', 'Soccer'); |
||
| 146 | $this->assertNotEmpty($leagues); |
||
| 147 | foreach ($leagues as $league) { |
||
| 148 | // Should be a league. |
||
| 149 | $this->assertInstanceOf(LeagueInterface::class, $league); |
||
| 150 | $this->assertEquals('Belgium', $league->getCountry()); |
||
| 151 | $this->assertEquals('Soccer', $league->getSport()->getName()); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Try a fake country. |
||
| 155 | $leagues = $this->db->getLeaguesByCountryAndSport('FakeCountry123', 'Soccer'); |
||
| 156 | $this->assertEmpty($leagues); |
||
| 157 | |||
| 158 | // Try a fake sport. |
||
| 159 | $leagues = $this->db->getLeaguesByCountryAndSport('Belgium', 'FakeSport123'); |
||
| 160 | $this->assertEmpty($leagues); |
||
| 161 | |||
| 162 | // Try a fake country and sport. |
||
| 163 | $leagues = $this->db->getLeaguesByCountryAndSport('FakeCountry123', 'FakeSport123'); |
||
| 164 | $this->assertEmpty($leagues); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @covers TheSportsDb\TheSportsDb::getTeam |
||
| 169 | */ |
||
| 170 | public function testGetTeam() { |
||
| 171 | $team = $this->db->getTeam(133604); |
||
| 172 | // Should be a sport. |
||
| 173 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 174 | $this->assertEquals('Arsenal', $team->getName()); |
||
| 175 | |||
| 176 | // Try a fake team. |
||
| 177 | $team = $this->db->getTeam('FakeTeam123'); |
||
| 178 | // Should be a team. |
||
| 179 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 180 | $this->assertEquals('FakeTeam123', $team->getId()); |
||
| 181 | |||
| 182 | // Sport doesn't exist, so exception when we try to load its teams. |
||
| 183 | $this->expectException(\Exception::class); |
||
| 184 | $this->expectExceptionMessage('Could not fully load team with id FakeTeam123.'); |
||
| 185 | $team->getName(); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @covers TheSportsDb\TheSportsDb::getTeamsByName |
||
| 190 | */ |
||
| 191 | public function testGetTeamsByName() { |
||
| 192 | $teams = $this->db->getTeamsByName('Arsenal'); |
||
| 193 | $this->assertNotEmpty($teams); |
||
| 194 | |||
| 195 | foreach ($teams as $team) { |
||
| 196 | // Should be a team. |
||
| 197 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 198 | $this->assertContains('Arsenal', $team->getName()); |
||
| 199 | } |
||
| 200 | |||
| 201 | // Try a fake team. |
||
| 202 | $teams = $this->db->getTeamsByName('FakeTeam123'); |
||
| 203 | $this->assertEmpty($teams); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @covers TheSportsDb\TheSportsDb::getTeamsByLeague |
||
| 208 | */ |
||
| 209 | public function testGetTeamsByLeague() { |
||
| 210 | $teams = $this->db->getTeamsByLeague(4328); |
||
| 211 | $this->assertNotEmpty($teams); |
||
| 212 | |||
| 213 | foreach ($teams as $team) { |
||
| 214 | // Should be a team. |
||
| 215 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 216 | $this->assertEquals(4328, $team->getLeague()->getId()); |
||
| 217 | } |
||
| 218 | |||
| 219 | // Try a fake league. |
||
| 220 | $teams = $this->db->getTeamsByLeague('FakeLeague123'); |
||
| 221 | $this->assertEmpty($teams); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @covers TheSportsDb\TheSportsDb::getTeamsByLeagueName |
||
| 226 | */ |
||
| 227 | public function testGetTeamsByLeagueName() { |
||
| 228 | $teams = $this->db->getTeamsByLeagueName('English Premier League'); |
||
| 229 | $this->assertNotEmpty($teams); |
||
| 230 | |||
| 231 | foreach ($teams as $team) { |
||
| 232 | // Should be a team. |
||
| 233 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 234 | $this->assertEquals('English Premier League', $team->getLeague()->getName()); |
||
| 235 | } |
||
| 236 | |||
| 237 | // Try a fake league. |
||
| 238 | $teams = $this->db->getTeamsByLeagueName('FakeLeague123'); |
||
| 239 | $this->assertEmpty($teams); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @covers TheSportsDb\TheSportsDb::getTeamsBySportAndCountry |
||
| 244 | */ |
||
| 245 | public function testGetTeamsBySportAndCountry() { |
||
| 246 | $teams = $this->db->getTeamsBySportAndCountry('Soccer', 'Belgium'); |
||
| 247 | $this->assertNotEmpty($teams); |
||
| 248 | foreach ($teams as $team) { |
||
| 249 | // Should be a team. |
||
| 250 | $this->assertInstanceOf(TeamInterface::class, $team); |
||
| 251 | $this->assertEquals('Belgium', $team->getCountry()); |
||
| 252 | $this->assertEquals('Soccer', $team->getSport()->getName()); |
||
| 253 | } |
||
| 254 | |||
| 255 | // Try a fake country. |
||
| 256 | $teams = $this->db->getTeamsBySportAndCountry('Soccer', 'FakeCountry123'); |
||
| 257 | $this->assertEmpty($teams); |
||
| 258 | |||
| 259 | // Try a fake sport. |
||
| 260 | $teams = $this->db->getTeamsBySportAndCountry('FakeSport123', 'Belgium'); |
||
| 261 | $this->assertEmpty($teams); |
||
| 262 | |||
| 263 | // Try a fake country and sport. |
||
| 264 | $teams = $this->db->getTeamsBySportAndCountry('FakeSport123', 'FakeCountry123'); |
||
| 265 | $this->assertEmpty($teams); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @covers TheSportsDb\TheSportsDb::getPlayer |
||
| 270 | */ |
||
| 271 | public function testGetPlayer() { |
||
| 272 | $player = $this->db->getPlayer(34145937); |
||
| 273 | // Should be a sport. |
||
| 274 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 275 | $this->assertEquals('Mario Balotelli', $player->getName()); |
||
| 276 | |||
| 277 | // Try a fake player. |
||
| 278 | $player = $this->db->getPlayer('FakePlayer123'); |
||
| 279 | // Should be a player. |
||
| 280 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 281 | $this->assertEquals('FakePlayer123', $player->getId()); |
||
| 282 | |||
| 283 | // Player doesn't exist, so exception when we try to load its sport. |
||
| 284 | $this->expectException(\Exception::class); |
||
| 285 | $this->expectExceptionMessage('Could not fully load player with id FakePlayer123.'); |
||
| 286 | $player->getSport(); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @covers TheSportsDb\TheSportsDb::getPlayersByTeam |
||
| 291 | */ |
||
| 292 | public function testGetPlayersByTeam() { |
||
| 293 | $players = $this->db->getPlayersByTeam(133604); |
||
| 294 | $this->assertNotEmpty($players); |
||
| 295 | |||
| 296 | foreach ($players as $player) { |
||
| 297 | // Should be a player. |
||
| 298 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 299 | $this->assertEquals(133604, $player->getTeam()->getId()); |
||
| 300 | } |
||
| 301 | |||
| 302 | // Try a fake team. |
||
| 303 | $players = $this->db->getPlayersByTeam('FakeTeam123'); |
||
| 304 | $this->assertEmpty($players); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @covers TheSportsDb\TheSportsDb::getPlayersByTeamName |
||
| 309 | */ |
||
| 310 | public function testGetPlayersByTeamName() { |
||
| 311 | $players = $this->db->getPlayersByTeamName('Arsenal'); |
||
| 312 | $this->assertNotEmpty($players); |
||
| 313 | |||
| 314 | foreach ($players as $player) { |
||
| 315 | // Should be a player. |
||
| 316 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 317 | $this->assertContains('Arsenal', $player->getTeam()->getName(), '', FALSE); |
||
| 318 | } |
||
| 319 | |||
| 320 | // Try a fake team. |
||
| 321 | $players = $this->db->getPlayersByTeamName('FakeTeam123'); |
||
| 322 | $this->assertEmpty($players); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @covers TheSportsDb\TheSportsDb::getPlayersByName |
||
| 327 | */ |
||
| 328 | public function testGetPlayersByName() { |
||
| 329 | $players = $this->db->getPlayersByName('mario'); |
||
| 330 | $this->assertNotEmpty($players); |
||
| 331 | |||
| 332 | foreach ($players as $player) { |
||
| 333 | // Should be a player. |
||
| 334 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 335 | $this->assertContains('mario', $player->getName(), '', TRUE); |
||
| 336 | } |
||
| 337 | |||
| 338 | // Try a fake name. |
||
| 339 | $players = $this->db->getPlayersByName('FakePlayer123'); |
||
| 340 | $this->assertEmpty($players); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @covers TheSportsDb\TheSportsDb::getPlayersByTeamNameAndName |
||
| 345 | */ |
||
| 346 | public function testGetPlayersByTeamNameAndName() { |
||
| 347 | $players = $this->db->getPlayersByTeamNameAndName('Arsenal', 'Ivan'); |
||
| 348 | $this->assertNotEmpty($players); |
||
| 349 | |||
| 350 | foreach ($players as $player) { |
||
| 351 | // Should be a player. |
||
| 352 | $this->assertInstanceOf(PlayerInterface::class, $player); |
||
| 353 | $this->assertContains('Ivan', $player->getName(), '', TRUE); |
||
| 354 | $this->assertContains('Arsenal', $player->getTeam()->getName(), '', TRUE); |
||
| 355 | } |
||
| 356 | |||
| 357 | // Try a fake team. |
||
| 358 | $players = $this->db->getPlayersByTeamNameAndName('FakeTeam123', 'Ivan'); |
||
| 359 | $this->assertEmpty($players); |
||
| 360 | |||
| 361 | // Try a fake name. |
||
| 362 | $players = $this->db->getPlayersByTeamNameAndName('Arsenal', 'FakePlayer123'); |
||
| 363 | $this->assertEmpty($players); |
||
| 364 | |||
| 365 | // Try a fake team and name. |
||
| 366 | $players = $this->db->getPlayersByTeamNameAndName('FakeTeam123', 'FakePlayer123'); |
||
| 367 | $this->assertEmpty($players); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @covers TheSportsDb\TheSportsDb::getEvent |
||
| 372 | */ |
||
| 373 | public function testGetEvent() { |
||
| 374 | $event = $this->db->getEvent(441613); |
||
| 375 | // Should be an event. |
||
| 376 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 377 | $this->assertEquals('Liverpool vs Swansea', $event->getName()); |
||
| 378 | |||
| 379 | // Try a fake event. |
||
| 380 | $event = $this->db->getEvent('FakeEvent123'); |
||
| 381 | // Should be an event. |
||
| 382 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 383 | $this->assertEquals('FakeEvent123', $event->getId()); |
||
| 384 | |||
| 385 | // Event doesn't exist, so exception when we try to load its sport. |
||
| 386 | $this->expectException(\Exception::class); |
||
| 387 | $this->expectExceptionMessage('Could not fully load event with id FakeEvent123.'); |
||
| 388 | $event->getLeague(); |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @covers TheSportsDb\TheSportsDb::getEventsByName |
||
| 393 | */ |
||
| 394 | public function testGetEventsByName() { |
||
| 395 | $events = $this->db->getEventsByName('Arsenal vs'); |
||
| 396 | $this->assertNotEmpty($events); |
||
| 397 | |||
| 398 | foreach ($events as $event) { |
||
| 399 | // Should be an event. |
||
| 400 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 401 | $this->assertContains('Arsenal vs', $event->getName(), '', TRUE); |
||
| 402 | } |
||
| 403 | |||
| 404 | // Try a fake name. |
||
| 405 | $events = $this->db->getEventsByName('FakeTeam1 vs FakeTeam2'); |
||
| 406 | $this->assertEmpty($events); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @covers TheSportsDb\TheSportsDb::getEventsByFileName |
||
| 411 | */ |
||
| 412 | public function testGetEventsByFileName() { |
||
| 413 | $events = $this->db->getEventsByFileName('English Premier League'); |
||
| 414 | $this->assertNotEmpty($events); |
||
| 415 | |||
| 416 | foreach ($events as $event) { |
||
| 417 | // Should be an event. |
||
| 418 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 419 | $this->assertContains('English Premier League', $event->getFilename(), '', TRUE); |
||
| 420 | } |
||
| 421 | |||
| 422 | // Try a fake name. |
||
| 423 | $events = $this->db->getEventsByFileName('FakeTeam1 vs FakeTeam2'); |
||
| 424 | $this->assertEmpty($events); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @covers TheSportsDb\TheSportsDb::getEventsByNameAndSeason |
||
| 429 | */ |
||
| 430 | public function testGetEventsByNameAndSeason() { |
||
| 431 | $events = $this->db->getEventsByNameAndSeason('Arsenal vs', '1415'); |
||
| 432 | $this->assertNotEmpty($events); |
||
| 433 | |||
| 434 | foreach ($events as $event) { |
||
| 435 | // Should be an event. |
||
| 436 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 437 | $this->assertContains('Arsenal vs', $event->getName(), '', TRUE); |
||
| 438 | $this->assertEquals('1415', $event->getSeason()->getName()); |
||
| 439 | } |
||
| 440 | |||
| 441 | // Try a fake name. |
||
| 442 | $events = $this->db->getEventsByNameAndSeason('FakeTeam1 vs FakeTeam2', '1415'); |
||
| 443 | $this->assertEmpty($events); |
||
| 444 | |||
| 445 | // Try a fake season. |
||
| 446 | $events = $this->db->getEventsByNameAndSeason('Arsenal vs', 'FakeSeason123'); |
||
| 447 | $this->assertEmpty($events); |
||
| 448 | |||
| 449 | // Try a fake name and season. |
||
| 450 | $events = $this->db->getEventsByNameAndSeason('FakeTeam1 vs FakeTeam2', 'FakeSeason123'); |
||
| 451 | $this->assertEmpty($events); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @covers TheSportsDb\TheSportsDb::getNextFiveEventsByTeam |
||
| 456 | */ |
||
| 457 | public function testGetNextFiveEventsByTeam() { |
||
| 458 | $events = $this->db->getNextFiveEventsByTeam(133604); |
||
| 459 | $this->assertNotEmpty($events); |
||
| 460 | $this->assertEquals(5, count($events)); |
||
| 461 | |||
| 462 | foreach ($events as $event) { |
||
| 463 | // Should be an event. |
||
| 464 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 465 | $this->assertContains(133604, array($event->getHomeTeam()->getId(), $event->getAwayTeam()->getId())); |
||
| 466 | } |
||
| 467 | |||
| 468 | // Try a fake name. |
||
| 469 | // Bug in the api, see http://www.thesportsdb.com/forum/viewtopic.php?f=6&t=196&sid=99a29ba57ec8ee0a1b12d5f429348121 |
||
| 470 | //$events = $this->db->getNextFiveEventsByTeam('FakeTeam123'); |
||
| 471 | //$this->assertEmpty($events); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @covers TheSportsDb\TheSportsDb::getNextFifteenEventsByLeague |
||
| 476 | */ |
||
| 477 | public function testGetNextFifteenEventsByLeague() { |
||
| 478 | $events = $this->db->getNextFifteenEventsByLeague(4328); |
||
| 479 | $this->assertNotEmpty($events); |
||
| 480 | $this->assertEquals(15, count($events)); |
||
| 481 | |||
| 482 | foreach ($events as $event) { |
||
| 483 | // Should be an event. |
||
| 484 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 485 | $this->assertEquals(4328, $event->getLeague()->getId()); |
||
| 486 | } |
||
| 487 | |||
| 488 | // Try a fake name. |
||
| 489 | $events = $this->db->getNextFifteenEventsByLeague('FakeLeague123'); |
||
| 490 | $this->assertEmpty($events); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @covers TheSportsDb\TheSportsDb::getNextFifteenEventsByLeagueAndRound |
||
| 495 | */ |
||
| 496 | public function testGetNextFifteenEventsByLeagueAndRound() { |
||
| 497 | $events = $this->db->getNextFifteenEventsByLeagueAndRound(4328, 38); |
||
| 498 | $this->assertNotEmpty($events); |
||
| 499 | $this->assertLessThanOrEqual(15, count($events)); |
||
| 500 | |||
| 501 | foreach ($events as $event) { |
||
| 502 | // Should be an event. |
||
| 503 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 504 | $this->assertEquals(4328, $event->getLeague()->getId()); |
||
| 505 | $this->assertEquals(38, $event->getRound()); |
||
| 506 | } |
||
| 507 | |||
| 508 | // Try a fake name. |
||
| 509 | $events = $this->db->getNextFifteenEventsByLeague('FakeLeague123'); |
||
| 510 | $this->assertEmpty($events); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @covers TheSportsDb\TheSportsDb::getLastFiveEventsByTeam |
||
| 515 | */ |
||
| 516 | public function testGetLastFiveEventsByTeam() { |
||
| 517 | $events = $this->db->getLastFiveEventsByTeam(133604); |
||
| 518 | $this->assertNotEmpty($events); |
||
| 519 | $this->assertLessThanOrEqual(5, count($events)); |
||
| 520 | |||
| 521 | foreach ($events as $event) { |
||
| 522 | // Should be an event. |
||
| 523 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 524 | $this->assertContains(133604, array($event->getHomeTeam()->getId(), $event->getAwayTeam()->getId())); |
||
| 525 | } |
||
| 526 | |||
| 527 | // Try a fake name. |
||
| 528 | // Bug in the api, see http://www.thesportsdb.com/forum/viewtopic.php?f=6&t=196&sid=99a29ba57ec8ee0a1b12d5f429348121 |
||
| 529 | //$events = $this->db->getLastFiveEventsByTeam('FakeTeam123'); |
||
| 530 | //$this->assertEmpty($events); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @covers TheSportsDb\TheSportsDb::getLastFifteenEventsByLeague |
||
| 535 | */ |
||
| 536 | public function testGetLastFifteenEventsByLeague() { |
||
| 537 | $events = $this->db->getLastFifteenEventsByLeague(4328); |
||
| 538 | $this->assertNotEmpty($events); |
||
| 539 | $this->assertLessThanOrEqual(15, count($events)); |
||
| 540 | |||
| 541 | foreach ($events as $event) { |
||
| 542 | // Should be an event. |
||
| 543 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 544 | $this->assertEquals(4328, $event->getLeague()->getId()); |
||
| 545 | } |
||
| 546 | |||
| 547 | // Try a fake name. |
||
| 548 | $events = $this->db->getLastFifteenEventsByLeague('FakeLeague123'); |
||
| 549 | $this->assertEmpty($events); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @covers TheSportsDb\TheSportsDb::getEventsByDay |
||
| 554 | */ |
||
| 555 | public function testGetEventsByDay() { |
||
| 556 | //http://www.thesportsdb.com/api/v1/json/1/eventsday.php?d=2014-10-10 |
||
| 557 | $day = new \DateTime(); |
||
| 558 | $day->setDate(2014, 1, 13); |
||
| 559 | $events = $this->db->getEventsByDay($day); |
||
| 560 | $this->assertNotEmpty($events); |
||
| 561 | |||
| 562 | foreach ($events as $event) { |
||
| 563 | // Should be an event. |
||
| 564 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 565 | $this->assertEquals($day->format('Y-m-d'), $event->getDate()); |
||
| 566 | } |
||
| 567 | |||
| 568 | $events = $this->db->getEventsByDay($day, 'Soccer'); |
||
| 569 | $this->assertNotEmpty($events); |
||
| 570 | |||
| 571 | foreach ($events as $event) { |
||
| 572 | // Should be an event. |
||
| 573 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 574 | $this->assertEquals($day->format('Y-m-d'), $event->getDate()); |
||
| 575 | $this->assertEquals('Soccer', $event->getLeague()->getSport()->getName()); |
||
| 576 | } |
||
| 577 | |||
| 578 | $events = $this->db->getEventsByDay($day, NULL, 'English Premier League'); |
||
| 579 | $this->assertNotEmpty($events); |
||
| 580 | |||
| 581 | foreach ($events as $event) { |
||
| 582 | // Should be an event. |
||
| 583 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 584 | $this->assertEquals($day->format('Y-m-d'), $event->getDate()); |
||
| 585 | $this->assertEquals('English Premier League', $event->getLeague()->getName()); |
||
| 586 | } |
||
| 587 | |||
| 588 | $events = $this->db->getEventsByDay($day, 'Soccer', 'English Premier League'); |
||
| 589 | $this->assertNotEmpty($events); |
||
| 590 | |||
| 591 | foreach ($events as $event) { |
||
| 592 | // Should be an event. |
||
| 593 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 594 | $this->assertEquals($day->format('Y-m-d'), $event->getDate()); |
||
| 595 | $this->assertEquals('English Premier League', $event->getLeague()->getName()); |
||
| 596 | $this->assertEquals('Soccer', $event->getLeague()->getSport()->getName()); |
||
| 597 | } |
||
| 598 | |||
| 599 | // Fake sport. |
||
| 600 | // Bug in the api, see http://www.thesportsdb.com/forum/viewtopic.php?f=6&t=196&sid=99a29ba57ec8ee0a1b12d5f429348121 |
||
| 601 | //$events = $this->db->getEventsByDay($day, 'FakeSport123', 'English Premier League'); |
||
| 602 | //$this->assertEmpty($events); |
||
| 603 | |||
| 604 | // Fake league. |
||
| 605 | $events = $this->db->getEventsByDay($day, 'Soccer', 'FakeLeague123'); |
||
| 606 | $this->assertEmpty($events); |
||
| 607 | |||
| 608 | // Fake sport and league. |
||
| 609 | $events = $this->db->getEventsByDay($day, 'FakeSport123', 'FakeLeague123'); |
||
| 610 | $this->assertEmpty($events); |
||
| 611 | |||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @covers TheSportsDb\TheSportsDb::getEventsByLeagueRoundAndSeason |
||
| 616 | */ |
||
| 617 | public function testGetEventsByLeagueRoundAndSeason() { |
||
| 618 | $events = $this->db->getEventsByLeagueRoundAndSeason(4328, 38, '1415'); |
||
| 619 | $this->assertNotEmpty($events); |
||
| 620 | |||
| 621 | foreach ($events as $event) { |
||
| 622 | // Should be an event. |
||
| 623 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 624 | $this->assertEquals(4328, $event->getLeague()->getId()); |
||
| 625 | $this->assertEquals(38, $event->getRound()); |
||
| 626 | $this->assertEquals('1415', $event->getSeason()->getName()); |
||
| 627 | } |
||
| 628 | |||
| 629 | // Fake league. |
||
| 630 | $events = $this->db->getEventsByLeagueRoundAndSeason('FakeLeague123', 38, '1415'); |
||
| 631 | $this->assertEmpty($events); |
||
| 632 | |||
| 633 | // Fake round. |
||
| 634 | $events = $this->db->getEventsByLeagueRoundAndSeason(4328, 'FakeRound123', '1415'); |
||
| 635 | $this->assertEmpty($events); |
||
| 636 | |||
| 637 | // Fake season. |
||
| 638 | $events = $this->db->getEventsByLeagueRoundAndSeason(4328, 38, 'FakeSeason123'); |
||
| 639 | $this->assertEmpty($events); |
||
| 640 | |||
| 641 | // Fake league and round. |
||
| 642 | $events = $this->db->getEventsByLeagueRoundAndSeason('FakeLeague123', 'FakeRound123', '1415'); |
||
| 643 | $this->assertEmpty($events); |
||
| 644 | |||
| 645 | // Fake round and season. |
||
| 646 | $events = $this->db->getEventsByLeagueRoundAndSeason(4328, 'FakeRound123', 'FakeSeason123'); |
||
| 647 | $this->assertEmpty($events); |
||
| 648 | |||
| 649 | // Fake league and season. |
||
| 650 | $events = $this->db->getEventsByLeagueRoundAndSeason('FakeLeague123', 38, 'FakeSeason123'); |
||
| 651 | $this->assertEmpty($events); |
||
| 652 | |||
| 653 | // Fake league round and season. |
||
| 654 | $events = $this->db->getEventsByLeagueRoundAndSeason('FakeLeague123', 'FakeRound123', 'FakeSeason123'); |
||
| 655 | $this->assertEmpty($events); |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @covers TheSportsDb\TheSportsDb::getEventsByLeagueAndSeason |
||
| 660 | */ |
||
| 661 | public function testGetEventsByLeagueAndSeason() { |
||
| 662 | $events = $this->db->getEventsByLeagueAndSeason(4328, '1415'); |
||
| 663 | $this->assertNotEmpty($events); |
||
| 664 | |||
| 665 | foreach ($events as $event) { |
||
| 666 | // Should be an event. |
||
| 667 | $this->assertInstanceOf(EventInterface::class, $event); |
||
| 668 | $this->assertEquals(4328, $event->getLeague()->getId()); |
||
| 669 | $this->assertEquals('1415', $event->getSeason()->getName()); |
||
| 670 | } |
||
| 671 | |||
| 672 | // Fake league. |
||
| 673 | $events = $this->db->getEventsByLeagueAndSeason('FakeLeague123', 38); |
||
| 674 | $this->assertEmpty($events); |
||
| 675 | |||
| 676 | // Fake season. |
||
| 677 | $events = $this->db->getEventsByLeagueAndSeason(4328, 'FakeSeason123'); |
||
| 678 | $this->assertEmpty($events); |
||
| 679 | |||
| 680 | // Fake league and season. |
||
| 681 | $events = $this->db->getEventsByLeagueAndSeason('FakeLeague123', 'FakeSeason123'); |
||
| 682 | $this->assertEmpty($events); |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @covers TheSportsDb\TheSportsDb::getSeasonsByLeague |
||
| 687 | */ |
||
| 688 | public function testGetSeasonsByLeague() { |
||
| 689 | $seasons = $this->db->getSeasonsByLeague(4328); |
||
| 690 | $this->assertNotEmpty($seasons); |
||
| 691 | |||
| 692 | foreach ($seasons as $season) { |
||
| 693 | // Should be an event. |
||
| 694 | $this->assertInstanceOf(SeasonInterface::class, $season); |
||
| 695 | $this->assertEquals(4328, $season->getLeague()->getId()); |
||
| 696 | } |
||
| 697 | |||
| 698 | // Try a fake league. |
||
| 699 | $seasons = $this->db->getSeasonsByLeague('FakeLeague123'); |
||
| 700 | $this->assertEmpty($seasons); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @covers TheSportsDb\TheSportsDb::setEntityManager |
||
| 705 | */ |
||
| 706 | public function testSetEntityManager() { |
||
| 707 | // Entity manager should be set during bootstrap. |
||
| 708 | $this->expectException(\Exception::class); |
||
| 709 | $this->expectExceptionMessage('Entity manager already set.'); |
||
| 710 | $this->db->setEntityManager($this->createMock(EntityManagerInterface::class)); |
||
| 711 | } |
||
| 712 | |||
| 713 | } |
||
| 714 |