IncidentControllerTest::testCreateActionFail()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 25
loc 25
rs 8.8571
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace IncidentBundle\Tests\Controller;
4
5
use IncidentBundle\Entity\Incident;
6
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
8
/**
9
 * Class IncidentControllerTest
10
 */
11
class IncidentControllerTest extends WebTestCase
12
{
13
    const UPDATED_NAME = 'updated.name';
14
    const UPDATED_STATUS = 35;
15
    const INCIDENT_DEF_IDENT = 'test_ident-23';
16
17
    /**
18
     * test create incident ok
19
     */
20 View Code Duplication
    public function testCreateActionOk()
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...
21
    {
22
        $client = static::createClient();
23
24
        $client->request(
25
            'DELETE',
26
            '/incident/'.static::INCIDENT_DEF_IDENT
27
        );
28
29
        $testDataObjForI = $this->createFullIncidentObj();
30
31
        $crawler = $client->request(
32
            'PUT',
33
            '/incident/',
34
            [],
35
            [],
36
            [],
37
            json_encode($testDataObjForI)
38
        );
39
40
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
41
42
        $this->assertObjectHasAttribute('data', $responseObj);
43
44
        $this->assertEquals($testDataObjForI, $responseObj->data);
45
46
        $this->assertObjectHasAttribute('errors', $responseObj);
47
        $this->assertEmpty($responseObj->errors);
48
    }
49
50
    /**
51
     * test create incident fail without some required args
52
     */
53 View Code Duplication
    public function testCreateActionFail()
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...
54
    {
55
        $client = static::createClient();
56
57
        $testDataObjForI = $this->createFullIncidentObj();
58
        unset($testDataObjForI->ident);
59
60
        $crawler = $client->request(
61
            'PUT',
62
            '/incident/',
63
            [],
64
            [],
65
            [],
66
            json_encode($testDataObjForI)
67
        );
68
69
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
70
71
        $this->assertObjectHasAttribute('data', $responseObj);
72
73
        $this->assertEmpty($responseObj->data);
74
75
        $this->assertObjectHasAttribute('errors', $responseObj);
76
        $this->count(1, $responseObj->errors);
0 ignored issues
show
Unused Code introduced by
The call to IncidentControllerTest::count() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to the method IncidentBundle\Tests\Con...ControllerTest::count() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
77
    }
78
79
    /**
80
     * @depends testCreateActionOk
81
     * test update incident ok
82
     */
83
    public function testUpdateActionOk()
84
    {
85
        $client = static::createClient();
86
87
        $testDataObjForI = $this->createFullIncidentObj();
88
89
        $testDataObjForI->name = self::UPDATED_NAME;
90
        $testDataObjForI->status = self::UPDATED_STATUS;
91
92
        $crawler = $client->request(
93
            'POST',
94
            '/incident/'.$testDataObjForI->ident,
95
            [],
96
            [],
97
            [],
98
            json_encode($testDataObjForI)
99
        );
100
101
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
102
103
        $this->assertObjectHasAttribute('data', $responseObj);
104
105
        $this->assertEquals($testDataObjForI, $responseObj->data);
106
107
        $this->assertObjectHasAttribute('errors', $responseObj);
108
        $this->assertEmpty($responseObj->errors);
109
    }
110
111
    /**
112
     * @depends testCreateActionOk
113
     * test update incident fail without ident
114
     */
115 View Code Duplication
    public function testUpdateActionFail()
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...
116
    {
117
        $client = static::createClient();
118
119
        $testDataObjForI = $this->createFullIncidentObj();
120
121
        unset($testDataObjForI->ident);
122
        $crawler = $client->request(
123
            'POST',
124
            '/incident/null',
125
            [],
126
            [],
127
            [],
128
            json_encode($testDataObjForI)
129
        );
130
131
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
132
133
        $this->assertObjectHasAttribute('data', $responseObj);
134
135
        $this->assertEmpty($responseObj->data);
136
137
        $this->assertObjectHasAttribute('errors', $responseObj);
138
        $this->count(1, $responseObj->errors);
0 ignored issues
show
Unused Code introduced by
The call to IncidentControllerTest::count() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to the method IncidentBundle\Tests\Con...ControllerTest::count() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
139
    }
140
141
    /**
142
     * @depends testUpdateActionOk
143
     * test get incident ok
144
     */
145
    public function testGetActionOk()
146
    {
147
        $client = static::createClient();
148
149
        $crawler = $client->request(
150
            'GET',
151
            '/incident/'.static::INCIDENT_DEF_IDENT
152
        );
153
154
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
155
156
        $this->assertObjectHasAttribute('data', $responseObj);
157
158
        $this->assertEquals(static::UPDATED_NAME, $responseObj->data->name);
159
        $this->assertEquals(static::UPDATED_STATUS, $responseObj->data->status);
160
161
        $this->assertObjectHasAttribute('errors', $responseObj);
162
        $this->assertEmpty($responseObj->errors);
163
    }
164
165
    /**
166
     * @depends testUpdateActionOk
167
     * test get incident fail wrong ident
168
     */
169
    public function testGetActionFail()
170
    {
171
        $client = static::createClient();
172
173
        $crawler = $client->request(
174
            'GET',
175
            '/incident/test2233432'
176
        );
177
178
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
179
180
        $this->assertObjectHasAttribute('data', $responseObj);
181
182
        $this->assertEmpty($responseObj->data);
183
184
        $this->assertObjectHasAttribute('errors', $responseObj);
185
        $this->assertCount(1, $responseObj->errors);
186
        $this->assertContains('Incident not find', $responseObj->errors[0]->text);
187
        $this->assertEquals(404, $responseObj->errors[0]->code);
188
    }
189
190
    /**
191
     * @return object
192
     */
193
    protected function createFullIncidentObj()
194
    {
195
        $testDataObjForI = (object) [
196
            'ident' => self::INCIDENT_DEF_IDENT,
197
            'name' => 'test_name-foo',
198
            'message' => 'Some check catch some error',
199
            'status' => 32,
200
            'type' => 'urgent',
201
        ];
202
203
        return $testDataObjForI;
204
    }
205
206
    /**
207
     * @depends testCreateActionOk
208
     * test get incident ok
209
     */
210
    public function testDeleteAction()
211
    {
212
        $client = static::createClient();
213
214
        $crawler = $client->request(
215
            'DELETE',
216
            '/incident/'.static::INCIDENT_DEF_IDENT
217
        );
218
219
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
220
221
        $this->assertObjectHasAttribute('errors', $responseObj);
222
        $this->assertEmpty($responseObj->errors);
223
224
        $crawler = $client->request(
225
            'DELETE',
226
            '/incident/'.static::INCIDENT_DEF_IDENT
227
        );
228
229
        $responseObj = $this->assertIncidentJsonResValid($client, $crawler);
230
231
        $this->assertCount(1, $responseObj->errors);
232
        $this->assertContains('Incident not find', $responseObj->errors[0]->text);
233
        $this->assertEquals(404, $responseObj->errors[0]->code);
234
    }
235
236
    /**
237
     * @param $client
238
     * @param $crawler
239
     *
240
     * @return mixed
241
     */
242 View Code Duplication
    protected function assertIncidentJsonResValid($client, $crawler)
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->assertEquals(200, $client->getResponse()->getStatusCode(), 'HTTP STATUS CODE FAIL:');
245
246
        $this->assertJson($crawler->text());
247
248
        $responseObj = json_decode($crawler->text());
249
250
        return $responseObj;
251
    }
252
}
253