1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace OSS\CoreBundle\Tests\Services;
|
4
|
|
|
|
5
|
|
|
use OSS\CoreBundle\Entity\Fixture;
|
6
|
|
|
use OSS\CoreBundle\Entity\Player;
|
7
|
|
|
use OSS\CoreBundle\Entity\PlayerSkills;
|
8
|
|
|
use OSS\CoreBundle\Entity\Team;
|
9
|
|
|
use OSS\CoreBundle\Exception\MatchException;
|
10
|
|
|
use OSS\CoreBundle\Services\MatchEvaluationService;
|
11
|
|
|
|
12
|
|
|
class MatchEvaluationTest extends \PHPUnit_Framework_TestCase
|
13
|
|
|
{
|
14
|
|
|
/**
|
15
|
|
|
* @var MatchEvaluationService
|
16
|
|
|
*/
|
17
|
|
|
private $matchEvaluationService;
|
18
|
|
|
|
19
|
|
|
public function setUp()
|
20
|
|
|
{
|
21
|
|
|
mt_srand(0); // take care to always generate the same row of random numbers
|
22
|
|
|
$this->matchEvaluationService = new MatchEvaluationService();
|
23
|
|
|
}
|
24
|
|
|
|
25
|
|
|
public function tearDown()
|
26
|
|
|
{
|
27
|
|
|
$this->matchEvaluationService = null;
|
28
|
|
|
}
|
29
|
|
|
|
30
|
|
View Code Duplication |
public function testHasScore()
|
|
|
|
|
31
|
|
|
{
|
32
|
|
|
$fixture = $this->createFixture($this->createTeam(1), $this->createTeam(2));
|
33
|
|
|
|
34
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch($fixture);
|
35
|
|
|
|
36
|
|
|
$this->assertGreaterThanOrEqual(0, $fixture->getScoreHome());
|
37
|
|
|
$this->assertGreaterThanOrEqual(0, $fixture->getScoreAway());
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* @expectedException \OSS\CoreBundle\Exception\MatchException
|
42
|
|
|
*/
|
43
|
|
|
public function testNoTeamsAssigned()
|
44
|
|
|
{
|
45
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch(new Fixture());
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
public function testIsFinished()
|
49
|
|
|
{
|
50
|
|
|
$fixture = $this->createFixture($this->createTeam(1), $this->createTeam(2));
|
51
|
|
|
|
52
|
|
|
$this->assertFalse($fixture->isFinished());
|
53
|
|
|
|
54
|
|
|
$this->matchEvaluationService->evaluateMinuteOfMatch($fixture);
|
55
|
|
|
$this->assertFalse($fixture->isFinished());
|
56
|
|
|
|
57
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch($fixture);
|
58
|
|
|
$this->assertTrue($fixture->isFinished());
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
View Code Duplication |
public function testEventsGenerated()
|
|
|
|
|
62
|
|
|
{
|
63
|
|
|
$fixture = $this->createFixture($this->createTeam(1), $this->createTeam(2));
|
64
|
|
|
|
65
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch($fixture);
|
66
|
|
|
$this->assertGreaterThan(0, count($fixture->getEvents()));
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
public function testHappensEvent()
|
70
|
|
|
{
|
71
|
|
|
for ($i = 0; $i < 32; $i++) {
|
72
|
|
|
$this->assertFalse($this->matchEvaluationService->happensEvent());
|
73
|
|
|
}
|
74
|
|
|
$this->assertTrue($this->matchEvaluationService->happensEvent());
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
public function testMinutesToPlay()
|
78
|
|
|
{
|
79
|
|
|
$fixture = $this->createFixture($this->createTeam(1), $this->createTeam(2));
|
80
|
|
|
|
81
|
|
|
for ($i = 0; $i <= 10; $i++) {
|
82
|
|
|
$this->assertGreaterThanOrEqual(90, $this->matchEvaluationService->getMinutesToPlay());
|
83
|
|
|
$this->assertLessThanOrEqual(95, $this->matchEvaluationService->getMinutesToPlay());
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch($fixture);
|
87
|
|
|
$this->assertEquals(90, $fixture->getMinutesPlayed());
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
public function testPointsAndGoalsForLeague()
|
91
|
|
|
{
|
92
|
|
|
$team1 = $this->createTeam(1);
|
93
|
|
|
$team2 = $this->createTeam(2);
|
94
|
|
|
$fixture = $this->createFixture($team1, $team2);
|
95
|
|
|
|
96
|
|
|
$this->assertEquals(0, $team1->getPoints());
|
97
|
|
|
$this->assertEquals(0, $team1->getGoalsFor());
|
98
|
|
|
$this->assertEquals(0, $team1->getGoalsAgainst());
|
99
|
|
|
$this->assertEquals(0, $team2->getPoints());
|
100
|
|
|
$this->assertEquals(0, $team2->getGoalsFor());
|
101
|
|
|
$this->assertEquals(0, $team2->getGoalsAgainst());
|
102
|
|
|
|
103
|
|
|
$this->matchEvaluationService->evaluateCompleteMatch($fixture);
|
104
|
|
|
|
105
|
|
|
$this->assertEquals(1, $team1->getPoints());
|
106
|
|
|
$this->assertEquals(0, $team1->getGoalsFor());
|
107
|
|
|
$this->assertEquals(0, $team1->getGoalsAgainst());
|
108
|
|
|
$this->assertEquals(1, $team2->getPoints());
|
109
|
|
|
$this->assertEquals(0, $team2->getGoalsFor());
|
110
|
|
|
$this->assertEquals(0, $team2->getGoalsAgainst());
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* @param int $id
|
115
|
|
|
*
|
116
|
|
|
* @return Team
|
117
|
|
|
*/
|
118
|
|
|
private function createTeam($id)
|
119
|
|
|
{
|
120
|
|
|
$team = new Team();
|
121
|
|
|
$team->setId($id);
|
122
|
|
|
$player = new Player();
|
123
|
|
|
$skills = new PlayerSkills();
|
124
|
|
|
$player->setSkills($skills);
|
125
|
|
|
$team->addPlayer($player);
|
126
|
|
|
|
127
|
|
|
return $team;
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
/**
|
131
|
|
|
* @param Team $teamHome
|
132
|
|
|
* @param Team $teamAway
|
133
|
|
|
*
|
134
|
|
|
* @return Fixture
|
135
|
|
|
*
|
136
|
|
|
* @throws MatchException
|
137
|
|
|
*/
|
138
|
|
|
private function createFixture(Team $teamHome, Team $teamAway)
|
139
|
|
|
{
|
140
|
|
|
$fixture = new Fixture();
|
141
|
|
|
$fixture->setTeamHome($teamHome);
|
142
|
|
|
$fixture->setTeamAway($teamAway);
|
143
|
|
|
|
144
|
|
|
return $fixture;
|
145
|
|
|
}
|
146
|
|
|
}
|
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.