PartyTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 207
Duplicated Lines 14.49 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 30
loc 207
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A tearDown() 0 7 1
A testGetParties() 0 8 1
A testGetGameParties() 0 10 1
A testGetParty() 0 16 1
A testGetPartyReturns404WhenPartyDoesNotExists() 10 10 1
A testGetPartyReturns404WhenPartyExistsButInAnotherGame() 0 11 1
A testGetPartyReturns404WhenGameNotFound() 10 10 1
A testCreatePartyReturnsForbiddenWhenNotLogged() 10 10 1
A testCreateParty() 0 20 1
A testJoinPartyIsForbiddenWhenAnonymous() 0 9 1
B testJoinParty() 0 24 1
A testJoinPartyThrowHttpConflictWhenJoinedByAlreadyJoinedPlayer() 0 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\RestApi;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Symfony\Component\HttpFoundation\Response;
7
use Eole\Core\Model\Party;
8
use Eole\Core\Model\Slot;
9
use Eole\Core\Service\PartyManager;
10
11
class PartyTest extends AbstractApplicationTest
12
{
13
    /**
14
     * @param PartyManager
15
     */
16
    private $partyManager;
17
18
    /**
19
     * @var Party
20
     */
21
    private $party;
22
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $dispatcherMock;
27
28
    /**
29
     * {@InheritDoc}
30
     */
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        $this->dispatcherMock = $this->getMock(EventDispatcherInterface::class);
36
        $this->partyManager = new PartyManager($this->dispatcherMock);
37
38
        $party = new Party();
39
40
        $party
41
            ->setGame($this->games[0])
42
            ->setHost($this->player)
43
            ->setSlots(array(
44
                new Slot($party, $this->player),
45
                new Slot($party),
46
            ))
47
        ;
48
49
        $this->party = $party;
50
51
        $this->app['orm.em']->persist($party);
52
        $this->app['orm.em']->flush();
53
    }
54
55
    /**
56
     * {@InheritDoc}
57
     */
58
    protected function tearDown()
59
    {
60
        parent::tearDown();
61
62
        $this->app['db']->executeQuery('delete from eole_core_slot');
63
        $this->app['db']->executeQuery('delete from eole_core_party');
64
    }
65
66
    public function testGetParties()
67
    {
68
        $client = $this->createClient();
69
70
        $client->request('GET', "/api/parties");
71
72
        $this->assertTrue($client->getResponse()->isSuccessful());
73
    }
74
75
    public function testGetGameParties()
76
    {
77
        $client = $this->createClient();
78
79
        $gameName = $this->games[0]->getName();
80
81
        $client->request('GET', "/api/games/$gameName/parties");
82
83
        $this->assertTrue($client->getResponse()->isSuccessful());
84
    }
85
86
    public function testGetParty()
87
    {
88
        $client = $this->createClient();
89
90
        $gameName = $this->games[0]->getName();
91
        $partyId = $this->party->getId();
92
93
        $client->request('GET', "/api/games/$gameName/parties/$partyId");
94
95
        $this->assertTrue($client->getResponse()->isSuccessful());
96
97
        $retrievedParty = json_decode($client->getResponse()->getContent());
98
99
        $this->assertObjectHasAttribute('id', $retrievedParty);
100
        $this->assertEquals($partyId, $retrievedParty->id);
101
    }
102
103 View Code Duplication
    public function testGetPartyReturns404WhenPartyDoesNotExists()
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...
104
    {
105
        $client = $this->createClient();
106
107
        $gameName = $this->games[0]->getName();
108
109
        $client->request('GET', "/api/games/$gameName/parties/978979");
110
111
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode());
112
    }
113
114
    public function testGetPartyReturns404WhenPartyExistsButInAnotherGame()
115
    {
116
        $client = $this->createClient();
117
118
        $gameName = $this->games[1]->getName();
119
        $partyId = $this->party->getId();
120
121
        $client->request('GET', "/api/games/$gameName/parties/$partyId");
122
123
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode());
124
    }
125
126 View Code Duplication
    public function testGetPartyReturns404WhenGameNotFound()
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...
127
    {
128
        $client = $this->createClient();
129
130
        $partyId = $this->party->getId();
131
132
        $client->request('GET', "/api/games/game-inexistant/parties/$partyId");
133
134
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode());
135
    }
136
137 View Code Duplication
    public function testCreatePartyReturnsForbiddenWhenNotLogged()
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...
138
    {
139
        $client = $this->createClient();
140
141
        $gameName = $this->games[0]->getName();
142
143
        $client->request('POST', "/api/games/$gameName/parties");
144
145
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $client->getResponse()->getStatusCode());
146
    }
147
148
    public function testCreateParty()
149
    {
150
        $client = $this->createClient();
151
152
        $gameName = $this->games[0]->getName();
153
154
        $client->request('POST', "/api/games/$gameName/parties", [], [], array(
155
            'HTTP_AUTHORIZATION' => self::createOAuth2Token('existing-player'),
156
        ));
157
158
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
159
160
        $createdParty = json_decode($client->getResponse()->getContent());
161
162
        $this->assertObjectHasAttribute('id', $createdParty);
163
        $this->assertObjectHasAttribute('slots', $createdParty);
164
        $this->assertObjectHasAttribute('host', $createdParty);
165
166
        $this->assertEquals('existing-player', $createdParty->host->username, 'Player whose create a party is the host.');
167
    }
168
169
    public function testJoinPartyIsForbiddenWhenAnonymous()
170
    {
171
        $client = $this->createClient();
172
173
        $gameName = $this->games[0]->getName();
174
        $partyId = $this->party->getId();
175
176
        $client->request('PATCH', "/api/games/$gameName/parties/$partyId/join");
177
    }
178
179
    public function testJoinParty()
180
    {
181
        $client = $this->createClient();
182
183
        $gameName = $this->games[0]->getName();
184
        $partyId = $this->party->getId();
185
186
        $client->request('PATCH', "/api/games/$gameName/parties/$partyId/join", [], [], array(
187
            'HTTP_AUTHORIZATION' => self::createOAuth2Token($this->player2->getUsername()),
188
        ));
189
190
        $this->assertTrue($client->getResponse()->isSuccessful());
191
192
        $position = json_decode($client->getResponse()->getContent());
193
194
        $this->assertInternalType('int', $position);
195
        $this->assertGreaterThanOrEqual(0, $position);
196
197
        $client->request('GET', "/api/games/$gameName/parties/$partyId");
198
199
        $retrievedParty = json_decode($client->getResponse()->getContent());
200
201
        $this->assertEquals($this->player2->getUsername(), $retrievedParty->slots[1]->player->username);
202
    }
203
204
    public function testJoinPartyThrowHttpConflictWhenJoinedByAlreadyJoinedPlayer()
205
    {
206
        $client = $this->createClient();
207
208
        $gameName = $this->games[0]->getName();
209
        $partyId = $this->party->getId();
210
211
        $client->request('PATCH', "/api/games/$gameName/parties/$partyId/join", [], [], array(
212
            'HTTP_AUTHORIZATION' => self::createOAuth2Token('existing-player'),
213
        ));
214
215
        $this->assertEquals(Response::HTTP_CONFLICT, $client->getResponse()->getStatusCode());
216
    }
217
}
218