Completed
Push — master ( cd8c72...c8cad1 )
by Guillaume
03:42
created

CampagneControllerTest::testPutValide()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\CampagneBundle\Tests\Controller;
4
5
use Starkerxp\StructureBundle\Tests\WebTest;
6
7
class CampagneControllerTest extends WebTest
8
{
9
10
    /**
11
     * @group campagne
12
     * @group post
13
     * @group controller
14
     */
15
    public function testPostValide()
16
    {
17
        $this->loadFixtureFiles(['@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',]);
18
        $data = [
19
            'name' => "Ma première campagne",
20
        ];
21
        $client = $this->getAuthClient();
22
        $client->request('POST', '/api/campagnes', $data);
23
        $response = $client->getResponse();
24
        $this->assertEquals(201, $response->getStatusCode());
25
        $manager = $this->getContainer()->get('starkerxp_campagne.manager.campagne');
26
        $campagnes = $manager->findAll();
27
        $this->assertCount(1, $campagnes);
28
    }
29
30
    /**
31
     * @group campagne
32
     * @group post
33
     * @group controller
34
     */
35 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...
36
    {
37
        $this->loadFixtureFiles(['@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',]);
38
        $client = $this->getAuthClient();
39
        $client->request('POST', '/api/campagnes', []);
40
        $response = $client->getResponse();
41
        $this->assertEquals(400, $response->getStatusCode());
42
        $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...
43
        //$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...
44
    }
45
46
47
    /**
48
     * @group campagne
49
     * @group put
50
     * @group controller
51
     */
52
    public function testPutValide()
53
    {
54
        $this->loadFixtureFiles(
55
            [
56
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
57
                '@StarkerxpCampagneBundle/Tests/DataFixtures/CampagneManager/DefaultCampagne.yml',
58
            ]
59
        );
60
        $manager = $this->getContainer()->get('starkerxp_campagne.manager.campagne');
61
        $listeCampagnes = $manager->getRepository()->findAll();
62
        $this->assertCount(1, $listeCampagnes);
63
        $campagneDepart = $manager->toArray($listeCampagnes[0], ['name']);// Exemple
64
        $data = [
65
            'name'     => "Mon nom", //exemple
66
        ];
67
        $client = $this->getAuthClient();
68
        $client->request('PUT', '/api/campagnes/'.$listeCampagnes[0]->getId(), $data);
69
        $response = $client->getResponse();
70
        $this->assertEquals(204, $response->getStatusCode());
71
        $manager->clear();
72
        $campagnes = $manager->findAll();
73
        $this->assertCount(1, $campagnes);
74
        $campagneFinal = $manager->toArray($campagnes[0], ['name']);// Exemple
75
        $this->assertNotEquals($campagneDepart, $campagneFinal);
76
        $this->assertEquals($data, $campagneFinal);
77
    }
78
79
    /**
80
     * @group campagne
81
     * @group put
82
     * @group controller
83
     */
84
    public function testPutInvalide()
85
    {
86
        $this->loadFixtureFiles(
87
            [
88
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
89
                '@StarkerxpCampagneBundle/Tests/DataFixtures/CampagneManager/DefaultCampagne.yml',
90
            ]
91
        );
92
        $manager = $this->getContainer()->get('starkerxp_campagne.manager.campagne');
93
        $listeCampagnes = $manager->getRepository()->findAll();
94
        $this->assertCount(1, $listeCampagnes);
95
        $client = $this->getAuthClient();
96
        $client->request('PUT', '/api/campagnes/'.$listeCampagnes[0]->getId(), []);
97
        $response = $client->getResponse();
98
        $this->assertEquals(400, $response->getStatusCode());
99
        $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...
100
        //$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...
101
102
    }
103
104
    /**
105
     * @group campagne
106
     * @group put
107
     * @group controller
108
     */
109 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...
110
    {
111
        $this->loadFixtureFiles(
112
            [
113
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
114
            ]
115
        );
116
        $data = [
117
            'name'     => "Mon nom", //exemple
118
        ];
119
        $client = $this->getAuthClient();
120
        $client->request('PUT', '/api/campagnes/404', $data);
121
        $response = $client->getResponse();
122
        $this->assertEquals(404, $response->getStatusCode());
123
        $body = json_decode($response->getContent(), true);
124
        $this->assertNotEmpty($body);
125
    }
126
127
128
    /**
129
     * @group campagne
130
     * @group cget
131
     * @group controller
132
     */
133 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...
134
    {
135
        $this->loadFixtureFiles(
136
            [
137
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
138
                '@StarkerxpCampagneBundle/Tests/DataFixtures/CampagneManager/CampagneManager.yml',
139
            ]
140
        );
141
        $client = $this->getAuthClient();
142
        $client->request('GET', '/api/campagnes', []);
143
        $response = $client->getResponse();
144
        $this->assertEquals(200, $response->getStatusCode());
145
        $body = json_decode($response->getContent(), true);
146
        $this->assertCount(10, $body);
147
        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...
148
            //$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...
149
        }
150
    }
151
152
    /**
153
     * @group campagne
154
     * @group cget
155
     * @group controller
156
     */
157 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...
158
    {
159
        $this->loadFixtureFiles(
160
            [
161
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
162
            ]
163
        );
164
        $client = $this->getAuthClient();
165
        $client->request('GET', '/api/campagnes', []);
166
        $response = $client->getResponse();
167
        $this->assertEquals(200, $response->getStatusCode());
168
        $body = json_decode($response->getContent(), true);
169
        $this->assertCount(0, $body);
170
    }
171
172
    /**
173
     * @group campagne
174
     * @group cget
175
     * @group controller
176
     */
177
    public function testCGetInvalide()
178
    {
179
        $this->loadFixtureFiles(
180
            [
181
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
182
            ]
183
        );
184
        $client = $this->getAuthClient();
185
        $client->request('GET', '/api/campagnes', ["filter_erreur" => "+h"]);
186
        $response = $client->getResponse();
187
        $this->assertEquals(400, $response->getStatusCode());
188
    }
189
190
    /**
191
     * @group campagne
192
     * @group get
193
     * @group controller
194
     */
195
    public function testGetValideAvecResultats()
196
    {
197
        $this->loadFixtureFiles(
198
            [
199
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
200
                '@StarkerxpCampagneBundle/Tests/DataFixtures/CampagneManager/CampagneManager.yml',
201
            ]
202
        );
203
        $manager = $this->getContainer()->get('starkerxp_campagne.manager.campagne');
204
        $listeCampagnes = $manager->getRepository()->findAll();
205
        $this->assertCount(10, $listeCampagnes);
206
        $client = $this->getAuthClient();
207
        $client->request('GET', '/api/campagnes/'.$listeCampagnes[0]->getId(), []);
208
        $response = $client->getResponse();
209
        $this->assertEquals(200, $response->getStatusCode());
210
        $body = json_decode($response->getContent(), true);
211
        $this->assertCount(3, $body);
212
        $this->assertArrayHasKey("name", $body); // Exemple
213
        $this->assertArrayHasKey("status", $body); // Exemple
214
        $this->assertArrayHasKey("id", $body); // Exemple
215
    }
216
217
    /**
218
     * @group campagne
219
     * @group get
220
     * @group controller
221
     */
222 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...
223
    {
224
        $this->loadFixtureFiles(
225
            [
226
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
227
            ]
228
        );
229
        $client = $this->getAuthClient();
230
        $client->request('GET', '/api/campagnes/404', []);
231
        $response = $client->getResponse();
232
        $this->assertEquals(404, $response->getStatusCode());
233
        $body = json_decode($response->getContent(), true);
234
        $this->assertNotEmpty($body);
235
    }
236
237
    /**
238
     * @group campagne
239
     * @group get
240
     * @group controller
241
     */
242 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...
243
    {
244
        $this->loadFixtureFiles(
245
            [
246
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
247
            ]
248
        );
249
        $client = $this->getAuthClient();
250
        $client->request('GET', '/api/campagnes/500', ["filter_erreur" => "+h"]);
251
        $response = $client->getResponse();
252
        $this->assertEquals(400, $response->getStatusCode());
253
    }
254
255
    /**
256
     * @group campagne
257
     * @group delete
258
     * @group controller
259
     */
260 View Code Duplication
    public function testDeleteValide()
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...
261
    {
262
        $this->loadFixtureFiles(
263
            [
264
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
265
                '@StarkerxpCampagneBundle/Tests/DataFixtures/CampagneManager/DefaultCampagne.yml',
266
            ]
267
        );
268
        $manager = $this->getContainer()->get('starkerxp_campagne.manager.campagne');
269
        $listeCampagnes = $manager->getRepository()->findAll();
270
        $this->assertCount(1, $listeCampagnes);
271
        $client = $this->getAuthClient();
272
        $client->request('DELETE', '/api/campagnes/'.$listeCampagnes[0]->getId());
273
        $response = $client->getResponse();
274
        $this->assertEquals(204, $response->getStatusCode());
275
        $manager->clear();
276
        $campagnes = $manager->findAll();
277
        $this->assertCount(0, $campagnes);
278
    }
279
280
    /**
281
     * @group campagne
282
     * @group delete
283
     * @group controller
284
     */
285 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...
286
    {
287
        $this->loadFixtureFiles(
288
            [
289
                '@StarkerxpUtilisateurBundle/Tests/DataFixtures/UtilisateurManager/DefaultUtilisateur.yml',
290
            ]
291
        );
292
        $client = $this->getAuthClient();
293
        $client->request('DELETE', '/api/campagnes/404', []);
294
        $response = $client->getResponse();
295
        $this->assertEquals(404, $response->getStatusCode());
296
        $body = json_decode($response->getContent(), true);
297
        $this->assertNotEmpty($body);
298
    }
299
}
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...
300