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

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