NewsitemsControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 12.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 10
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPost() 0 23 1
A testGet() 0 14 1
A testPatch() 0 26 1
A testDelete() 10 10 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 Tests\KI\PublicationBundle\Controller;
4
5
use Tests\KI\CoreBundle\WebTestCase;
6
7
class NewsitemsControllerTest extends WebTestCase
8
{
9
    // On crée une ressource sur laquelle seront effectués les tests.
10
    // Ne pas oublier de supprimer à la fin avec le test DELETE.
11
    public function testPost()
12
    {
13
        $this->client->request('POST', '/newsitems', [
14
            'name' => 'La Porte',
15
            'text' => 'C\'est comme perdre',
16
            'sendMail' => true,
17
            'authorClub' => 'ki'
18
        ]);
19
        $response = $this->client->getResponse();
20
        $this->assertJsonResponse($response, 201);
21
22
        // On vérifie qu'un mail a été envoyé
23
        $mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
24
        $this->assertEquals(2, $mailCollector->getMessageCount());
0 ignored issues
show
Bug introduced by
The method getMessageCount() does not seem to exist on object<Symfony\Component...DataCollectorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        $collectedMessages = $mailCollector->getMessages();
27
        $message = $collectedMessages[0];
28
29
        // On vérifie le message
30
        $this->assertInstanceOf('Swift_Message', $message);
31
        $this->assertTrue(!empty($message->getSubject()));
32
        $this->assertEquals('[email protected]', key($message->getFrom()));
33
    }
34
35
    public function testGet()
36
    {
37
        $this->client->request('GET', '/newsitems');
38
        $response = $this->client->getResponse();
39
        $this->assertJsonResponse($response, 200);
40
41
        $this->client->request('GET', '/newsitems/la-porte');
42
        $response = $this->client->getResponse();
43
        $this->assertJsonResponse($response, 200);
44
45
        $this->client->request('GET', '/newsitems/sjoajsiohaysahais-asbsksaba7');
46
        $response = $this->client->getResponse();
47
        $this->assertJsonResponse($response, 404);
48
    }
49
50
    public function testPatch()
51
    {
52
        $this->client->request(
53
            'PATCH',
54
            '/newsitems/la-porte',
55
            ['text' => 'ddssqdqsd', 'sendMail' => false]
56
        );
57
        $response = $this->client->getResponse();
58
        $this->assertJsonResponse($response, 204);
59
60
        $this->client->request(
61
            'PATCH',
62
            '/newsitems/la-porte',
63
            ['text' => '']
64
        );
65
        $response = $this->client->getResponse();
66
        $this->assertJsonResponse($response, 400);
67
68
        $this->client->request(
69
            'PATCH',
70
            '/newsitems/sjoajsiohaysahais-asbsksaba7',
71
            ['username' => 'miam', 'email' => '[email protected]']
72
        );
73
        $response = $this->client->getResponse();
74
        $this->assertJsonResponse($response, 404);
75
    }
76
77 View Code Duplication
    public function testDelete()
78
    {
79
        $this->client->request('DELETE', '/newsitems/la-porte');
80
        $response = $this->client->getResponse();
81
        $this->assertJsonResponse($response, 204);
82
83
        $this->client->request('DELETE', '/newsitems/la-porte');
84
        $response = $this->client->getResponse();
85
        $this->assertJsonResponse($response, 404);
86
    }
87
}
88