Completed
Push — master ( e4a474...3a3d8d )
by Guillaume
02:43
created

EventControllerTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 440
Duplicated Lines 36.36 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 160
loc 440
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B testPostValide() 0 26 1
A getTemplateId() 0 7 1
A getCampaignId() 0 7 1
B testPostInvalide() 25 25 1
B testPutValide() 0 38 1
B testPutInvalide() 0 31 1
B testPutSansResultat() 26 26 1
B testCGetValideAvecResultats() 0 29 2
A testCGetValideSansResultat() 22 22 1
A testCGetInvalide() 20 20 1
B testGetValideAvecResultats() 0 30 1
A testGetValideSansResultat() 23 23 1
A testGetInvalide() 21 21 1
B testDeleteValide() 0 29 1
A testDeleteSansResultat() 23 23 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 Starkerxp\CampaignBundle\Tests\Controller;
4
5
use Starkerxp\StructureBundle\Test\WebTest;
6
7
class EventControllerTest extends WebTest
8
{
9
10
    /**
11
     * @group event
12
     * @group post
13
     * @group controller
14
     */
15
    public function testPostValide()
16
    {
17
        $this->loadFixtureFiles(
18
            [
19
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
20
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
21
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
22
            ]
23
        );
24
        $data = [
25
            "template" => $this->getTemplateId(),
26
        ];
27
        $url = $this->generateUrl(
28
            'starkerxp_campaign.event.post',
29
            [
30
                "campaign_id" => $this->getCampaignId(),
31
            ]
32
        );
33
        $client = $this->getAuthClient();
34
        $client->request('POST', $url, $data);
35
        $response = $client->getResponse();
36
        $this->assertEquals(201, $response->getStatusCode());
37
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
38
        $events = $manager->findAll();
39
        $this->assertCount(1, $events);
40
    }
41
42
    protected function getTemplateId()
43
    {
44
        $repositoryTemplate = $this->getEntityManager()->getRepository("StarkerxpCampaignBundle:Template");
45
        $resultats = $repositoryTemplate->findAll();
46
47
        return $resultats[0]->getId();
48
    }
49
50
    protected function getCampaignId()
51
    {
52
        $repositoryCampaign = $this->getEntityManager()->getRepository("StarkerxpCampaignBundle:Campaign");
53
        $resultats = $repositoryCampaign->findAll();
54
55
        return $resultats[0]->getId();
56
    }
57
58
    /**
59
     * @group event
60
     * @group post
61
     * @group controller
62
     */
63 View Code Duplication
    public function testPostInvalide()
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...
64
    {
65
        $this->loadFixtureFiles(
66
            [
67
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
68
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
69
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
70
            ]
71
        );
72
        $url = $this->generateUrl(
73
            'starkerxp_campaign.event.post',
74
            [
75
                "campaign_id" => $this->getCampaignId(),
76
            ]
77
        );
78
        $data = [
79
            "template" => $this->getTemplateId() + 10,
80
        ];
81
        $client = $this->getAuthClient();
82
        $client->request('POST', $url, $data);
83
        $response = $client->getResponse();
84
        $this->assertEquals(400, $response->getStatusCode());
85
        $body = json_decode($response->getContent(), true)['payload'];
86
        $this->assertArrayHasKey("template", $body);
87
    }
88
89
    /**
90
     * @group event
91
     * @group put
92
     * @group controller
93
     */
94
    public function testPutValide()
95
    {
96
        $this->loadFixtureFiles(
97
            [
98
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
99
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
100
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
101
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
102
            ]
103
        );
104
        $templates = $this->getRepository("StarkerxpCampaignBundle:Template")->findBy([], ['id' => 'ASC']);
105
106
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
107
        $listeEvents = $manager->getRepository()->findAll();
108
        $this->assertCount(1, $listeEvents);
109
        $eventDepart = $manager->toArray($listeEvents[0], ['campaign_id', 'template_id']);
110
        $data = [
111
            "campaign" => $this->getCampaignId(),
112
            "template" => $templates[1]->getId(),
113
        ];
114
        $url = $this->generateUrl(
115
            'starkerxp_campaign.event.put',
116
            [
117
                "campaign_id" => $this->getCampaignId(),
118
                "event_id" => $listeEvents[0]->getId(),
119
            ]
120
        );
121
        $client = $this->getAuthClient();
122
        $client->request('PUT', $url, $data);
123
        $response = $client->getResponse();
124
        $this->assertEquals(204, $response->getStatusCode());
125
        $manager->clear();
126
        $events = $manager->findAll();
127
        $this->assertCount(1, $events);
128
        $eventFinal = $manager->toArray($events[0], ['campaign_id', 'template_id']);
129
        $this->assertNotEquals($eventDepart, $eventFinal);
130
        $this->assertEquals(array_values($data), array_values($eventFinal));
131
    }
132
133
    /**
134
     * @group event
135
     * @group put
136
     * @group controller
137
     */
138
    public function testPutInvalide()
139
    {
140
        $this->loadFixtureFiles(
141
            [
142
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
143
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
144
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
145
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
146
            ]
147
        );
148
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
149
        $listeEvents = $manager->getRepository()->findAll();
150
        $this->assertCount(1, $listeEvents);
151
        $url = $this->generateUrl(
152
            'starkerxp_campaign.event.put',
153
            [
154
                "campaign_id" => $this->getCampaignId(),
155
                "event_id" => $listeEvents[0]->getId(),
156
            ]
157
        );
158
        $data = [
159
            "template" => $this->getTemplateId() + 10,
160
        ];
161
        $client = $this->getAuthClient();
162
        $client->request('PUT', $url, $data);
163
        $response = $client->getResponse();
164
        $this->assertEquals(400, $response->getStatusCode());
165
        $body = json_decode($response->getContent(), true)['payload'];
166
        $this->assertArrayHasKey("template", $body);
167
168
    }
169
170
    /**
171
     * @group event
172
     * @group put
173
     * @group controller
174
     */
175 View Code Duplication
    public function testPutSansResultat()
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...
176
    {
177
        $this->loadFixtureFiles(
178
            [
179
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
180
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
181
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
182
            ]
183
        );
184
        $data = [
185
            'template'     => "Mon nom",
186
        ];
187
        $url = $this->generateUrl(
188
            'starkerxp_campaign.event.put',
189
            [
190
                "campaign_id" => $this->getCampaignId(),
191
                "event_id" => 404,
192
            ]
193
        );
194
        $client = $this->getAuthClient();
195
        $client->request('PUT', $url, $data);
196
        $response = $client->getResponse();
197
        $this->assertEquals(404, $response->getStatusCode());
198
        $body = json_decode($response->getContent(), true);
199
        $this->assertNotEmpty($body);
200
    }
201
202
    /**
203
     * @group event
204
     * @group cget
205
     * @group controller
206
     */
207
    public function testCGetValideAvecResultats()
208
    {
209
        $this->loadFixtureFiles(
210
            [
211
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
212
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
213
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
214
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/EventManager.yml',
215
            ]
216
        );
217
        $url = $this->generateUrl(
218
            'starkerxp_campaign.event.cget',
219
            [
220
                "campaign_id" => $this->getCampaignId(),
221
            ]
222
        );
223
        $client = $this->getAuthClient();
224
        $client->request('GET', $url, []);
225
        $response = $client->getResponse();
226
        $this->assertEquals(200, $response->getStatusCode());
227
        $body = json_decode($response->getContent(), true);
228
        $this->assertCount(10, $body);
229
        foreach ($body as $element) {
230
            $this->assertCount(3, $element);
231
            $this->assertArrayHasKey("id", $element);
232
            $this->assertArrayHasKey("campaign_id", $element);
233
            $this->assertArrayHasKey("campaign_id", $element);
234
        }
235
    }
236
237
    /**
238
     * @group event
239
     * @group cget
240
     * @group controller
241
     */
242 View Code Duplication
    public function testCGetValideSansResultat()
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...
243
    {
244
        $this->loadFixtureFiles(
245
            [
246
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
247
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
248
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
249
            ]
250
        );
251
        $url = $this->generateUrl(
252
            'starkerxp_campaign.event.cget',
253
            [
254
                "campaign_id" => $this->getCampaignId(),
255
            ]
256
        );
257
        $client = $this->getAuthClient();
258
        $client->request('GET', $url, []);
259
        $response = $client->getResponse();
260
        $this->assertEquals(200, $response->getStatusCode());
261
        $body = json_decode($response->getContent(), true);
262
        $this->assertCount(0, $body);
263
    }
264
265
    /**
266
     * @group event
267
     * @group cget
268
     * @group controller
269
     */
270 View Code Duplication
    public function testCGetInvalide()
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...
271
    {
272
        $this->loadFixtureFiles(
273
            [
274
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
275
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
276
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
277
            ]
278
        );
279
        $url = $this->generateUrl(
280
            'starkerxp_campaign.event.cget',
281
            [
282
                "campaign_id" => $this->getCampaignId(),
283
            ]
284
        );
285
        $client = $this->getAuthClient();
286
        $client->request('GET', $url, ["filter_erreur" => "+h"]);
287
        $response = $client->getResponse();
288
        $this->assertEquals(400, $response->getStatusCode());
289
    }
290
291
    /**
292
     * @group event
293
     * @group get
294
     * @group controller
295
     */
296
    public function testGetValideAvecResultats()
297
    {
298
        $this->loadFixtureFiles(
299
            [
300
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
301
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
302
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
303
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/EventManager.yml',
304
            ]
305
        );
306
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
307
        $listeEvents = $manager->getRepository()->findAll();
308
        $this->assertCount(10, $listeEvents);
309
        $url = $this->generateUrl(
310
            'starkerxp_campaign.event.get',
311
            [
312
                "campaign_id" => $this->getCampaignId(),
313
                "event_id" => $listeEvents[0]->getId(),
314
            ]
315
        );
316
        $client = $this->getAuthClient();
317
        $client->request('GET', $url, []);
318
        $response = $client->getResponse();
319
        $this->assertEquals(200, $response->getStatusCode());
320
        $body = json_decode($response->getContent(), true);
321
        $this->assertCount(3, $body);
322
        $this->assertArrayHasKey("id", $body);
323
        $this->assertArrayHasKey("campaign_id", $body);
324
        $this->assertArrayHasKey("template_id", $body);
325
    }
326
327
    /**
328
     * @group event
329
     * @group get
330
     * @group controller
331
     */
332 View Code Duplication
    public function testGetValideSansResultat()
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...
333
    {
334
        $this->loadFixtureFiles(
335
            [
336
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
337
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
338
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
339
            ]
340
        );
341
        $url = $this->generateUrl(
342
            'starkerxp_campaign.event.get',
343
            [
344
                "campaign_id" => $this->getCampaignId(),
345
                "event_id" => 404,
346
            ]
347
        );
348
        $client = $this->getAuthClient();
349
        $client->request('GET', $url, []);
350
        $response = $client->getResponse();
351
        $this->assertEquals(404, $response->getStatusCode());
352
        $body = json_decode($response->getContent(), true);
353
        $this->assertNotEmpty($body);
354
    }
355
356
    /**
357
     * @group event
358
     * @group get
359
     * @group controller
360
     */
361 View Code Duplication
    public function testGetInvalide()
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...
362
    {
363
        $this->loadFixtureFiles(
364
            [
365
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
366
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
367
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
368
            ]
369
        );
370
        $url = $this->generateUrl(
371
            'starkerxp_campaign.event.get',
372
            [
373
                "campaign_id" => $this->getCampaignId(),
374
                "event_id" => 500,
375
            ]
376
        );
377
        $client = $this->getAuthClient();
378
        $client->request('GET', $url, ["filter_erreur" => "+h"]);
379
        $response = $client->getResponse();
380
        $this->assertEquals(400, $response->getStatusCode());
381
    }
382
383
    /**
384
     * @group event
385
     * @group delete
386
     * @group controller
387
     */
388
    public function testDeleteValide()
389
    {
390
391
        $this->loadFixtureFiles(
392
            [
393
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
394
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
395
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
396
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
397
            ]
398
        );
399
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
400
        $listeEvents = $manager->getRepository()->findAll();
401
        $this->assertCount(1, $listeEvents);
402
        $url = $this->generateUrl(
403
            'starkerxp_campaign.event.delete',
404
            [
405
                "campaign_id" => $this->getCampaignId(),
406
                "event_id" => $listeEvents[0]->getId(),
407
            ]
408
        );
409
        $client = $this->getAuthClient();
410
        $client->request('DELETE', $url);
411
        $response = $client->getResponse();
412
        $this->assertEquals(204, $response->getStatusCode());
413
        $manager->clear();
414
        $events = $manager->findAll();
415
        $this->assertCount(0, $events);
416
    }
417
418
    /**
419
     * @group event
420
     * @group delete
421
     * @group controller
422
     */
423 View Code Duplication
    public function testDeleteSansResultat()
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...
424
    {
425
        $this->loadFixtureFiles(
426
            [
427
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
428
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
429
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
430
            ]
431
        );
432
        $url = $this->generateUrl(
433
            'starkerxp_campaign.event.delete',
434
            [
435
                "campaign_id" => $this->getCampaignId(),
436
                "event_id" => 404,
437
            ]
438
        );
439
        $client = $this->getAuthClient();
440
        $client->request('DELETE', $url, []);
441
        $response = $client->getResponse();
442
        $this->assertEquals(404, $response->getStatusCode());
443
        $body = json_decode($response->getContent(), true);
444
        $this->assertNotEmpty($body);
445
    }
446
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
447