Completed
Push — master ( 1497b3...5a6800 )
by Kamil
26:06 queued 41s
created

it_allows_updating_information_about_product_review()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Tests\Controller;
15
16
use Lakion\ApiTestCase\JsonApiTestCase;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Component\Review\Model\ReviewInterface;
19
use Symfony\Component\HttpFoundation\Response;
20
21
final class ProductReviewApiTest extends JsonApiTestCase
22
{
23
24
    /**
25
     * @var array
26
     */
27
    private static $authorizedHeaderWithContentType = [
28
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
29
        'CONTENT_TYPE'       => 'application/json',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $authorizedHeaderWithAccept = [
36
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
37
        'ACCEPT'             => 'application/json',
38
    ];
39
40
    /**
41
     * @test
42
     */
43
    public function it_does_not_allows_showing_product_review_list_when_access_is_denied()
44
    {
45
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
46
47
        /** @var ProductInterface $product */
48
        $product = $productReviewsData['product1'];
49
50
        $this->client->request('GET', $this->getReviewListUrl($product));
51
        $response = $this->client->getResponse();
52
53
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function it_does_not_allows_showing_product_review_when_it_does_not_exist()
60
    {
61
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
62
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
63
64
        /** @var ProductInterface $product */
65
        $product = $productReviewsData['product1'];
66
67
        $this->client->request('GET', $this->getReviewListUrl($product) . '0', [], [], static::$authorizedHeaderWithAccept);
68
        $response = $this->client->getResponse();
69
70
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function it_allows_showing_product_review()
77
    {
78
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
79
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
80
81
        /** @var ProductInterface $product */
82
        $product = $productReviewsData['product1'];
83
84
        /** @var ReviewInterface $productReview */
85
        $productReview = $productReviewsData['productReview1'];
86
87
        $this->client->request('GET', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithAccept);
88
        $response = $this->client->getResponse();
89
90
        $this->assertResponse($response, 'product_review/show_response', Response::HTTP_OK);
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function it_allows_indexing_product_reviews()
97
    {
98
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
99
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
100
101
        /** @var ProductInterface $product */
102
        $product = $productReviewsData['product1'];
103
104
        $this->client->request('GET', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithAccept);
105
        $response = $this->client->getResponse();
106
107
        $this->assertResponse($response, 'product_review/index_response', Response::HTTP_OK);
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function it_allows_creating_product_review()
114
    {
115
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
116
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
117
118
        /** @var ProductInterface $product */
119
        $product = $productReviewsData['product1'];
120
121
        $data =
122
<<<EOT
123
        {
124
          "title": "A good product",
125
          "rating": "3",
126
          "comment": "This is a good product.",
127
          "author": {
128
            "email": "[email protected]"
129
          }
130
        }
131
EOT;
132
133
        $this->client->request('POST', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithContentType, $data);
134
        $response = $this->client->getResponse();
135
136
        $this->assertResponse($response, 'product_review/create_response', Response::HTTP_CREATED);
137
    }
138
139
    /**
140
     * @test
141
     */
142
    public function it_does_not_allows_creating_product_review_without_required_fields()
143
    {
144
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
145
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
146
147
        /** @var ProductInterface $product */
148
        $product = $productReviewsData['product1'];
149
150
        $this->client->request('POST', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithContentType, []);
151
        $response = $this->client->getResponse();
152
153
        $this->assertResponse($response, 'product_review/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function it_does_not_allows_deleting_product_review_if_it_does_not_exist()
160
    {
161
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
162
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
163
164
        /** @var ProductInterface $product */
165
        $product = $productReviewsData['product1'];
166
167
        $this->client->request('DELETE', $this->getReviewListUrl($product) . '0', [], [], static::$authorizedHeaderWithAccept);
168
        $response = $this->client->getResponse();
169
170
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function it_allows_deleting_product_review()
177
    {
178
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
179
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
180
181
        /** @var ProductInterface $product */
182
        $product = $productReviewsData['product1'];
183
184
        /** @var ReviewInterface $productReview */
185
        $productReview = $productReviewsData['productReview1'];
186
187
        $this->client->request('DELETE', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, []);
188
189
        $response = $this->client->getResponse();
190
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
191
192
        /** @var ProductInterface $product */
193
        $product = $productReviewsData['product1'];
194
195
        $this->client->request('GET', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithAccept);
196
        $response = $this->client->getResponse();
197
198
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
199
    }
200
201
    /**
202
     * @test
203
     */
204
    public function it_allows_updating_information_about_product_review()
205
    {
206
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
207
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
208
209
        /** @var ProductInterface $product */
210
        $product = $productReviewsData['product1'];
211
212
        /** @var ReviewInterface $productReview */
213
        $productReview = $productReviewsData['productReview1'];
214
215
        $data =
216
            <<<EOT
217
        {
218
            "title": "NEW_REVIEW_TITLE",
219
            "rating": "1",
220
            "comment": "NEW_REVIEW_COMMENT"
221
        }
222
EOT;
223
        $this->client->request('PUT', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, $data);
224
        $response = $this->client->getResponse();
225
226
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
227
    }
228
229
    /**
230
     * @test
231
     */
232
    public function it_allows_updating_partial_information_about_product_review()
233
    {
234
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
235
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
236
        $this->loadFixturesFromFile('resources/locales.yml');
237
238
        /** @var ProductInterface $product */
239
        $product = $productReviewsData['product1'];
240
241
        /** @var ReviewInterface $productReview */
242
        $productReview = $productReviewsData['productReview1'];
243
244
        $data =
245
            <<<EOT
246
        {
247
            "comment": "A_NEW_REVIEW_COMMENT"
248
        }
249
EOT;
250
251
        $this->client->request('PATCH', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, $data);
252
        $response = $this->client->getResponse();
253
254
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
255
    }
256
257
    /**
258
     * @test
259
     */
260
    public function it_allows_accepting_product_review()
261
    {
262
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
263
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
264
265
        /** @var ProductInterface $product */
266
        $product = $productReviewsData['product1'];
267
268
        /** @var ReviewInterface $productReview */
269
        $productReview = $productReviewsData['productReview1'];
270
271
        $this->client->request('PATCH', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
272
        $response = $this->client->getResponse();
273
274
        $this->assertResponse($response, 'product_review/accept_response', Response::HTTP_OK);
275
    }
276
277
    /**
278
     * @test
279
     */
280
    public function it_does_not_allows_accepting_product_review_if_it_has_not_new_status()
281
    {
282
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
283
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
284
285
        /** @var ProductInterface $product */
286
        $product = $productReviewsData['product1'];
287
288
        /** @var ReviewInterface $productReview */
289
        $productReview = $productReviewsData['productReview3'];
290
291
        $this->client->request('POST', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
292
        $response = $this->client->getResponse();
293
294
        $this->assertResponse($response, 'product_review/change_status_fail_response', Response::HTTP_BAD_REQUEST);
295
    }
296
297
    /**
298
     * @test
299
     */
300
    public function it_allows_rejecting_product_review()
301
    {
302
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
303
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
304
305
        /** @var ProductInterface $product */
306
        $product = $productReviewsData['product1'];
307
308
        /** @var ReviewInterface $productReview */
309
        $productReview = $productReviewsData['productReview1'];
310
311
        $this->client->request('PATCH', $this->getReviewUrl($product, $productReview) . '/reject', [], [], static::$authorizedHeaderWithAccept);
312
        $response = $this->client->getResponse();
313
314
        $this->assertResponse($response, 'product_review/reject_response', Response::HTTP_OK);
315
    }
316
317
    /**
318
     * @test
319
     */
320
    public function it_does_not_allows_rejecting_product_review_if_it_has_not_new_status()
321
    {
322
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
323
        $productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
324
325
        /** @var ProductInterface $product */
326
        $product = $productReviewsData['product1'];
327
328
        /** @var ReviewInterface $productReview */
329
        $productReview = $productReviewsData['productReview3'];
330
331
        $this->client->request('POST', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
332
        $response = $this->client->getResponse();
333
334
        $this->assertResponse($response, 'product_review/change_status_fail_response', Response::HTTP_BAD_REQUEST);
335
    }
336
337
    /**
338
     * @param ProductInterface $product
339
     *
340
     * @return string
341
     */
342
    private function getReviewListUrl(ProductInterface $product): string
343
    {
344
        return sprintf('/api/v1/products/%s/reviews/', $product->getCode());
345
    }
346
347
    /**
348
     * @param ProductInterface $product
349
     * @param ReviewInterface  $productReview
350
     *
351
     * @return string
352
     */
353
    private function getReviewUrl(ProductInterface $product, ReviewInterface $productReview): string
354
    {
355
        return sprintf('%s%s', $this->getReviewListUrl($product), $productReview->getId());
356
    }
357
}
358