Completed
Push — master ( 4bb011...4b99be )
by Guillaume
13:23
created

EventControllerTest::testPostValide()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\CampaignBundle\Tests\Controller;
4
5
use Starkerxp\StructureBundle\Tests\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
            "campaign" => $this->getCampaignId(),
26
            "template" => $this->getTemplateId(),
27
        ];
28
        $url = $this->generateUrl(
29
            'starkerxp_campaign.event.post',
30
            [
31
                "campaign_id" => $this->getCampaignId(),
32
            ]
33
        );
34
        $client = $this->getAuthClient();
35
        $client->request('POST', $url, $data);
36
        $response = $client->getResponse();
37
38
        $this->assertEquals(201, $response->getStatusCode());
39
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
40
        $events = $manager->findAll();
41
        $this->assertCount(1, $events);
42
    }
43
44
    protected function getCampaignId()
45
    {
46
        $repositoryCampaign = $this->getEntityManager()->getRepository("StarkerxpCampaignBundle:Campaign");
47
        $resultats = $repositoryCampaign->findAll();
48
49
        return $resultats[0]->getId();
50
    }
51
52
    protected function getTemplateId()
53
    {
54
        $repositoryTemplate = $this->getEntityManager()->getRepository("StarkerxpCampaignBundle:Template");
55
        $resultats = $repositoryTemplate->findAll();
56
57
        return $resultats[0]->getId();
58
    }
59
60
    /**
61
     * @group event
62
     * @group post
63
     * @group controller
64
     */
65 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...
66
    {
67
        $this->loadFixtureFiles(
68
            [
69
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
70
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
71
            ]
72
        );
73
        $url = $this->generateUrl(
74
            'starkerxp_campaign.event.post',
75
            [
76
                "campaign_id" => $this->getCampaignId(),
77
            ]
78
        );
79
        $client = $this->getAuthClient();
80
        $client->request('POST', $url, []);
81
        $response = $client->getResponse();
82
        $this->assertEquals(400, $response->getStatusCode());
83
        $body = json_decode($response->getContent(), true)['payload'];
0 ignored issues
show
Unused Code introduced by
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        //$this->assertArrayHasKey("nom", $body); // Exemple
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
    }
86
87
    /**
88
     * @group event
89
     * @group put
90
     * @group controller
91
     */
92
    public function testPutValide()
93
    {
94
        $this->loadFixtureFiles(
95
            [
96
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
97
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
98
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
99
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
100
            ]
101
        );
102
        $templates = $this->getRepository("StarkerxpCampaignBundle:Template")->findBy([], ['id' => 'ASC']);
103
104
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
105
        $listeEvents = $manager->getRepository()->findAll();
106
        $this->assertCount(1, $listeEvents);
107
        $eventDepart = $manager->toArray($listeEvents[0], ['campaign_id', 'template_id']);
108
        $data = [
109
            "campaign" => $this->getCampaignId(),
110
            "template" => $templates[1]->getId(),
111
        ];
112
        $url = $this->generateUrl(
113
            'starkerxp_campaign.event.put',
114
            [
115
                "campaign_id" => $this->getCampaignId(),
116
                "id"          => $listeEvents[0]->getId(),
117
            ]
118
        );
119
        $client = $this->getAuthClient();
120
        $client->request('PUT', $url, $data);
121
        $response = $client->getResponse();
122
        $this->assertEquals(204, $response->getStatusCode());
123
        $manager->clear();
124
        $events = $manager->findAll();
125
        $this->assertCount(1, $events);
126
        $eventFinal = $manager->toArray($events[0], ['campaign_id', 'template_id']);
127
        $this->assertNotEquals($eventDepart, $eventFinal);
128
        $this->assertEquals(array_values($data), array_values($eventFinal));
129
    }
130
131
    /**
132
     * @group event
133
     * @group put
134
     * @group controller
135
     */
136
    public function testPutInvalide()
137
    {
138
        $this->loadFixtureFiles(
139
            [
140
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
141
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
142
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
143
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
144
            ]
145
        );
146
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
147
        $listeEvents = $manager->getRepository()->findAll();
148
        $this->assertCount(1, $listeEvents);
149
        $url = $this->generateUrl(
150
            'starkerxp_campaign.event.put',
151
            [
152
                "campaign_id" => $this->getCampaignId(),
153
                "id"          => $listeEvents[0]->getId(),
154
            ]
155
        );
156
        $client = $this->getAuthClient();
157
        $client->request('PUT', $url, []);
158
        $response = $client->getResponse();
159
        $this->assertEquals(400, $response->getStatusCode());
160
        $body = json_decode($response->getContent(), true)['payload'];
0 ignored issues
show
Unused Code introduced by
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
        //$this->assertArrayHasKey("nom", $body); // Exemple
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
163
    }
164
165
    /**
166
     * @group event
167
     * @group put
168
     * @group controller
169
     */
170 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...
171
    {
172
        $this->loadFixtureFiles(
173
            [
174
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
175
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
176
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
177
            ]
178
        );
179
        $data = [
180
            //'nom'     => "Mon nom", //exemple
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
181
        ];
182
        $url = $this->generateUrl(
183
            'starkerxp_campaign.event.put',
184
            [
185
                "campaign_id" => $this->getCampaignId(),
186
                "id"          => 404,
187
            ]
188
        );
189
        $client = $this->getAuthClient();
190
        $client->request('PUT', $url, $data);
191
        $response = $client->getResponse();
192
        $this->assertEquals(404, $response->getStatusCode());
193
        $body = json_decode($response->getContent(), true);
194
        $this->assertNotEmpty($body);
195
    }
196
197
    /**
198
     * @group event
199
     * @group cget
200
     * @group controller
201
     */
202 View Code Duplication
    public function testCGetValideAvecResultats()
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...
203
    {
204
        $this->loadFixtureFiles(
205
            [
206
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
207
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
208
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
209
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/EventManager.yml',
210
            ]
211
        );
212
        $url = $this->generateUrl(
213
            'starkerxp_campaign.event.cget',
214
            [
215
                "campaign_id" => $this->getCampaignId(),
216
            ]
217
        );
218
        $client = $this->getAuthClient();
219
        $client->request('GET', $url, []);
220
        $response = $client->getResponse();
221
        $this->assertEquals(200, $response->getStatusCode());
222
        $body = json_decode($response->getContent(), true);
223
        $this->assertCount(10, $body);
224
        foreach ($body as $element) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
225
            //$this->assertArrayHasKey("nom", $body); // Exemple
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
226
        }
227
    }
228
229
    /**
230
     * @group event
231
     * @group cget
232
     * @group controller
233
     */
234 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...
235
    {
236
        $this->loadFixtureFiles(
237
            [
238
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
239
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
240
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
241
            ]
242
        );
243
        $url = $this->generateUrl(
244
            'starkerxp_campaign.event.cget',
245
            [
246
                "campaign_id" => $this->getCampaignId(),
247
            ]
248
        );
249
        $client = $this->getAuthClient();
250
        $client->request('GET', $url, []);
251
        $response = $client->getResponse();
252
        $this->assertEquals(200, $response->getStatusCode());
253
        $body = json_decode($response->getContent(), true);
254
        $this->assertCount(0, $body);
255
    }
256
257
    /**
258
     * @group event
259
     * @group cget
260
     * @group controller
261
     */
262 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...
263
    {
264
        $this->loadFixtureFiles(
265
            [
266
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
267
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
268
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
269
            ]
270
        );
271
        $url = $this->generateUrl(
272
            'starkerxp_campaign.event.cget',
273
            [
274
                "campaign_id" => $this->getCampaignId(),
275
            ]
276
        );
277
        $client = $this->getAuthClient();
278
        $client->request('GET', $url, ["filter_erreur" => "+h"]);
279
        $response = $client->getResponse();
280
        $this->assertEquals(400, $response->getStatusCode());
281
    }
282
283
    /**
284
     * @group event
285
     * @group get
286
     * @group controller
287
     */
288
    public function testGetValideAvecResultats()
289
    {
290
        $this->loadFixtureFiles(
291
            [
292
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
293
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
294
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
295
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/EventManager.yml',
296
            ]
297
        );
298
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
299
        $listeEvents = $manager->getRepository()->findAll();
300
        $this->assertCount(10, $listeEvents);
301
        $url = $this->generateUrl(
302
            'starkerxp_campaign.event.get',
303
            [
304
                "campaign_id" => $this->getCampaignId(),
305
                "id"          => $listeEvents[0]->getId(),
306
            ]
307
        );
308
        $client = $this->getAuthClient();
309
        $client->request('GET', $url, []);
310
        $response = $client->getResponse();
311
        $this->assertEquals(200, $response->getStatusCode());
312
        $body = json_decode($response->getContent(), true);
313
        $this->assertCount(3, $body);
314
        $this->assertArrayHasKey("id", $body); // Exemple
315
        $this->assertArrayHasKey("campaign_id", $body); // Exemple
316
    }
317
318
    /**
319
     * @group event
320
     * @group get
321
     * @group controller
322
     */
323 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...
324
    {
325
        $this->loadFixtureFiles(
326
            [
327
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
328
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
329
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
330
            ]
331
        );
332
        $url = $this->generateUrl(
333
            'starkerxp_campaign.event.get',
334
            [
335
                "campaign_id" => $this->getCampaignId(),
336
                "id"          => 404,
337
            ]
338
        );
339
        $client = $this->getAuthClient();
340
        $client->request('GET', $url, []);
341
        $response = $client->getResponse();
342
        $this->assertEquals(404, $response->getStatusCode());
343
        $body = json_decode($response->getContent(), true);
344
        $this->assertNotEmpty($body);
345
    }
346
347
    /**
348
     * @group event
349
     * @group get
350
     * @group controller
351
     */
352 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...
353
    {
354
        $this->loadFixtureFiles(
355
            [
356
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
357
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
358
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
359
            ]
360
        );
361
        $url = $this->generateUrl(
362
            'starkerxp_campaign.event.get',
363
            [
364
                "campaign_id" => $this->getCampaignId(),
365
                "id"          => 500,
366
            ]
367
        );
368
        $client = $this->getAuthClient();
369
        $client->request('GET', $url, ["filter_erreur" => "+h"]);
370
        $response = $client->getResponse();
371
        $this->assertEquals(400, $response->getStatusCode());
372
    }
373
374
    /**
375
     * @group event
376
     * @group delete
377
     * @group controller
378
     */
379
    public function testDeleteValide()
380
    {
381
382
        $this->loadFixtureFiles(
383
            [
384
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
385
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
386
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
387
                '@StarkerxpCampaignBundle/Tests/DataFixtures/EventManager/DefaultEvent.yml',
388
            ]
389
        );
390
        $manager = $this->getContainer()->get('starkerxp_campaign.manager.event');
391
        $listeEvents = $manager->getRepository()->findAll();
392
        $this->assertCount(1, $listeEvents);
393
        $url = $this->generateUrl(
394
            'starkerxp_campaign.event.delete',
395
            [
396
                "campaign_id" => $this->getCampaignId(),
397
                "id"          => $listeEvents[0]->getId(),
398
            ]
399
        );
400
        $client = $this->getAuthClient();
401
        $client->request('DELETE', $url);
402
        $response = $client->getResponse();
403
        $this->assertEquals(204, $response->getStatusCode());
404
        $manager->clear();
405
        $events = $manager->findAll();
406
        $this->assertCount(0, $events);
407
    }
408
409
    /**
410
     * @group event
411
     * @group delete
412
     * @group controller
413
     */
414 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...
415
    {
416
        $this->loadFixtureFiles(
417
            [
418
                '@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',
419
                '@StarkerxpCampaignBundle/Tests/DataFixtures/TemplateManager/TemplateManager.yml',
420
                '@StarkerxpCampaignBundle/Tests/DataFixtures/CampaignManager/DefaultCampaign.yml',
421
            ]
422
        );
423
        $url = $this->generateUrl(
424
            'starkerxp_campaign.event.delete',
425
            [
426
                "campaign_id" => $this->getCampaignId(),
427
                "id"          => 404,
428
            ]
429
        );
430
        $client = $this->getAuthClient();
431
        $client->request('DELETE', $url, []);
432
        $response = $client->getResponse();
433
        $this->assertEquals(404, $response->getStatusCode());
434
        $body = json_decode($response->getContent(), true);
435
        $this->assertNotEmpty($body);
436
    }
437
}
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...
438