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

TemplateControllerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 292
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 73
loc 292
rs 10
c 0
b 0
f 0

13 Methods

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