UserApiTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 317
Duplicated Lines 26.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 84
loc 317
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testRootPathIs404() 0 7 1
A testGetPlayersReturnsArrayOrStdClass() 0 11 2
A testAuthMeIsForbiddenWithoutOAuthToken() 0 8 1
A testAuthMeReturnsExpectedPlayer() 0 16 1
A testAuthMeFailsOnInvalidCredentials() 0 10 1
A testCreatePlayerReturnsCreatedStatusCodeAndValidPlayer() 0 15 1
A testCreatePlayerWithMissingArgumentReturnsBadRequest() 18 18 1
A testCreatePlayerTwiceReturnsConflictStatusCode() 18 18 1
A testCreateGuestReturnsAGuest() 22 22 1
A testCreateGuestGenerateDifferentGuestWhenCalledMultipleTimes() 0 16 1
B testCreateGuestUseProvidedPasswordIfAny() 26 26 1
A testAuthMeCanAuthenticateCreatedGuest() 0 18 1
A testPlayersCountReturnsExpectedValues() 0 19 1
A testDeletePlayer() 0 13 1
A testRegisterGuest() 0 23 1
A testRegisterGuestNeedsToBeLogged() 0 12 1
A testRegisterGuestFailsOnAlreadyPlayer() 0 13 1
A testGetGames() 0 17 1
A testGetGameByNameReturnsExpectedGame() 0 14 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\HttpFoundation\Response;
6
7
class UserApiTest extends AbstractApplicationTest
8
{
9
    public function testRootPathIs404()
10
    {
11
        $client = $this->createClient();
12
13
        $client->request('GET', '/');
14
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode());
15
    }
16
17
    public function testGetPlayersReturnsArrayOrStdClass()
18
    {
19
        $client = $this->createClient();
20
21
        $client->request('GET', '/api/players');
22
        $this->assertTrue($client->getResponse()->isSuccessful());
23
24
        $content = json_decode($client->getResponse()->getContent());
25
26
        $this->assertTrue(is_array($content) || is_object($content));
27
    }
28
29
    public function testAuthMeIsForbiddenWithoutOAuthToken()
30
    {
31
        $client = $this->createClient();
32
33
        $client->request('GET', '/api/auth/me');
34
35
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $client->getResponse()->getStatusCode());
36
    }
37
38
    public function testAuthMeReturnsExpectedPlayer()
39
    {
40
        $client = $this->createClient();
41
42
        $client->request('GET', '/api/auth/me', [], [], array(
43
            'HTTP_AUTHORIZATION' => self::createOAuth2Token('existing-player'),
44
        ));
45
46
        $this->assertTrue($client->getResponse()->isSuccessful());
47
48
        $player = json_decode($client->getResponse()->getContent());
49
50
        $this->assertObjectHasAttribute('id', $player);
51
        $this->assertObjectHasAttribute('username', $player);
52
        $this->assertEquals($player->username, 'existing-player');
53
    }
54
55
    public function testAuthMeFailsOnInvalidCredentials()
56
    {
57
        $client = $this->createClient();
58
59
        $client->request('GET', '/api/auth/me', [], [], array(
60
            'HTTP_AUTHORIZATION' => self::createOAuth2Token('non-existing-player'),
61
        ));
62
63
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $client->getResponse()->getStatusCode());
64
    }
65
66
    public function testCreatePlayerReturnsCreatedStatusCodeAndValidPlayer()
67
    {
68
        $client = $this->createClient();
69
70
        $client->request('POST', '/api/players', array(
71
            'username' => 'test-user',
72
            'password' => 'test-pass',
73
        ));
74
75
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
76
77
        $player = json_decode($client->getResponse()->getContent());
78
79
        $this->assertEquals('test-user', $player->username, 'Username is the one I defined.');
80
    }
81
82 View Code Duplication
    public function testCreatePlayerWithMissingArgumentReturnsBadRequest()
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...
83
    {
84
        $client = $this->createClient();
85
86
        $client->request('POST', '/api/players', array(
87
            'username' => '',
88
            'password' => 'test-pass',
89
        ));
90
91
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
92
93
        $client->request('POST', '/api/players', array(
94
            'username' => 'test-user',
95
            'password' => '',
96
        ));
97
98
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
99
    }
100
101 View Code Duplication
    public function testCreatePlayerTwiceReturnsConflictStatusCode()
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...
102
    {
103
        $client = $this->createClient();
104
105
        $client->request('POST', '/api/players', array(
106
            'username' => 'test-user',
107
            'password' => 'test-pass',
108
        ));
109
110
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
111
112
        $client->request('POST', '/api/players', array(
113
            'username' => 'test-user',
114
            'password' => 'test-pass',
115
        ));
116
117
        $this->assertEquals(Response::HTTP_CONFLICT, $client->getResponse()->getStatusCode());
118
    }
119
120 View Code Duplication
    public function testCreateGuestReturnsAGuest()
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...
121
    {
122
        $client = $this->createClient();
123
124
        $client->request('POST', '/api/players/guest');
125
126
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
127
128
        $guest = json_decode($client->getResponse()->getContent());
129
130
        $this->assertObjectHasAttribute('id', $guest);
131
        $this->assertObjectHasAttribute('username', $guest);
132
        $this->assertObjectHasAttribute('guest', $guest);
133
134
        $this->assertTrue($guest->guest, 'Guest field is set to true.');
135
136
        $client->request('GET', '/api/players/'.$guest->username);
137
138
        $guestRetrieved = json_decode($client->getResponse()->getContent());
139
140
        $this->assertEquals($guestRetrieved, $guest);
141
    }
142
143
    public function testCreateGuestGenerateDifferentGuestWhenCalledMultipleTimes()
144
    {
145
        $client = $this->createClient();
146
        $guests = array();
147
148
        $client->request('POST', '/api/players/guest');
149
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode(), 'Creating first guest.');
150
        $guests []= json_decode($client->getResponse()->getContent());
151
152
        $client->request('POST', '/api/players/guest');
153
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode(), 'Creating second guest.');
154
        $guests []= json_decode($client->getResponse()->getContent());
155
156
        $this->assertNotEquals($guests[0]->id, $guests[1]->id, 'Comparing guests ids.');
157
        $this->assertNotEquals($guests[0]->username, $guests[1]->username, 'Comparing guests usernames.');
158
    }
159
160 View Code Duplication
    public function testCreateGuestUseProvidedPasswordIfAny()
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...
161
    {
162
        $client = $this->createClient();
163
164
        $client->request('POST', '/api/players/guest', array(
165
            'provided-password',
166
        ));
167
168
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
169
170
        $guest = json_decode($client->getResponse()->getContent());
171
172
        $this->assertObjectHasAttribute('id', $guest);
173
        $this->assertObjectHasAttribute('username', $guest);
174
        $this->assertObjectHasAttribute('guest', $guest);
175
176
        $this->assertTrue($guest->guest, 'Guest field is set to true.');
177
178
        $client->request('GET', '/api/players/'.$guest->username);
179
180
        $guestRetrieved = json_decode($client->getResponse()->getContent());
181
182
        $this->assertEquals($guestRetrieved, $guest);
183
184
        // Todo check whether password is provided password
185
    }
186
187
    public function testAuthMeCanAuthenticateCreatedGuest()
188
    {
189
        $client = $this->createClient();
190
191
        $client->request('POST', '/api/players/guest');
192
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode(), 'Creating first guest.');
193
        $guest = json_decode($client->getResponse()->getContent());
194
195
        $client->request('GET', '/api/auth/me', [], [], array(
196
            'HTTP_AUTHORIZATION' => self::createOAuth2Token($guest->username),
197
        ));
198
199
        $this->assertTrue($client->getResponse()->isSuccessful());
200
201
        $authenticatedGuest = json_decode($client->getResponse()->getContent());
202
203
        $this->assertEquals($guest->username, $authenticatedGuest->username, 'Authenticated guest is the one I created.');
204
    }
205
206
    public function testPlayersCountReturnsExpectedValues()
207
    {
208
        $client = $this->createClient();
209
210
        $client->request('GET', '/api/players-count');
211
        $this->assertTrue($client->getResponse()->isSuccessful(), 'Players count returns successful response.');
212
213
        $initialCount = json_decode($client->getResponse()->getContent());
214
215
        $client->request('POST', '/api/players/guest');
216
        $client->request('POST', '/api/players/guest');
217
218
        $client->request('GET', '/api/players-count');
219
220
        $reponse = json_decode($client->getResponse()->getContent());
221
222
        $this->assertTrue($client->getResponse()->isSuccessful(), 'Players count returns successful response.');
223
        $this->assertEquals($initialCount + 2, $reponse, 'When I create 2 new players, players count is incremented by 2.');
224
    }
225
226
    public function testDeletePlayer()
227
    {
228
        $client = $this->createClient();
229
230
        $client->request('DELETE', '/api/players/existing-player');
231
        $this->assertTrue($client->getResponse()->isSuccessful(), 'Delete user returns successful response.');
232
233
        $client->request('GET', '/api/players/existing-player');
234
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode(), 'Retrieving a deleted user returns 404.');
235
236
        $client->request('DELETE', '/api/players/existing-player');
237
        $this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode(), 'Delete an inexisting user returns 404.');
238
    }
239
240
    public function testRegisterGuest()
241
    {
242
        $client = $this->createClient();
243
244
        $client->request('POST', '/api/players/guest');
245
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
246
247
        $guest = json_decode($client->getResponse()->getContent());
248
249
        $client->request('POST', '/api/players/register', array(
250
            'username' => 'Killer60',
251
            'password' => 'myPassword'
252
        ), [], array(
253
            'HTTP_AUTHORIZATION' => self::createOAuth2Token($guest->username),
254
        ));
255
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
256
257
        $player = json_decode($client->getResponse()->getContent());
258
259
        $this->assertEquals($guest->id, $player->id, 'Registered player keep the same id of the guest.');
260
        $this->assertFalse($player->guest, 'Registered player is no longer a guest.');
261
        $this->assertEquals('Killer60', $player->username, 'Player username is updated to the one requested.');
262
    }
263
264
    public function testRegisterGuestNeedsToBeLogged()
265
    {
266
        $client = $this->createClient();
267
268
        $client->request('POST', '/api/players/guest');
269
270
        $client->request('POST', '/api/players/register', array(
271
            'username' => 'Killer60',
272
        ));
273
274
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $client->getResponse()->getStatusCode());
275
    }
276
277
    public function testRegisterGuestFailsOnAlreadyPlayer()
278
    {
279
        $client = $this->createClient();
280
281
        $client->request('POST', '/api/players/register', array(
282
            'username' => 'Killer60',
283
            'password' => 'myPassword'
284
        ), [], array(
285
            'HTTP_AUTHORIZATION' => self::createOAuth2Token('existing-player'),
286
        ));
287
288
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
289
    }
290
291
    public function testGetGames()
292
    {
293
        $client = $this->createClient();
294
295
        $client->request('GET', '/api/games');
296
297
        $this->assertTrue($client->getResponse()->isSuccessful(), 'Response is successful');
298
299
        $games = json_decode($client->getResponse()->getContent());
300
301
        $this->assertCount(2, $games);
302
303
        $this->assertObjectHasAttribute('id', $games[0]);
304
        $this->assertObjectHasAttribute('name', $games[0]);
305
        $this->assertObjectHasAttribute('id', $games[1]);
306
        $this->assertObjectHasAttribute('name', $games[1]);
307
    }
308
309
    public function testGetGameByNameReturnsExpectedGame()
310
    {
311
        $client = $this->createClient();
312
313
        $client->request('GET', '/api/games/game-0');
314
315
        $this->assertTrue($client->getResponse()->isSuccessful(), 'Response is successful');
316
317
        $game = json_decode($client->getResponse()->getContent());
318
319
        $this->assertObjectHasAttribute('id', $game);
320
        $this->assertObjectHasAttribute('name', $game);
321
        $this->assertEquals('game-0', $game->name);
322
    }
323
}
324