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

RoundTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 1
b 0
f 0
wmc 1
1
<?php
2
use PHPUnit\Framework\TestCase;
3
4
/**
5
 *
6
 */
7
class RoundTest extends TestCase
8
{
9
10
	/** @test */
11
	public function check_name_setup_round() {
12
		$round = new \TournamentGenerator\Round('Round name 1');
13
14
		$this->assertEquals('Round name 1', $round->getName());
15
		$this->assertEquals('Round name 1', (string) $round);
16
17
		$round->setName('Round name 2');
18
19
		$this->assertEquals('Round name 2', $round->getName());
20
	}
21
22
	/** @test */
23
	public function check_id_setup_round() {
24
		$round = new \TournamentGenerator\Round('Round name 1', 123);
25
26
		$this->assertEquals(123, $round->getId());
27
28
		$round->setId('ID2');
29
30
		$this->assertEquals('ID2', $round->getId());
31
32
		$this->expectException(Exception::class);
33
		$round->setId(['This', 'is', 'not', 'a', 'valid' => 'id']);
34
	}
35
36
	/** @test */
37
	public function check_group_add_round() {
38
39
		$round = new \TournamentGenerator\Round('Name of round 1');
40
41
		$group = new \TournamentGenerator\Group('Group name');
42
		$group2 = new \TournamentGenerator\Group('Group name 2');
43
		$group3 = new \TournamentGenerator\Group('Group name 3');
44
45
		// Test adding a single group
46
		$output = $round->addGroup($group);
47
		$this->assertCount(1, $round->getGroups());
48
49
		// Test if the output is $this
50
		$this->assertInstanceOf('\\TournamentGenerator\\Round', $output);
51
52
		// Test adding multiple categories
53
		$round->addGroup($group2, $group3);
54
		$this->assertCount(3, $round->getGroups());
55
56
		// Test adding not a group class
57
		$this->expectException(TypeError::class);
58
		$round->addGroup('totally not a Group class');
59
60
	}
61
62
	/** @test */
63
	public function check_group_creation_round() {
64
65
		$round = new \TournamentGenerator\Round('Name of round 1');
66
67
		$group = $round->group('Group name');
68
69
		// Test if Group class is really created
70
		$this->assertInstanceOf('\\TournamentGenerator\\Group', $group);
71
		// Test if the group was added
72
		$this->assertCount(1, $round->getGroups());
73
74
	}
75
76
	/** @test */
77
	public function check_getting_group_ids_round() {
78
79
		$round = new \TournamentGenerator\Round('Name of round 1');
80
81
		$group = $round->group('Group name', 'id1');
82
		$group2 = $round->group('Group name', 'id2');
83
		$group3 = $round->group('Group name', 12345);
84
85
		// Test if the group was added
86
		$this->assertCount(3, $round->getGroupsIds());
87
		$this->assertSame(['id1', 'id2', 12345], $round->getGroupsIds());
88
89
	}
90
91
	/** @test */
92
	public function check_ordering_groups_round() {
93
94
		$round = new \TournamentGenerator\Round('Name of round 1');
95
96
		$group = $round->group('Group name', 1)->setOrder(2);
97
		$group2 = $round->group('Group name', 2)->setOrder(3);
98
		$group3 = $round->group('Group name', 3)->setOrder(1);
99
100
		// Test if the group was added
101
		$groups = array_map(function($a) {
102
			return $a->getId();
103
		}, $round->orderGroups());
104
		$this->assertSame([3, 1, 2], $groups);
105
106
	}
107
108
	/** @test */
109
	public function check_skip_round() {
110
111
		$round = new \TournamentGenerator\Round('Name of round');
112
113
		// Test allowSkip() method
114
		$round->allowSkip();
115
		$this->assertTrue($round->getSkip());
116
117
		// Test disallowSkip() method
118
		$round->disallowSkip();
119
		$this->assertFalse($round->getSkip());
120
121
		// Test setSkip() method
122
		$round->setSkip(true);
123
		$this->assertTrue($round->getSkip());
124
125
	}
126
127
	/** @test */
128
	public function check_group_inherits_skip_round() {
129
130
		$round = new \TournamentGenerator\Round('Name of round');
131
132
		$round->allowSkip();
133
		$group = $round->group('Group name');
134
135
		$this->assertTrue($group->getSkip());
136
137
	}
138
139
	/** @test */
140
	public function check_team_add_round() {
141
142
		$round = new \TournamentGenerator\Round('Name of round');
143
144
		$team = new \TournamentGenerator\Team('Team name');
145
		$team2 = new \TournamentGenerator\Team('Team name 2');
146
		$team3 = new \TournamentGenerator\Team('Team name 3');
147
148
		// Test adding a single team
149
		$output = $round->addTeam($team);
150
		$this->assertCount(1, $round->getTeams());
151
152
		// Test if the output is $this
153
		$this->assertInstanceOf('\\TournamentGenerator\\Round', $output);
154
155
		// Test adding multiple teams
156
		$round->addTeam($team2, $team3);
157
		$this->assertCount(3, $round->getTeams());
158
159
		// Test adding not a team class
160
		$this->expectException(TypeError::class);
161
		$round->addTeam('totally not a Team class');
162
163
	}
164
165
	/** @test */
166
	public function check_team_creation_round() {
167
168
		$round = new \TournamentGenerator\Round('Name of round');
169
170
		$team = $round->team('Team name');
171
172
		// Test if Team class is really created
173
		$this->assertInstanceOf('\\TournamentGenerator\\Team', $team);
174
		// Test if the team was added
175
		$this->assertCount(1, $round->getTeams());
176
177
	}
178
179
	/** @test */
180
	public function check_split_teams_round() {
181
182
		$round = new \TournamentGenerator\Round('Name of round');
183
184
		for ($i=1; $i <= 8; $i++) {
185
			$round->team('Team '.$i);
186
		}
187
188
		$group1 = $round->group('Group 1');
189
		$group2 = $round->group('Group 2');
190
191
		$round->splitTeams();
192
193
		$this->assertCount(4, $group1->getTeams());
194
		$this->assertCount(4, $group2->getTeams());
195
196
	}
197
198
	/** @test */
199
	public function check_split_teams_with_defined_groups_round() {
200
201
		$round = new \TournamentGenerator\Round('Name of tournament 1');
202
203
		for ($i=1; $i <= 8; $i++) {
204
			$round->team('Team '.$i);
205
		}
206
207
		$group1 = $round->group('Group 1');
208
		$group2 = $round->group('Group 2');
209
		$group3 = $round->group('Group 3');
210
211
		$round->splitTeams($group1, $group2);
212
213
		$this->assertCount(4, $group1->getTeams());
214
		$this->assertCount(4, $group2->getTeams());
215
		$this->assertCount(0, $group3->getTeams());
216
217
	}
218
219
	/** @test */
220
	public function check_sorting_teams_round() {
221
222
		$round = new \TournamentGenerator\Round('Name of round');
223
224
		$group = $round->group('Group name', 'group1');
225
226
		for ($i=1; $i <= 4; $i++) {
227
			$round->team('Team '.$i);
228
		}
229
230
		$teams = $round->getTeams();
231
232
		$group->addTeam($teams);
233
234
		/**
235
		*	Team 1 - Wins: 1 Draws: 0 Losses: 2 Score: 3300 By points: #3 By score: #2
236
		*	Team 2 - Wins: 2 Draws: 0 Losses: 1 Score: 7100 By points: #2 By score: #1
237
		*	Team 3 - Wins: 3 Draws: 0 Losses: 0 Score: 400  By points: #1 By score: #4
238
		*	Team 4 - Wins: 0 Draws: 0 Losses: 3 Score: 2099 By points: #4s By score: #3
239
		*/
240
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
241
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
242
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
243
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
244
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
245
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
246
247
		$teamsSorted = array_map(function($team) {
248
			return $team->getName();
249
		}, $round->sortTeams());
250
251
		$this->assertSame(['Team 3', 'Team 2', 'Team 1', 'Team 4'], $teamsSorted);
252
253
	}
254
255
	/** @test */
256
	public function check_sorting_teams_from_getTeams_round() {
257
258
		$round = new \TournamentGenerator\Round('Name of round');
259
260
		$group = $round->group('Group name');
261
262
		for ($i=1; $i <= 4; $i++) {
263
			$round->team('Team '.$i);
264
		}
265
266
		$teams = $round->getTeams();
267
268
		$group->addTeam($teams);
269
270
		/**
271
		*	Team 1 - Wins: 1 Draws: 0 Losses: 2 Score: 3300 By points: #3 By score: #2
272
		*	Team 2 - Wins: 2 Draws: 0 Losses: 1 Score: 7100 By points: #2 By score: #1
273
		*	Team 3 - Wins: 3 Draws: 0 Losses: 0 Score: 400  By points: #1 By score: #4
274
		*	Team 4 - Wins: 0 Draws: 0 Losses: 3 Score: 2099 By points: #4s By score: #3
275
		*/
276
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
277
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
278
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
279
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
280
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
281
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
282
283
		$teamsSorted = array_map(function($team) {
284
			return $team->getName();
285
		}, $round->getTeams(true));
286
287
		$this->assertSame(['Team 3', 'Team 2', 'Team 1', 'Team 4'], $teamsSorted);
288
289
	}
290
291
	/** @test */
292
	public function check_sorting_teams_by_score_round() {
293
294
		$round = new \TournamentGenerator\Round('Name of round');
295
296
		$group = $round->group('Group name');
297
298
		for ($i=1; $i <= 4; $i++) {
299
			$round->team('Team '.$i);
300
		}
301
302
		$teams = $round->getTeams();
303
304
		$group->addTeam($teams);
305
306
		/**
307
		*	Team 1 - Wins: 1 Draws: 0 Losses: 2 Score: 3300 By points: #3 By score: #2
308
		*	Team 2 - Wins: 2 Draws: 0 Losses: 1 Score: 7100 By points: #2 By score: #1
309
		*	Team 3 - Wins: 3 Draws: 0 Losses: 0 Score: 400  By points: #1 By score: #4
310
		*	Team 4 - Wins: 0 Draws: 0 Losses: 3 Score: 2099 By points: #4s By score: #3
311
		*/
312
		$group->game([$teams[0], $teams[1]])->setResults([$teams[0]->getId() => 2000, $teams[1]->getId() => 2001]);
313
		$group->game([$teams[2], $teams[3]])->setResults([$teams[2]->getId() => 100, $teams[3]->getId() => 99]);
314
		$group->game([$teams[0], $teams[2]])->setResults([$teams[0]->getId() => 199, $teams[2]->getId() => 200]);
315
		$group->game([$teams[1], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[1]->getId() => 5000]);
316
		$group->game([$teams[0], $teams[3]])->setResults([$teams[3]->getId() => 1000, $teams[0]->getId() => 1001]);
317
		$group->game([$teams[1], $teams[2]])->setResults([$teams[1]->getId() => 99, $teams[2]->getId() => 100]);
318
319
		$teamsSorted = array_map(function($team) {
320
			return $team->getName();
321
		}, $round->sortTeams(\TournamentGenerator\Constants::SCORE));
322
323
		$this->assertSame(['Team 2', 'Team 1', 'Team 4', 'Team 3'], $teamsSorted);
324
325
	}
326
327
	/** @test */
328
	public function check_games_generation_round() {
329
330
		$round = new \TournamentGenerator\Round('Name of round');
331
332
		$group = $round->group('Group name')->setType(\TournamentGenerator\Constants::ROUND_ROBIN);
333
		$group2 = $round->group('Group name')->setType(\TournamentGenerator\Constants::ROUND_ROBIN);
334
335
		for ($i=1; $i <= 8; $i++) {
336
			$round->team('Team '.$i);
337
		}
338
339
		$round->splitTeams();
340
341
		$this->assertCount(12, $round->genGames());
342
		$this->assertCount(12, $round->getGames());
343
	}
344
345
	/** @test */
346
	public function check_played_round() {
347
348
		$round = new \TournamentGenerator\Round('Name of round');
349
350
		$group = $round->group('Group name')->setType(\TournamentGenerator\Constants::ROUND_ROBIN);
351
		$group2 = $round->group('Group name')->setType(\TournamentGenerator\Constants::ROUND_ROBIN);
352
353
		for ($i=1; $i <= 8; $i++) {
354
			$round->team('Team '.$i);
355
		}
356
357
		$round->splitTeams();
358
359
		$this->assertFalse($round->isPlayed());
360
361
		$round->genGames();
362
363
		$this->assertFalse($round->isPlayed());
364
365
		$round->simulate();
366
367
		$this->assertTrue($round->isPlayed());
368
369
		$round->resetGames();
370
371
		$this->assertFalse($round->isPlayed());
372
	}
373
374
	/** @test */
375
	public function check_progress_round() {
376
377
		$round = new \TournamentGenerator\Round('Name of round');
378
		$round2 = new \TournamentGenerator\Round('Name of round 2');
379
380
		$group = $round->group('Group 1');
381
		$group2 = $round2->group('Group 2 - wins');
382
		$group3 = $round2->group('Group 3 - losses');
383
384
		for ($i=1; $i <= 4; $i++) {
385
			$round->team('Team '.$i);
386
		}
387
388
		$round->splitTeams();
389
390
		$group->progression($group2, 0, 2); // Progress 2 teams
391
		$group->progression($group3, -2, 2); // Progress 2 teams
392
393
		$round->genGames();
394
		$round->simulate();
395
		$round->progress();
396
397
		$this->assertCount(4, $round2->getTeams());
398
		$this->assertCount(2, $group2->getTeams());
399
		$this->assertCount(2, $group3->getTeams());
400
401
	}
402
403
	/** @test */
404
	public function check_progress_blank_round() {
405
406
		$round = new \TournamentGenerator\Round('Name of round');
407
		$round2 = new \TournamentGenerator\Round('Name of round 2');
408
409
		$group = $round->group('Group 1');
410
		$group2 = $round2->group('Group 2 - wins');
411
		$group3 = $round2->group('Group 3 - losses');
412
413
		for ($i=1; $i <= 4; $i++) {
414
			$round->team('Team '.$i);
415
		}
416
417
		$round->splitTeams();
418
419
		$group->progression($group2, 0, 2); // Progress 2 teams
420
		$group->progression($group3, -2, 2); // Progress 2 teams
421
422
		$round->genGames();
423
		$round->simulate();
424
		$round->progress(true);
425
426
		$this->assertCount(4, $round2->getTeams());
427
		$this->assertCount(2, $group2->getTeams());
428
		$this->assertCount(2, $group3->getTeams());
429
430
		foreach ($round2->getTeams() as $team) {
431
			$this->assertInstanceOf('\\TournamentGenerator\\BlankTeam', $team);
432
		}
433
434
	}
435
436
}
437