Completed
Push — master ( 4700f7...536353 )
by Nil
02:29
created

testPatchActionWhenEmployeeDoesNotExistReturns400()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 19
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
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
     * This is required for \Symfony\Bridge\PsrHttpMessage\Factory to work.
28
     * This comes as a trade-off of building the underlying package as framework-agnostic.
29
     *
30
     * @param string $method
31
     * @param string $server
32
     * @param string $uri
33
     */
34
    protected function serverEnvironment($method, $server, $uri)
0 ignored issues
show
Coding Style introduced by
serverEnvironment uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
    {
36
        $_SERVER['REQUEST_METHOD'] = strtoupper($method);
37
        $_SERVER['SERVER_NAME'] = str_replace(['http://', 'https://'], '', $server);
38
        $_SERVER['REQUEST_URI'] = $uri;
39
        $_SERVER['CONTENT_TYPE'] = 'application/json';
40
    }
41
42
    /**
43
     * @test
44
     */
45 View Code Duplication
    public function testListAction()
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...
46
    {
47
        $this->serverEnvironment('GET', 'localhost', '/api/v1/employees');
48
        $response = $this->call('GET', 'http://localhost/api/v1/employees');
49
50
        $this->assertEquals(200, $response->getStatusCode());
51
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function testGetAction()
58
    {
59
        $content = <<<JSON
60
{
61
    "data": {
62
        "type": "employee",
63
        "attributes": {
64
            "company": "NilPortugues.com",
65
            "surname": "Portugués",
66
            "first_name": "Nil",
67
            "email_address": "nilportugues@localhost",
68
            "job_title": "Web Developer",
69
            "business_phone": "(123)555-0100",
70
            "home_phone": "(123)555-0102",
71
            "mobile_phone": null,
72
            "fax_number": "(123)555-0103",
73
            "address": "Plaça Catalunya 1",
74
            "city": "Barcelona",
75
            "state_province": "Barcelona",
76
            "zip_postal_code": "08028",
77
            "country_region": "Spain",
78
            "web_page": "http://nilportugues.com",
79
            "notes": null,
80
            "attachments": null
81
        }
82
    }
83
}
84
JSON;
85
        $this->serverEnvironment('POST', 'localhost', '/api/v1/employees');
86
        $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
87
88
        $this->serverEnvironment('GET', 'localhost', '/api/v1/employees/1');
89
        $response = $this->call('GET', 'http://localhost/api/v1/employees/1');
90
91
        $this->assertEquals(200, $response->getStatusCode());
92
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
93
    }
94
95
    /**
96
     * @test
97
     */
98 View Code Duplication
    public function testGetActionWhenEmployeeDoesNotExist()
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...
99
    {
100
        $this->serverEnvironment('GET', 'localhost', '/api/v1/employees/1000');
101
        $response = $this->call('GET', 'http://localhost/api/v1/employees/1000');
102
103
        $this->assertEquals(404, $response->getStatusCode());
104
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function testPostAction()
111
    {
112
        $content = <<<JSON
113
{
114
    "data": {
115
        "type": "employee",
116
        "attributes": {
117
            "company": "NilPortugues.com",
118
            "surname": "Portugués",
119
            "first_name": "Nil",
120
            "email_address": "nilportugues@localhost",
121
            "job_title": "Web Developer",
122
            "business_phone": "(123)555-0100",
123
            "home_phone": "(123)555-0102",
124
            "mobile_phone": null,
125
            "fax_number": "(123)555-0103",
126
            "address": "Plaça Catalunya 1",
127
            "city": "Barcelona",
128
            "state_province": "Barcelona",
129
            "zip_postal_code": "08028",
130
            "country_region": "Spain",
131
            "web_page": "http://nilportugues.com",
132
            "notes": null,
133
            "attachments": null
134
        }
135
    }
136
}
137
JSON;
138
        $this->serverEnvironment('POST', 'localhost', '/api/v1/employees');
139
        $response = $this->call('POST', 'http://localhost/api/v1/employees', json_decode($content, true), [], [], []);
140
141
        $this->assertEquals(201, $response->getStatusCode());
142
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
143
        $this->assertEquals('http://localhost/api/v1/employees/1', $response->headers->get('Location'));
144
    }
145
146
    /**
147
     * @test
148
     */
149 View Code Duplication
    public function testPatchActionWhenEmployeeDoesNotExistReturns400()
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...
150
    {
151
        $content = <<<JSON
152
{
153
  "data": {
154
    "type": "employee",
155
    "id": 1000,
156
    "attributes": {
157
      "email_address": "[email protected]"
158
    }
159
  }
160
}
161
JSON;
162
        $this->serverEnvironment('PATCH', 'localhost', '/api/v1/employees/1000');
163
        $response = $this->call('PATCH', 'http://localhost/api/v1/employees/1000', json_decode($content, true), [], [], []);
164
165
        $this->assertEquals(400, $response->getStatusCode());
166
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
167
    }
168
169
    /**
170
     * @test
171
     */
172 View Code Duplication
    public function testPutActionWhenEmployeeDoesNotExistReturns400()
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...
173
    {
174
        $content = <<<JSON
175
{
176
  "data": {
177
    "type": "employee",
178
    "id": 1000,
179
    "attributes": {
180
          "company": "NilPortugues.com",
181
          "surname": "Portugués",
182
          "first_name": "Nil",
183
          "email_address": "nilportugues@localhost",
184
          "job_title": "Full Stack Web Developer",
185
          "business_phone": "(123)555-0100",
186
          "home_phone": "(123)555-0102",
187
          "mobile_phone": null,
188
          "fax_number": "(123)555-0103",
189
          "address": "Plaça Catalunya 1",
190
          "city": "Barcelona",
191
          "state_province": "Barcelona",
192
          "zip_postal_code": "08028",
193
          "country_region": "Spain",
194
          "web_page": "http://nilportugues.com",
195
          "notes": null,
196
          "attachments": null
197
       }
198
  }
199
}
200
JSON;
201
        $this->serverEnvironment('PUT', 'localhost', '/api/v1/employees/1000');
202
        $response = $this->call('PUT', 'http://localhost/api/v1/employees/1000', json_decode($content, true), [], [], []);
203
204
        $this->assertEquals(400, $response->getStatusCode());
205
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
206
    }
207
208
    /**
209
     * @test
210
     */
211 View Code Duplication
    public function testDeleteActionWhenEmployeeDoesNotExistReturns404()
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...
212
    {
213
        $this->serverEnvironment('DELETE', 'localhost', '/api/v1/employees/1000');
214
        $response = $this->call('DELETE', 'http://localhost/api/v1/employees/1000');
215
216
        $this->assertEquals(404, $response->getStatusCode());
217
        $this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
218
    }
219
}
220