Passed
Pull Request — master (#1)
by one
07:55
created

ReviewControllerTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 133
dl 0
loc 287
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnauthorizedCreateAction() 0 23 1
A testShowAction() 0 16 1
A testDeleteAction() 0 16 1
A testNotFoundUpdateAction() 0 19 1
A testCreateAction() 0 29 1
A testBadRequestCreateAction() 0 20 1
A testNotFoundShowAction() 0 10 1
A testUnauthorizedUpdateAction() 0 21 1
A testUpdateAction() 0 25 1
A testListAction() 0 14 1
A testFilterListAction() 0 18 1
A testUnauthorizedDeleteAction() 0 10 1
A testNotFoundDeleteAction() 0 16 1
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 ReviewControllerTest extends AbstractWebTestCase
18
{
19
    /**
20
     * @var int
21
     */
22
    protected static $entityId;
23
24
    /**
25
     * @var int
26
     */
27
    protected static $rating;
28
29
    public function testUnauthorizedCreateAction()
30
    {
31
        self::$rating = 9;
32
33
        $this->client->request(
34
            Request::METHOD_POST,
35
            '/reviews',
36
            [],
37
            [],
38
            [],
39
            json_encode([
40
                'body' => 'Ut accusantium ad facere qui est. Voluptas quae rerum voluptas perspiciatis molestiae voluptas assumenda. Nobis impedit laudantium eaque saepe quae.',
41
                'rating' => self::$rating,
42
                'publicationDate' => '2018-06-24T00:00:00+00:00',
43
            ])
44
        );
45
46
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
47
48
        $this->assertTrue(
49
            $this->client->getResponse()->headers->contains(
50
                'Content-Type',
51
                'application/json'
52
            )
53
        );
54
    }
55
56
    public function testCreateAction()
57
    {
58
        $this->client->request(
59
            Request::METHOD_POST,
60
            '/reviews',
61
            [],
62
            [],
63
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
64
            json_encode([
65
                'body' => 'Ut accusantium ad facere qui est. Voluptas quae rerum voluptas perspiciatis molestiae voluptas assumenda. Nobis impedit laudantium eaque saepe quae.',
66
                'rating' => self::$rating,
67
                'publicationDate' => '2018-06-24T00:00:00+00:00',
68
            ])
69
        );
70
71
        $this->assertEquals(Response::HTTP_CREATED, $this->client->getResponse()->getStatusCode());
72
73
        $this->assertTrue(
74
            $this->client->getResponse()->headers->contains(
75
                'Content-Type',
76
                'application/json'
77
            )
78
        );
79
80
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
81
82
        $this->assertArrayHasKey('id', $responseContent);
83
84
        self::$entityId = $responseContent['id'];
85
    }
86
87
    public function testBadRequestCreateAction()
88
    {
89
        $this->client->request(
90
            Request::METHOD_POST,
91
            '/reviews',
92
            [],
93
            [],
94
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
95
            json_encode([
96
                'body' => 'Ut accusantium ad facere qui est. Voluptas quae rerum voluptas perspiciatis molestiae voluptas assumenda. Nobis impedit laudantium eaque saepe quae.',
97
                'rating' => self::$rating,
98
            ])
99
        );
100
101
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $this->client->getResponse()->getStatusCode());
102
103
        $this->assertTrue(
104
            $this->client->getResponse()->headers->contains(
105
                'Content-Type',
106
                'application/json'
107
            )
108
        );
109
    }
110
111
    public function testUnauthorizedUpdateAction()
112
    {
113
        self::$rating = 7;
114
115
        $this->client->request(
116
            Request::METHOD_PATCH,
117
            sprintf('/reviews/%d', self::$entityId),
118
            [],
119
            [],
120
            [],
121
            json_encode([
122
                'rating' => self::$rating,
123
            ])
124
        );
125
126
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
127
128
        $this->assertTrue(
129
            $this->client->getResponse()->headers->contains(
130
                'Content-Type',
131
                'application/json'
132
            )
133
        );
134
    }
135
136
    public function testUpdateAction()
137
    {
138
        $this->client->request(
139
            Request::METHOD_PATCH,
140
            sprintf('/reviews/%d', self::$entityId),
141
            [],
142
            [],
143
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
144
            json_encode([
145
                'rating' => self::$rating,
146
            ])
147
        );
148
149
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
150
151
        $this->assertTrue(
152
            $this->client->getResponse()->headers->contains(
153
                'Content-Type',
154
                'application/json'
155
            )
156
        );
157
158
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
159
160
        $this->assertSame('rating', array_search(self::$rating, $responseContent));
161
    }
162
163
    public function testNotFoundUpdateAction()
164
    {
165
        $this->client->request(
166
            Request::METHOD_PATCH,
167
            '/reviews/0',
168
            [],
169
            [],
170
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
171
            json_encode([
172
                'rating' => 7,
173
            ])
174
        );
175
176
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
177
178
        $this->assertTrue(
179
            $this->client->getResponse()->headers->contains(
180
                'Content-Type',
181
                'application/json'
182
            )
183
        );
184
    }
185
186
    public function testListAction()
187
    {
188
        $this->client->request(Request::METHOD_GET, '/reviews');
189
190
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
191
192
        $this->assertTrue(
193
            $this->client->getResponse()->headers->contains(
194
                'Content-Type',
195
                'application/json'
196
            )
197
        );
198
199
        $this->assertContains('reviews', $this->client->getResponse()->getContent());
200
    }
201
202
    public function testFilterListAction()
203
    {
204
        $this->client->request(Request::METHOD_GET, sprintf('/reviews?review_filter[rating]=%s', self::$rating));
205
206
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
207
208
        $this->assertTrue(
209
            $this->client->getResponse()->headers->contains(
210
                'Content-Type',
211
                'application/json'
212
            )
213
        );
214
215
        $this->assertContains('reviews', $this->client->getResponse()->getContent());
216
217
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
218
219
        $this->assertSame('rating', array_search(self::$rating, $responseContent['reviews'][0]));
220
    }
221
222
    public function testShowAction()
223
    {
224
        $this->client->request(Request::METHOD_GET, sprintf('/reviews/%d', self::$entityId));
225
226
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
227
228
        $this->assertTrue(
229
            $this->client->getResponse()->headers->contains(
230
                'Content-Type',
231
                'application/json'
232
            )
233
        );
234
235
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
236
237
        $this->assertSame('id', array_search(self::$entityId, $responseContent));
238
    }
239
240
    public function testNotFoundShowAction()
241
    {
242
        $this->client->request(Request::METHOD_GET, '/reviews/0');
243
244
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
245
246
        $this->assertTrue(
247
            $this->client->getResponse()->headers->contains(
248
                'Content-Type',
249
                'application/json'
250
            )
251
        );
252
    }
253
254
    public function testUnauthorizedDeleteAction()
255
    {
256
        $this->client->request(Request::METHOD_DELETE, sprintf('/reviews/%d', self::$entityId));
257
258
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
259
260
        $this->assertTrue(
261
            $this->client->getResponse()->headers->contains(
262
                'Content-Type',
263
                'application/json'
264
            )
265
        );
266
    }
267
268
    public function testDeleteAction()
269
    {
270
        $this->client->request(
271
            Request::METHOD_DELETE,
272
            sprintf('/reviews/%d', self::$entityId),
273
            [],
274
            [],
275
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
276
        );
277
278
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
279
280
        $this->assertTrue(
281
            $this->client->getResponse()->headers->contains(
282
                'Content-Type',
283
                'application/json'
284
            )
285
        );
286
    }
287
288
    public function testNotFoundDeleteAction()
289
    {
290
        $this->client->request(
291
            Request::METHOD_DELETE,
292
            '/reviews/0',
293
            [],
294
            [],
295
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
296
        );
297
298
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
299
300
        $this->assertTrue(
301
            $this->client->getResponse()->headers->contains(
302
                'Content-Type',
303
                'application/json'
304
            )
305
        );
306
    }
307
}
308