Completed
Push — dev ( 735e11...beed57 )
by Julien
20:45
created

PartyManagerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 229
Duplicated Lines 11.35 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 26
loc 229
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateParty() 0 11 1
A testCreatePartyCreateStandardSlots() 0 13 1
B testGetPlayerPosition() 0 26 1
A testGetPlayerPositionReturnsNullWhenPlayerNotHere() 0 13 1
B testHasPlayer() 0 25 1
B testGetFreeSlotsCount() 0 25 1
A testHasFreeSlot() 0 23 1
A testAddPlayer() 0 22 1
A testAddPlayerThrowsExceptionWhenFull() 0 13 1
A testStartParty() 13 13 1
A testEndParty() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
namespace Tests\Eole\Core\Service;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Eole\Core\Model\Game;
7
use Eole\Core\Model\Player;
8
use Eole\Core\Model\Party;
9
use Eole\Core\Model\Slot;
10
use Eole\Core\Service\PartyManager;
11
12
class PartyManagerTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var PartyManager
16
     */
17
    private $partyManager;
18
19
    /**
20
     * @var EventDispatcherInterface
21
     */
22
    private $dispatcherMock;
23
24
    /**
25
     * {@InheritDoc}
26
     */
27
    public function setUp()
28
    {
29
        $this->dispatcherMock = $this->getMock(EventDispatcherInterface::class);
30
        $this->partyManager = new PartyManager($this->dispatcherMock);
31
    }
32
33
    public function testCreateParty()
34
    {
35
        $game = new Game();
36
        $player = new Player();
37
38
        $createdParty = $this->partyManager->createParty($game, $player);
39
40
        $this->assertSame($game, $createdParty->getGame());
41
        $this->assertSame($player, $createdParty->getHost());
42
        $this->assertEquals(Party::PREPARATION, $createdParty->getState());
43
    }
44
45
    public function testCreatePartyCreateStandardSlots()
46
    {
47
        $game = new Game();
48
        $player = new Player();
49
50
        $createdParty = $this->partyManager->createParty($game, $player);
51
52
        $this->assertCount(2, $createdParty->getSlots());
53
        $this->assertSame($player, $createdParty->getSlot(0)->getPlayer());
54
        $this->assertTrue($createdParty->getSlot(1)->isFree(), 'createParty create a second empty slot.');
55
        $this->assertSame($createdParty, $createdParty->getSlot(0)->getParty());
56
        $this->assertSame($createdParty, $createdParty->getSlot(1)->getParty());
57
    }
58
59
    public function testGetPlayerPosition()
60
    {
61
        $party = new Party();
62
        $player = (new Player())->setId(1);
63
        $otherPlayer = (new Player())->setId(2);
64
65
        $party
66
            ->addSlot(new Slot($party, $player))
67
            ->addSlot(new Slot($party, null))
68
            ->addSlot(new Slot($party, $otherPlayer))
69
        ;
70
71
        $this->assertEquals(0, $this->partyManager->getPlayerPosition($party, $player));
72
        $this->assertEquals(2, $this->partyManager->getPlayerPosition($party, $otherPlayer));
73
74
        $otherParty = new Party();
75
76
        $otherParty
77
            ->addSlot(new Slot($otherParty, null))
78
            ->addSlot(new Slot($otherParty, $otherPlayer))
79
            ->addSlot(new Slot($otherParty, $player))
80
        ;
81
82
        $this->assertEquals(2, $this->partyManager->getPlayerPosition($otherParty, $player));
83
        $this->assertEquals(1, $this->partyManager->getPlayerPosition($otherParty, $otherPlayer));
84
    }
85
86
    public function testGetPlayerPositionReturnsNullWhenPlayerNotHere()
87
    {
88
        $party = new Party();
89
        $player = (new Player())->setId(1);
90
        $otherPlayer = (new Player())->setId(2);
91
92
        $party
93
            ->addSlot(new Slot($party, null))
94
            ->addSlot(new Slot($party, $otherPlayer))
95
        ;
96
97
        $this->assertNull($this->partyManager->getPlayerPosition($party, $player));
98
    }
99
100
    public function testHasPlayer()
101
    {
102
        $party = new Party();
103
        $player = (new Player())->setId(1);
104
        $otherPlayer = (new Player())->setId(2);
105
106
        $party
107
            ->addSlot(new Slot($party, $player))
108
            ->addSlot(new Slot($party, null))
109
            ->addSlot(new Slot($party, $otherPlayer))
110
        ;
111
112
        $this->assertTrue($this->partyManager->hasPlayer($party, $player));
113
        $this->assertTrue($this->partyManager->hasPlayer($party, $otherPlayer));
114
115
        $otherParty = new Party();
116
117
        $otherParty
118
            ->addSlot(new Slot($otherParty, null))
119
            ->addSlot(new Slot($otherParty, $player))
120
        ;
121
122
        $this->assertTrue($this->partyManager->hasPlayer($otherParty, $player));
123
        $this->assertFalse($this->partyManager->hasPlayer($otherParty, $otherPlayer));
124
    }
125
126
    public function testGetFreeSlotsCount()
127
    {
128
        $party = new Party();
129
        $player = new Player();
130
        $otherPlayer = new Player();
131
132
        $party
133
            ->addSlot(new Slot($party, $player))
134
            ->addSlot(new Slot($party, null))
135
            ->addSlot(new Slot($party, $otherPlayer))
136
        ;
137
138
        $this->assertEquals(1, $this->partyManager->getFreeSlotsCount($party));
139
140
        $otherParty = new Party();
141
142
        $otherParty
143
            ->addSlot(new Slot($otherParty, null))
144
            ->addSlot(new Slot($otherParty, null))
145
            ->addSlot(new Slot($otherParty, $player))
146
            ->addSlot(new Slot($otherParty, null))
147
        ;
148
149
        $this->assertEquals(3, $this->partyManager->getFreeSlotsCount($otherParty));
150
    }
151
152
    public function testHasFreeSlot()
153
    {
154
        $party = new Party();
155
        $player = new Player();
156
        $otherPlayer = new Player();
157
158
        $party
159
            ->addSlot(new Slot($party, $player))
160
            ->addSlot(new Slot($party, null))
161
            ->addSlot(new Slot($party, $otherPlayer))
162
        ;
163
164
        $this->assertTrue($this->partyManager->hasFreeSlot($party));
165
166
        $otherParty = new Party();
167
168
        $otherParty
169
            ->addSlot(new Slot($otherParty, $player))
170
            ->addSlot(new Slot($party, $otherPlayer))
171
        ;
172
173
        $this->assertFalse($this->partyManager->hasFreeSlot($otherParty));
174
    }
175
176
    public function testAddPlayer()
177
    {
178
        $party = new Party();
179
        $player = new Player();
180
        $otherPlayer = new Player();
181
182
        $party
183
            ->addSlot(new Slot($party, null))
184
            ->addSlot(new Slot($party, null))
185
            ->addSlot(new Slot($party, null))
186
        ;
187
188
        $playerPosition = $this->partyManager->addPlayer($party, $player);
189
190
        $this->assertEquals(0, $playerPosition);
191
        $this->assertSame($player, $party->getSlot(0)->getPlayer());
192
193
        $otherPlayerPosition = $this->partyManager->addPlayer($party, $otherPlayer);
194
195
        $this->assertEquals(1, $otherPlayerPosition);
196
        $this->assertSame($otherPlayer, $party->getSlot(1)->getPlayer());
197
    }
198
199
    public function testAddPlayerThrowsExceptionWhenFull()
200
    {
201
        $party = new Party();
202
        $player = new Player();
203
        $otherPlayer = new Player();
204
205
        $party
206
            ->addSlot(new Slot($party, $player))
207
        ;
208
209
        $this->setExpectedException('OverflowException');
210
        $this->partyManager->addPlayer($party, $otherPlayer);
211
    }
212
213 View Code Duplication
    public function testStartParty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
214
    {
215
        $party = new Party();
216
217
        $party->setState(Party::PREPARATION);
218
219
        $this->partyManager->startParty($party);
220
221
        $this->assertEquals(Party::ACTIVE, $party->getState());
222
223
        $this->setExpectedException('RuntimeException');
224
        $this->partyManager->startParty($party);
225
    }
226
227 View Code Duplication
    public function testEndParty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
228
    {
229
        $party = new Party();
230
231
        $party->setState(Party::ACTIVE);
232
233
        $this->partyManager->endParty($party);
234
235
        $this->assertEquals(Party::ENDED, $party->getState());
236
237
        $this->setExpectedException('RuntimeException');
238
        $this->partyManager->endParty($party);
239
    }
240
}
241