Passed
Push — master ( 4b85e8...da6ead )
by Tomáš
01:57
created

TournamentTest   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 507
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 205
dl 0
loc 507
rs 9.68
c 1
b 0
f 0
wmc 34
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
5
/**
6
 *
7
 */
8
class TournamentTest extends TestCase
9
{
10
11
	/** @test */
12
	public function check_if_name_is_setup_and_can_get_tournament() {
13
14
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
15
16
		// Test getName() method
17
		$this->assertEquals('Name of tournament 1', $tournament->getName());
18
19
		// Test __toString() method
20
		$this->assertEquals('Name of tournament 1', (string) $tournament);
21
22
		// Test setName() method
23
		$tournament->setName('Name of tournament 2');
24
		$this->assertEquals('Name of tournament 2', $tournament->getName());
25
26
	}
27
28
	/** @test */
29
	public function check_play_time_tournament() {
30
31
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
32
33
		// Test getPlay() method
34
		$tournament->setPlay(123);
35
		$this->assertEquals(123, $tournament->getPlay());
36
37
	}
38
39
	/** @test */
40
	public function check_game_wait_time_tournament() {
41
42
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
43
44
		// Test getPlay() method
45
		$tournament->setGameWait(123);
46
		$this->assertEquals(123, $tournament->getGameWait());
47
48
	}
49
50
	/** @test */
51
	public function check_round_wait_time_tournament() {
52
53
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
54
55
		// Test getPlay() method
56
		$tournament->setRoundWait(123);
57
		$this->assertEquals(123, $tournament->getRoundWait());
58
59
	}
60
61
	/** @test */
62
	public function check_category_wait_time_tournament() {
63
64
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
65
66
		// Test getPlay() method
67
		$tournament->setCategoryWait(123);
68
		$this->assertEquals(123, $tournament->getCategoryWait());
69
70
	}
71
72
	/** @test */
73
	public function check_skip_tournament() {
74
75
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
76
77
		// Test allowSkip() method
78
		$tournament->allowSkip();
79
		$this->assertTrue($tournament->getSkip());
80
81
		// Test disallowSkip() method
82
		$tournament->disallowSkip();
83
		$this->assertFalse($tournament->getSkip());
84
85
		// Test setSkip() method
86
		$tournament->setSkip(true);
87
		$this->assertTrue($tournament->getSkip());
88
89
	}
90
91
	/** @test */
92
	public function check_category_add_tournament() {
93
94
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
95
96
		$category = new \TournamentGenerator\Category('Category name');
97
		$category2 = new \TournamentGenerator\Category('Category name 2');
98
		$category3 = new \TournamentGenerator\Category('Category name 3');
99
100
		// Test adding a single category
101
		$output = $tournament->addCategory($category);
102
		$this->assertCount(1, $tournament->getCategories());
103
104
		// Test if the output is $this
105
		$this->assertInstanceOf('\\TournamentGenerator\\Tournament', $output);
106
107
		// Test adding multiple categories
108
		$tournament->addCategory($category2, $category3);
109
		$this->assertCount(3, $tournament->getCategories());
110
111
		// Test adding not a category class
112
		$this->expectException(TypeError::class);
113
		$tournament->addCategory('totally not a Category class');
114
115
	}
116
117
	/** @test */
118
	public function check_category_creation_tournament() {
119
120
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
121
122
		$category = $tournament->category('Category name');
123
124
		// Test if Category class is really created
125
		$this->assertInstanceOf('\\TournamentGenerator\\Category', $category);
126
		// Test if the category was added
127
		$this->assertCount(1, $tournament->getCategories());
128
129
	}
130
131
	/** @test */
132
	public function check_category_inherits_skip_tournament() {
133
134
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
135
136
		$tournament->allowSkip();
137
		$category = $tournament->category('Category name');
138
139
		$this->assertTrue($category->getSkip());
140
141
	}
142
143
	/** @test */
144
	public function check_round_add_tournament() {
145
146
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
147
148
		$round = new \TournamentGenerator\Round('Round name');
149
		$round2 = new \TournamentGenerator\Round('Round name 2');
150
		$round3 = new \TournamentGenerator\Round('Round name 3');
151
152
		// Test adding a single round
153
		$output = $tournament->addRound($round);
154
		$this->assertCount(1, $tournament->getRounds());
155
156
		// Test if the output is $this
157
		$this->assertInstanceOf('\\TournamentGenerator\\Tournament', $output);
158
159
		// Test adding multiple rounds
160
		$tournament->addRound($round2, $round3);
161
		$this->assertCount(3, $tournament->getRounds());
162
163
		// Test adding not a round class
164
		$this->expectException(TypeError::class);
165
		$tournament->addRound('totally not a Round class');
166
167
	}
168
169
	/** @test */
170
	public function check_round_creation_tournament() {
171
172
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
173
174
		$round = $tournament->round('Round name');
175
176
		// Test if Round class is really created
177
		$this->assertInstanceOf('\\TournamentGenerator\\Round', $round);
178
		// Test if the round was added
179
		$this->assertCount(1, $tournament->getRounds());
180
181
	}
182
183
	/** @test */
184
	public function check_round_inherits_skip_tournament() {
185
186
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
187
188
		$tournament->allowSkip();
189
		$round = $tournament->round('Round name');
190
191
		$this->assertTrue($round->getSkip());
192
193
	}
194
195
	/** @test */
196
	public function check_team_add_tournament() {
197
198
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
199
200
		$team = new \TournamentGenerator\Team('Team name');
201
		$team2 = new \TournamentGenerator\Team('Team name 2');
202
		$team3 = new \TournamentGenerator\Team('Team name 3');
203
204
		// Test adding a single team
205
		$output = $tournament->addTeam($team);
206
		$this->assertCount(1, $tournament->getTeams());
207
208
		// Test if the output is $this
209
		$this->assertInstanceOf('\\TournamentGenerator\\Tournament', $output);
210
211
		// Test adding multiple teams
212
		$tournament->addTeam($team2, $team3);
213
		$this->assertCount(3, $tournament->getTeams());
214
215
		// Test adding not a team class
216
		$this->expectException(TypeError::class);
217
		$tournament->addTeam('totally not a Team class');
218
219
	}
220
221
	/** @test */
222
	public function check_team_creation_tournament() {
223
224
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
225
226
		$team = $tournament->team('Team name');
227
228
		// Test if Team class is really created
229
		$this->assertInstanceOf('\\TournamentGenerator\\Team', $team);
230
		// Test if the team was added
231
		$this->assertCount(1, $tournament->getTeams());
232
233
	}
234
235
	/** @test */
236
	public function check_rounds_from_categories_tournament() {
237
238
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
239
240
		$category1 = $tournament->category('Category 1');
241
		$category2 = $tournament->category('Category 2');
242
243
		$category1->round('Round1');
244
		$category1->round('Round2');
245
246
		$category2->round('Round3');
247
		$category2->round('Round4');
248
249
		$tournament->round('Round5');
250
251
		$this->assertCount(5, $tournament->getRounds());
252
253
	}
254
255
	/** @test */
256
	public function check_teams_from_categories_and_rounds_tournament() {
257
258
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
259
260
		$category1 = $tournament->category('Category 1');
261
		$category2 = $tournament->category('Category 2');
262
263
		$round1 = $tournament->round('Round1');
264
		$round2 = $tournament->round('Round1');
265
266
		$category1->addTeam(new \TournamentGenerator\Team('Team1'),new \TournamentGenerator\Team('Team2'),new \TournamentGenerator\Team('Team3'));
267
		$category2->addTeam(new \TournamentGenerator\Team('Team4'),new \TournamentGenerator\Team('Team5'),new \TournamentGenerator\Team('Team6'));
268
269
		$round1->addTeam(new \TournamentGenerator\Team('Team7'),new \TournamentGenerator\Team('Team8'),new \TournamentGenerator\Team('Team9'));
270
		$round2->addTeam(new \TournamentGenerator\Team('Team10'),new \TournamentGenerator\Team('Team11'),new \TournamentGenerator\Team('Team12'));
271
272
		$tournament->addTeam(new \TournamentGenerator\Team('Team13'),new \TournamentGenerator\Team('Team14'),new \TournamentGenerator\Team('Team15'));
273
274
		$this->assertCount(15, $tournament->getTeams());
275
		// Test if teams does not duplicate
276
		$this->assertCount(15, $tournament->getTeams());
277
278
	}
279
280
	/** @test */
281
	public function check_split_teams_tournament() {
282
283
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
284
285
		for ($i=1; $i <= 8; $i++) {
286
			$tournament->team('Team '.$i);
287
		}
288
289
		$round = $tournament->round('Round name');
290
291
		$group1 = $round->group('Group 1');
292
		$group2 = $round->group('Group 2');
293
294
		$tournament->splitTeams();
295
296
		$this->assertCount(4, $group1->getTeams());
297
		$this->assertCount(4, $group2->getTeams());
298
299
	}
300
301
	/** @test */
302
	public function check_split_teams_with_defined_groups_tournament() {
303
304
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
305
306
		for ($i=1; $i <= 8; $i++) {
307
			$tournament->team('Team '.$i);
308
		}
309
310
		$round = $tournament->round('Round name');
311
		$round2 = $tournament->round('Round 2 name');
312
313
		$group1 = $round->group('Group 1');
314
		$group2 = $round->group('Group 2');
315
		$group3 = $round2->group('Group 3');
316
317
		$tournament->splitTeams($round);
318
319
		$this->assertCount(4, $group1->getTeams());
320
		$this->assertCount(4, $group2->getTeams());
321
		$this->assertCount(0, $group3->getTeams());
322
323
	}
324
325
	protected function gen_tournament() {
326
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
327
328
		$tournament
329
			->setPlay(7) // SET GAME TIME TO 7 MINUTES
330
			->setGameWait(2) // SET TIME BETWEEN GAMES TO 2 MINUTES
331
			->setRoundWait(0); // SET TIME BETWEEN ROUNDS TO 0 MINUTES
332
333
		for ($i=1; $i <= 8; $i++) {
334
			$tournament->team('Team '.$i);
335
		}
336
		// Create a round and a final round
337
		$round = $tournament->round("First's round's name");
338
		$final = $tournament->round("Final's round's name");
339
340
		// Create 2 groups for the first round
341
		$group_1 = $round->group('Round 1')->setInGame(2)->setType(TournamentGenerator\Constants::ROUND_ROBIN);
342
		$group_2 = $round->group('Round 2')->setInGame(2)->setType(TournamentGenerator\Constants::ROUND_ROBIN);
343
344
		// Create a final group
345
		$final_group = $final->group('Finale')->setInGame(2)->setType(TournamentGenerator\Constants::ROUND_ROBIN);
346
347
		$tournament->splitTeams($round);
348
349
		$group_1->progression($final_group, 0, 2); // PROGRESS 2 BEST WINNING TEAMS
350
		$group_2->progression($final_group, 0, 2); // PROGRESS 2 BEST WINNING TEAMS
351
352
		return $tournament;
353
	}
354
355
	/** @test */
356
	public function check_gen_games_simulate_tournament() {
357
358
		$tournament = $this->gen_tournament();
359
360
		$games = $tournament->genGamesSimulate();
361
362
		$this->assertCount(18, $games);
363
364
	}
365
366
	/** @test */
367
	public function check_gen_games_simulate_with_time_tournament() {
368
369
		$tournament = $this->gen_tournament();
370
371
		$time = $tournament->genGamesSimulate(true);
372
373
		$this->assertEquals(162, $time);
374
375
	}
376
377
	/** @test */
378
	public function check_gen_games_simulate_real_tournament() {
379
380
		$tournament = $this->gen_tournament();
381
382
		$games = $tournament->genGamesSimulateReal();
383
384
		$this->assertCount(18, $games);
385
386
	}
387
388
	/** @test */
389
	public function check_gen_games_simulate_real_with_time_tournament() {
390
391
		$tournament = $this->gen_tournament();
392
393
		$time = $tournament->genGamesSimulateReal(true);
394
395
		$this->assertEquals(162, $time);
396
397
	}
398
399
	/** @test */
400
	public function check_tournament_time_tournament() {
401
402
		$tournament = $this->gen_tournament();
403
404
		$tournament->genGamesSimulate();
405
406
		$this->assertEquals(162, $tournament->getTournamentTime());
407
408
	}
409
410
	/** @test */
411
	public function check_getting_games_tournament() {
412
413
		$tournament = $this->gen_tournament();
414
415
		$tournament->genGamesSimulate();
416
417
		$this->assertCount(18, $tournament->getGames());
418
419
	}
420
421
	/** @test */
422
	public function check_sorting_teams_tournament() {
423
424
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
425
426
		$round = $tournament->round('Round name');
427
428
		$group = $round->group('Group name');
429
430
		for ($i=1; $i <= 4; $i++) {
431
			$tournament->team('Team '.$i);
432
		}
433
434
		$teams = $tournament->getTeams();
435
436
		$group->addTeam($teams);
437
438
439
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
440
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
441
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
442
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
443
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
444
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
445
446
		$teamsSorted = array_map(function($team) {
447
			return $team->getName();
448
		}, $tournament->sortTeams());
449
450
		$this->assertSame(['Team 3', 'Team 2', 'Team 1', 'Team 4'], $teamsSorted);
451
452
	}
453
454
	/** @test */
455
	public function check_sorting_teams_from_getTeams_tournament() {
456
457
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
458
459
		$round = $tournament->round('Round name');
460
461
		$group = $round->group('Group name');
462
463
		for ($i=1; $i <= 4; $i++) {
464
			$tournament->team('Team '.$i);
465
		}
466
467
		$teams = $tournament->getTeams();
468
469
		$group->addTeam($teams);
470
471
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
472
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
473
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
474
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
475
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
476
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
477
478
		$teamsSorted = array_map(function($team) {
479
			return $team->getName();
480
		}, $tournament->getTeams(true));
481
482
		$this->assertSame(['Team 3', 'Team 2', 'Team 1', 'Team 4'], $teamsSorted);
483
484
	}
485
486
	/** @test */
487
	public function check_sorting_teams_by_score_tournament() {
488
489
		$tournament = new \TournamentGenerator\Tournament('Name of tournament 1');
490
491
		$round = $tournament->round('Round name');
492
493
		$group = $round->group('Group name');
494
495
		for ($i=1; $i <= 4; $i++) {
496
			$tournament->team('Team '.$i);
497
		}
498
499
		$teams = $tournament->getTeams();
500
501
		$group->addTeam($teams);
502
503
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
504
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
505
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
506
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
507
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
508
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
509
510
		$teamsSorted = array_map(function($team) {
511
			return $team->getName();
512
		}, $tournament->sortTeams(\TournamentGenerator\Constants::SCORE));
513
514
		$this->assertSame(['Team 2', 'Team 1', 'Team 4', 'Team 3'], $teamsSorted);
515
516
	}
517
518
}
519