TaskTest::testGetTaskNotFound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\integration;
6
7
class TaskTest extends BaseTestCase
8
{
9
    private static int $id;
10
11
    /**
12
     * Test Get All Tasks.
13
     */
14
    public function testGetTasks(): void
15
    {
16
        $response = $this->runApp('GET', '/api/v1/tasks');
17
18
        $result = (string) $response->getBody();
19
20
        $this->assertEquals(200, $response->getStatusCode());
21
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
22
        $this->assertStringContainsString('success', $result);
23
        $this->assertStringContainsString('pagination', $result);
24
        $this->assertStringContainsString('data', $result);
25
        $this->assertStringNotContainsString('error', $result);
26
    }
27
28
    /**
29
     * Test Get Tasks By Page.
30
     */
31
    public function testGetTasksByPage(): void
32
    {
33
        $response = $this->runApp('GET', '/api/v1/tasks?page=1&perPage=3');
34
35
        $result = (string) $response->getBody();
36
37
        $this->assertEquals(200, $response->getStatusCode());
38
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
39
        $this->assertStringContainsString('success', $result);
40
        $this->assertStringContainsString('pagination', $result);
41
        $this->assertStringContainsString('data', $result);
42
        $this->assertStringContainsString('status', $result);
43
        $this->assertStringNotContainsString('error', $result);
44
    }
45
46
    /**
47
     * Test Get One Task.
48
     */
49
    public function testGetTask(): void
50
    {
51
        $response = $this->runApp('GET', '/api/v1/tasks/1');
52
53
        $result = (string) $response->getBody();
54
55
        $this->assertEquals(200, $response->getStatusCode());
56
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
57
        $this->assertStringContainsString('success', $result);
58
        $this->assertStringContainsString('id', $result);
59
        $this->assertStringContainsString('name', $result);
60
        $this->assertStringContainsString('status', $result);
61
        $this->assertStringNotContainsString('error', $result);
62
    }
63
64
    /**
65
     * Test Get Task Not Found.
66
     */
67
    public function testGetTaskNotFound(): void
68
    {
69
        $response = $this->runApp('GET', '/api/v1/tasks/123456789');
70
71
        $result = (string) $response->getBody();
72
73
        $this->assertEquals(404, $response->getStatusCode());
74
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
75
        $this->assertStringNotContainsString('success', $result);
76
        $this->assertStringNotContainsString('id', $result);
77
        $this->assertStringContainsString('error', $result);
78
    }
79
80
    /**
81
     * Test Create Task.
82
     */
83
    public function testCreateTask(): void
84
    {
85
        $response = $this->runApp(
86
            'POST',
87
            '/api/v1/tasks',
88
            ['name' => 'New Task', 'description' => 'My Desc.']
89
        );
90
91
        $result = (string) $response->getBody();
92
93
        self::$id = json_decode($result)->message->id;
94
95
        $this->assertEquals(201, $response->getStatusCode());
96
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
97
        $this->assertStringContainsString('success', $result);
98
        $this->assertStringContainsString('id', $result);
99
        $this->assertStringContainsString('name', $result);
100
        $this->assertStringContainsString('status', $result);
101
        $this->assertStringNotContainsString('error', $result);
102
    }
103
104
    /**
105
     * Test Get Task Created.
106
     */
107
    public function testGetTaskCreated(): void
108
    {
109
        $response = $this->runApp('GET', '/api/v1/tasks/' . self::$id);
110
111
        $result = (string) $response->getBody();
112
113
        $this->assertEquals(200, $response->getStatusCode());
114
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
115
        $this->assertStringContainsString('success', $result);
116
        $this->assertStringContainsString('id', $result);
117
        $this->assertStringContainsString('name', $result);
118
        $this->assertStringContainsString('status', $result);
119
        $this->assertStringNotContainsString('error', $result);
120
    }
121
122
    /**
123
     * Test Create Task Without Name.
124
     */
125
    public function testCreateTaskWithOutTaskName(): void
126
    {
127
        $response = $this->runApp('POST', '/api/v1/tasks');
128
129
        $result = (string) $response->getBody();
130
131
        $this->assertEquals(400, $response->getStatusCode());
132
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
133
        $this->assertStringNotContainsString('success', $result);
134
        $this->assertStringNotContainsString('id', $result);
135
        $this->assertStringContainsString('error', $result);
136
    }
137
138
    /**
139
     * Test Create Task With Invalid TaskName.
140
     */
141
    public function testCreateTaskWithInvalidTaskName(): void
142
    {
143
        $response = $this->runApp(
144
            'POST',
145
            '/api/v1/tasks',
146
            ['name' => '', 'status' => 1]
147
        );
148
149
        $result = (string) $response->getBody();
150
151
        $this->assertEquals(400, $response->getStatusCode());
152
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
153
        $this->assertStringNotContainsString('success', $result);
154
        $this->assertStringContainsString('error', $result);
155
    }
156
157
    /**
158
     * Test Create Task With Invalid Status.
159
     */
160
    public function testCreateTaskWithInvalidStatus(): void
161
    {
162
        $response = $this->runApp(
163
            'POST',
164
            '/api/v1/tasks',
165
            ['name' => 'ToDo', 'status' => 123]
166
        );
167
168
        $result = (string) $response->getBody();
169
170
        $this->assertEquals(400, $response->getStatusCode());
171
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
172
        $this->assertStringNotContainsString('success', $result);
173
        $this->assertStringContainsString('error', $result);
174
    }
175
176
    /**
177
     * Test Create Task Without Authorization Bearer JWT.
178
     */
179
    public function testCreateTaskWithoutBearerJWT(): void
180
    {
181
        $auth = self::$jwt;
182
        self::$jwt = '';
183
        $response = $this->runApp(
184
            'POST',
185
            '/api/v1/tasks',
186
            ['name' => 'my task', 'status' => 0]
187
        );
188
        self::$jwt = $auth;
189
190
        $result = (string) $response->getBody();
191
192
        $this->assertEquals(400, $response->getStatusCode());
193
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
194
        $this->assertStringNotContainsString('success', $result);
195
        $this->assertStringContainsString('error', $result);
196
    }
197
198
    /**
199
     * Test Create Task With Invalid JWT.
200
     */
201
    public function testCreateTaskWithInvalidJWT(): void
202
    {
203
        $auth = self::$jwt;
204
        self::$jwt = 'invalidToken';
205
        $response = $this->runApp(
206
            'POST',
207
            '/api/v1/tasks',
208
            ['name' => 'my task', 'status' => 0]
209
        );
210
        self::$jwt = $auth;
211
212
        $result = (string) $response->getBody();
213
214
        $this->assertEquals(400, $response->getStatusCode());
215
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
216
        $this->assertStringNotContainsString('success', $result);
217
        $this->assertStringContainsString('error', $result);
218
    }
219
220
    /**
221
     * Test Create Task With Forbidden JWT.
222
     */
223
    public function testCreateTaskWithForbiddenJWT(): void
224
    {
225
        $auth = self::$jwt;
226
        self::$jwt = 'Bearer eyJ0eXAiOiJK1NiJ9.eyJzdWIiOiI4Ii';
227
        $response = $this->runApp(
228
            'POST',
229
            '/api/v1/tasks',
230
            ['name' => 'my task', 'status' => 0]
231
        );
232
        self::$jwt = $auth;
233
234
        $result = (string) $response->getBody();
235
236
        $this->assertEquals(403, $response->getStatusCode());
237
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
238
        $this->assertStringNotContainsString('success', $result);
239
        $this->assertStringContainsString('error', $result);
240
    }
241
242
    /**
243
     * Test Update Task.
244
     */
245
    public function testUpdateTask(): void
246
    {
247
        $response = $this->runApp(
248
            'PUT',
249
            '/api/v1/tasks/' . self::$id,
250
            ['name' => 'Update Task', 'description' => 'Update Desc', 'status' => 1]
251
        );
252
253
        $result = (string) $response->getBody();
254
255
        $this->assertEquals(200, $response->getStatusCode());
256
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
257
        $this->assertStringContainsString('success', $result);
258
        $this->assertStringContainsString('id', $result);
259
        $this->assertStringContainsString('name', $result);
260
        $this->assertStringContainsString('status', $result);
261
        $this->assertStringNotContainsString('error', $result);
262
    }
263
264
    /**
265
     * Test Update Task Without Send Data.
266
     */
267
    public function testUpdateTaskWithOutSendData(): void
268
    {
269
        $response = $this->runApp('PUT', '/api/v1/tasks/' . self::$id);
270
271
        $result = (string) $response->getBody();
272
273
        $this->assertEquals(400, $response->getStatusCode());
274
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
275
        $this->assertStringNotContainsString('success', $result);
276
        $this->assertStringNotContainsString('id', $result);
277
        $this->assertStringContainsString('error', $result);
278
    }
279
280
    /**
281
     * Test Update Task Not Found.
282
     */
283
    public function testUpdateTaskNotFound(): void
284
    {
285
        $response = $this->runApp(
286
            'PUT',
287
            '/api/v1/tasks/123456789',
288
            ['name' => 'Task']
289
        );
290
291
        $result = (string) $response->getBody();
292
293
        $this->assertEquals(404, $response->getStatusCode());
294
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
295
        $this->assertStringNotContainsString('success', $result);
296
        $this->assertStringNotContainsString('id', $result);
297
        $this->assertStringContainsString('error', $result);
298
    }
299
300
    /**
301
     * Test Update Task of Another User.
302
     */
303
    public function testUpdateTaskOfAnotherUser(): void
304
    {
305
        $response = $this->runApp(
306
            'PUT',
307
            '/api/v1/tasks/6',
308
            ['name' => 'Task']
309
        );
310
311
        $result = (string) $response->getBody();
312
313
        $this->assertEquals(404, $response->getStatusCode());
314
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
315
        $this->assertStringNotContainsString('success', $result);
316
        $this->assertStringNotContainsString('id', $result);
317
        $this->assertStringContainsString('error', $result);
318
    }
319
320
    /**
321
     * Test Delete Task.
322
     */
323
    public function testDeleteTask(): void
324
    {
325
        $response = $this->runApp('DELETE', '/api/v1/tasks/' . self::$id);
326
327
        $result = (string) $response->getBody();
328
329
        $this->assertEquals(204, $response->getStatusCode());
330
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
331
        $this->assertStringContainsString('success', $result);
332
        $this->assertStringNotContainsString('error', $result);
333
    }
334
335
    /**
336
     * Test Delete Task Not Found.
337
     */
338
    public function testDeleteTaskNotFound(): void
339
    {
340
        $response = $this->runApp('DELETE', '/api/v1/tasks/123456789');
341
342
        $result = (string) $response->getBody();
343
344
        $this->assertEquals(404, $response->getStatusCode());
345
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
346
        $this->assertStringNotContainsString('success', $result);
347
        $this->assertStringNotContainsString('id', $result);
348
        $this->assertStringContainsString('error', $result);
349
    }
350
}
351