Completed
Branch master (384cd7)
by Nil
04:48
created

JsonApiControllerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 232
Duplicated Lines 39.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 10
c 6
b 0
f 0
lcom 1
cbo 3
dl 91
loc 232
rs 10

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
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/9/15
5
 * Time: 3:05 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Tests\Laravel5\JsonApi;
12
13
/**
14
 * Class JsonApiControllerTest.
15
 */
16
class JsonApiControllerTest extends LaravelTestCase
17
{
18
    /**
19
     * Setup DB before each test.
20
     */
21
    public function setUp()
22
    {
23
        parent::setUp();
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function testListAction()
30
    {
31
        $response = $this->call('GET', 'http://localhost/api/v1/employees');
32
33
        $this->assertEquals(200, $response->getStatusCode());
34
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function testGetAction()
41
    {
42
        $content = <<<JSON
43
{
44
    "data": {
45
        "type": "employee",
46
        "attributes": {
47
            "company": "NilPortugues.com",
48
            "surname": "Portugués",
49
            "first_name": "Nil",
50
            "email_address": "nilportugues@localhost",
51
            "job_title": "Web Developer",
52
            "business_phone": "(123)555-0100",
53
            "home_phone": "(123)555-0102",
54
            "mobile_phone": null,
55
            "fax_number": "(123)555-0103",
56
            "address": "Plaça Catalunya 1",
57
            "city": "Barcelona",
58
            "state_province": "Barcelona",
59
            "zip_postal_code": "08028",
60
            "country_region": "Spain",
61
            "web_page": "http://nilportugues.com",
62
            "notes": null,
63
            "attachments": null
64
        }
65
    }
66
}
67
JSON;
68
69
        $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
70
71
        $response = $this->call('GET', 'http://localhost/api/v1/employees/1');
72
73
        $this->assertEquals(200, $response->getStatusCode());
74
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function testGetActionWhenEmployeeDoesNotExist()
81
    {
82
        $response = $this->call('GET', 'http://localhost/api/v1/employees/1000');
83
84
        $this->assertEquals(404, $response->getStatusCode());
85
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function testPostAction()
92
    {
93
        $content = <<<JSON
94
{
95
    "data": {
96
        "type": "employee",
97
        "attributes": {
98
            "company": "NilPortugues.com",
99
            "surname": "Portugués",
100
            "first_name": "Nil",
101
            "email_address": "nilportugues@localhost",
102
            "job_title": "Web Developer",
103
            "business_phone": "(123)555-0100",
104
            "home_phone": "(123)555-0102",
105
            "mobile_phone": null,
106
            "fax_number": "(123)555-0103",
107
            "address": "Plaça Catalunya 1",
108
            "city": "Barcelona",
109
            "state_province": "Barcelona",
110
            "zip_postal_code": "08028",
111
            "country_region": "Spain",
112
            "web_page": "http://nilportugues.com",
113
            "notes": null,
114
            "attachments": null
115
        }
116
    }
117
}
118
JSON;
119
        $response = $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
120
121
        $this->assertEquals(201, $response->getStatusCode());
122
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
123
        $this->assertEquals('http://localhost/api/v1/employees/1', $response->headers->get('Location'));
124
    }
125
126
    /**
127
     * @test
128
     */
129
    public function testPostActionCreateNonexistentTypeAndReturnErrors()
130
    {
131
        $content = <<<JSON
132
{
133
    "data": {
134
        "type": "not_employee",
135
        "attributes": {}
136
    }
137
}
138
JSON;
139
        $response = $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
140
141
        $this->assertEquals(422, $response->getStatusCode());
142
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function testPostActionReturnsErrorBecauseAttributesAreMissing()
149
    {
150
        $content = <<<JSON
151
{
152
    "data": {
153
        "type": "employee",
154
        "attributes": {
155
            "company": "NilPortugues.com",
156
            "surname": "Portugués",
157
            "first_name": "Nil",
158
            "email_address": "nilportugues@localhost",
159
            "job_title": "Web Developer",
160
            "business_phone": "(123)555-0100",
161
            "home_phone": "(123)555-0102",
162
            "mobile_phone": null,
163
            "country_region": "Spain",
164
            "web_page": "http://nilportugues.com",
165
            "notes": null,
166
            "attachments": null
167
        }
168
    }
169
}
170
JSON;
171
        $response = $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
172
173
        $this->assertEquals(422, $response->getStatusCode());
174
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function testPatchActionWhenEmployeeDoesNotExistReturns400()
181
    {
182
        $content = <<<JSON
183
{
184
  "data": {
185
    "type": "employee",
186
    "id": 1000,
187
    "attributes": {
188
      "email_address": "[email protected]"
189
    }
190
  }
191
}
192
JSON;
193
        $response = $this->call('PATCH', 'http://localhost/api/v1/employees/1000', json_decode($content, true), [], [], []);
194
195
        $this->assertEquals(400, $response->getStatusCode());
196
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
197
    }
198
199
    /**
200
     * @test
201
     */
202
    public function testPutActionWhenEmployeeDoesNotExistReturns400()
203
    {
204
        $content = <<<JSON
205
{
206
  "data": {
207
    "type": "employee",
208
    "id": 1000,
209
    "attributes": {
210
          "company": "NilPortugues.com",
211
          "surname": "Portugués",
212
          "first_name": "Nil",
213
          "email_address": "nilportugues@localhost",
214
          "job_title": "Full Stack Web Developer",
215
          "business_phone": "(123)555-0100",
216
          "home_phone": "(123)555-0102",
217
          "mobile_phone": null,
218
          "fax_number": "(123)555-0103",
219
          "address": "Plaça Catalunya 1",
220
          "city": "Barcelona",
221
          "state_province": "Barcelona",
222
          "zip_postal_code": "08028",
223
          "country_region": "Spain",
224
          "web_page": "http://nilportugues.com",
225
          "notes": null,
226
          "attachments": null
227
       }
228
  }
229
}
230
JSON;
231
        $response = $this->call('PUT', 'http://localhost/api/v1/employees/1000', json_decode($content, true), [], [], []);
232
233
        $this->assertEquals(400, $response->getStatusCode());
234
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
235
    }
236
237
    /**
238
     * @test
239
     */
240
    public function testDeleteActionWhenEmployeeDoesNotExistReturns404()
241
    {
242
        $response = $this->call('DELETE', 'http://localhost/api/v1/employees/1000');
243
244
        $this->assertEquals(404, $response->getStatusCode());
245
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
246
    }
247
}
248