FixtureTest::testScoreMatchesEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace OSS\CoreBundle\Tests\Entity;
4
5
use OSS\CoreBundle\Entity\Event;
6
use OSS\CoreBundle\Entity\Fixture;
7
use OSS\CoreBundle\Entity\Player;
8
use OSS\CoreBundle\Entity\Team;
9
use OSS\CoreBundle\Services\MatchEvaluationService;
10
11
class FixtureTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @expectedException \OSS\CoreBundle\Exception\MatchException
15
     */
16
    public function testSetTeam()
17
    {
18
        $fixture = new Fixture();
19
        $team = new Team();
20
21
        $fixture->setTeamHome($team);
22
        $fixture->setTeamAway($team);
23
    }
24
25
    public function testScoreMatchesEvents()
26
    {
27
        $fixture = new Fixture();
28
        $team1 = new Team();
29
        $team1->setId(1);
30
        $team2 = new Team();
31
        $team2->setId(2);
32
        $fixture->setTeamHome($team1);
33
        $fixture->setTeamAway($team2);
34
35
        $this->assertEquals(0, $this->countGoalsInEvents($fixture, $fixture->getTeamHome()));
36
        $this->assertEquals(0, $this->countGoalsInEvents($fixture, $fixture->getTeamAway()));
37
        $this->assertEquals(0, $fixture->getGoalsScored());
38
39
        $fixture->addEvent(Event::createGoal($fixture, $fixture->getTeamHome(), new Player(), 1));
40
        $this->assertEquals(1, $this->countGoalsInEvents($fixture, $fixture->getTeamHome()));
41
        $this->assertEquals(0, $this->countGoalsInEvents($fixture, $fixture->getTeamAway()));
42
        $this->assertEquals(1, $fixture->getScoreHome());
43
        $this->assertEquals(0, $fixture->getScoreAway());
44
        $this->assertEquals(1, $fixture->getGoalsScored());
45
    }
46
47
    public function testNoScoreBeforeMatchStart()
48
    {
49
        $fixture = new Fixture();
50
        $this->assertNull($fixture->getScoreHome());
51
        $this->assertNull($fixture->getScoreAway());
52
    }
53
54
    public function testZeroScoreAfterMatchStart()
55
    {
56
        $fixture = new Fixture();
57
        $team1 = new Team();
58
        $team1->setId(1);
59
        $team1->addPlayer(new Player());
60
        $team2 = new Team();
61
        $team2->setId(2);
62
        $team2->addPlayer(new Player());
63
        $fixture->setTeamHome($team1);
64
        $fixture->setTeamAway($team2);
65
66
        $matchEvaluationService = new MatchEvaluationService();
67
        $matchEvaluationService->evaluateMinuteOfMatch($fixture);
68
        $this->assertNotNull($fixture->getScoreHome());
69
        $this->assertNotNull($fixture->getScoreAway());
70
    }
71
72
    /**
73
     * @expectedException \OSS\CoreBundle\Exception\MatchException
74
     */
75
    public function testNoWinnerException()
76
    {
77
        $fixture = new Fixture();
78
        $fixture->resetScoreHome();
79
        $fixture->resetScoreAway();
80
81
        $fixture->getWinner();
82
    }
83
84
    public function testDraw()
85
    {
86
        $fixture = new Fixture();
87
        $fixture->resetScoreHome();
88
        $fixture->resetScoreAway();
89
90
        $this->assertTrue($fixture->isDraw());
91
    }
92
93
    public function testGetGoalEvents()
94
    {
95
        $team1 = new Team();
96
        $team1->setId(1);
97
        $team2 = new Team();
98
        $team2->setId(2);
99
100
        $fixture = new Fixture();
101
        $fixture->setTeamHome($team1);
102
        $fixture->setTeamAway($team2);
103
        $fixture->addEvent(Event::createGoal($fixture, $team1, new Player(), 1));
104
        $fixture->addEvent(Event::createChance($fixture, $team2, new Player(), 2));
105
106
        $this->assertCount(1, $fixture->getGoalEvents());
107
    }
108
109
    /**
110
     * @param Fixture $fixture
111
     * @param Team $team
112
     *
113
     * @return int
114
     */
115
    private function countGoalsInEvents(Fixture $fixture, Team $team)
116
    {
117
        $goalsInEvents = 0;
118
        foreach ($fixture->getEvents() as $event) {
119
            if ($event->isGoal() && $event->getTeam()->equals($team)) {
120
                $goalsInEvents++;
121
            }
122
        }
123
124
        return $goalsInEvents;
125
    }
126
}
127