testUnauthorizedDeleteAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Tests\Controller;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class MovieControllerTest extends AbstractWebTestCase
18
{
19
    /**
20
     * @var int
21
     */
22
    protected static $entityId;
23
24
    /**
25
     * @var int
26
     */
27
    protected static $duration;
28
29
    public function testUnauthorizedCreateAction()
30
    {
31
        self::$duration = 3600;
32
33
        $this->client->request(
34
            Request::METHOD_POST,
35
            '/movies',
36
            [],
37
            [],
38
            [],
39
            json_encode([
40
                'duration' => self::$duration,
41
                'title' => 'Dolor similique aliquam.',
42
                'description' => 'Voluptatem voluptatem rerum vel error autem sunt ut reiciendis. Itaque numquam quam veniam id recusandae dolor totam. Necessitatibus amet eos ut enim ipsam.',
43
                'author' => 'Jade Carroll',
44
                'publicationDate' => '2000-09-04T17:58:04+00:00',
45
            ])
46
        );
47
48
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
49
50
        $this->assertTrue(
51
            $this->client->getResponse()->headers->contains(
52
                'Content-Type',
53
                'application/json'
54
            )
55
        );
56
    }
57
58
    public function testCreateAction()
59
    {
60
        $this->client->request(
61
            Request::METHOD_POST,
62
            '/movies',
63
            [],
64
            [],
65
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
66
            json_encode([
67
                'duration' => self::$duration,
68
                'title' => 'Dolor similique aliquam.',
69
                'description' => 'Voluptatem voluptatem rerum vel error autem sunt ut reiciendis. Itaque numquam quam veniam id recusandae dolor totam. Necessitatibus amet eos ut enim ipsam.',
70
                'director' => 'Jade Carroll',
71
                'publicationDate' => '2000-09-04T17:58:04+00:00',
72
            ])
73
        );
74
75
        $this->assertEquals(Response::HTTP_CREATED, $this->client->getResponse()->getStatusCode());
76
77
        $this->assertTrue(
78
            $this->client->getResponse()->headers->contains(
79
                'Content-Type',
80
                'application/json'
81
            )
82
        );
83
84
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
85
86
        $this->assertArrayHasKey('id', $responseContent);
87
88
        self::$entityId = $responseContent['id'];
89
    }
90
91
    public function testUnauthorizedUpdateAction()
92
    {
93
        self::$duration = 7200;
94
95
        $this->client->request(
96
            Request::METHOD_PATCH,
97
            sprintf('/movies/%d', self::$entityId),
98
            [],
99
            [],
100
            [],
101
            json_encode([
102
                'duration' => self::$duration,
103
            ])
104
        );
105
106
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
107
108
        $this->assertTrue(
109
            $this->client->getResponse()->headers->contains(
110
                'Content-Type',
111
                'application/json'
112
            )
113
        );
114
    }
115
116
    public function testBadRequestCreateAction()
117
    {
118
        $this->client->request(
119
            Request::METHOD_POST,
120
            '/movies',
121
            [],
122
            [],
123
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
124
            json_encode([
125
                'duration' => self::$duration,
126
                'title' => 'Dolor similique aliquam.',
127
                'description' => 'Voluptatem voluptatem rerum vel error autem sunt ut reiciendis. Itaque numquam quam veniam id recusandae dolor totam. Necessitatibus amet eos ut enim ipsam.',
128
                'director' => 'Jade Carroll',
129
            ])
130
        );
131
132
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $this->client->getResponse()->getStatusCode());
133
134
        $this->assertTrue(
135
            $this->client->getResponse()->headers->contains(
136
                'Content-Type',
137
                'application/json'
138
            )
139
        );
140
    }
141
142
    public function testUpdateAction()
143
    {
144
        $this->client->request(
145
            Request::METHOD_PATCH,
146
            sprintf('/movies/%d', self::$entityId),
147
            [],
148
            [],
149
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
150
            json_encode([
151
                'duration' => self::$duration,
152
            ])
153
        );
154
155
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
156
157
        $this->assertTrue(
158
            $this->client->getResponse()->headers->contains(
159
                'Content-Type',
160
                'application/json'
161
            )
162
        );
163
164
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
165
166
        $this->assertSame('duration', array_search(self::$duration, $responseContent));
167
    }
168
169
    public function testNotFoundUpdateAction()
170
    {
171
        $this->client->request(
172
            Request::METHOD_PATCH,
173
            '/movies/0',
174
            [],
175
            [],
176
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
177
            json_encode([
178
                'duration' => self::$duration,
179
            ])
180
        );
181
182
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
183
184
        $this->assertTrue(
185
            $this->client->getResponse()->headers->contains(
186
                'Content-Type',
187
                'application/json'
188
            )
189
        );
190
    }
191
192
    public function testListAction()
193
    {
194
        $this->client->request(Request::METHOD_GET, '/movies');
195
196
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
197
198
        $this->assertTrue(
199
            $this->client->getResponse()->headers->contains(
200
                'Content-Type',
201
                'application/json'
202
            )
203
        );
204
205
        $this->assertContains('movies', $this->client->getResponse()->getContent());
206
    }
207
208
    public function testFilterListAction()
209
    {
210
        $this->client->request(Request::METHOD_GET, sprintf('/movies?movie_filter[duration]=%s', self::$duration));
211
212
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
213
214
        $this->assertTrue(
215
            $this->client->getResponse()->headers->contains(
216
                'Content-Type',
217
                'application/json'
218
            )
219
        );
220
221
        $this->assertContains('movies', $this->client->getResponse()->getContent());
222
223
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
224
225
        $this->assertSame('duration', array_search(self::$duration, $responseContent['movies'][0]));
226
    }
227
228
    public function testShowAction()
229
    {
230
        $this->client->request(Request::METHOD_GET, sprintf('/movies/%d', self::$entityId));
231
232
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
233
234
        $this->assertTrue(
235
            $this->client->getResponse()->headers->contains(
236
                'Content-Type',
237
                'application/json'
238
            )
239
        );
240
241
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
242
243
        $this->assertSame('id', array_search(self::$entityId, $responseContent));
244
    }
245
246
    public function testNotFoundShowAction()
247
    {
248
        $this->client->request(Request::METHOD_GET, '/movies/0');
249
250
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
251
252
        $this->assertTrue(
253
            $this->client->getResponse()->headers->contains(
254
                'Content-Type',
255
                'application/json'
256
            )
257
        );
258
    }
259
260
    public function testUnauthorizedDeleteAction()
261
    {
262
        $this->client->request(Request::METHOD_DELETE, sprintf('/movies/%d', self::$entityId));
263
264
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
265
266
        $this->assertTrue(
267
            $this->client->getResponse()->headers->contains(
268
                'Content-Type',
269
                'application/json'
270
            )
271
        );
272
    }
273
274
    public function testDeleteAction()
275
    {
276
        $this->client->request(
277
            Request::METHOD_DELETE,
278
            sprintf('/movies/%d', self::$entityId),
279
            [],
280
            [],
281
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
282
        );
283
284
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
285
286
        $this->assertTrue(
287
            $this->client->getResponse()->headers->contains(
288
                'Content-Type',
289
                'application/json'
290
            )
291
        );
292
    }
293
294
    public function testNotFoundDeleteAction()
295
    {
296
        $this->client->request(
297
            Request::METHOD_DELETE,
298
            '/movies/0',
299
            [],
300
            [],
301
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
302
        );
303
304
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
305
306
        $this->assertTrue(
307
            $this->client->getResponse()->headers->contains(
308
                'Content-Type',
309
                'application/json'
310
            )
311
        );
312
    }
313
}
314