Completed
Push — master ( 144340...c83c24 )
by Paweł
17:07 queued 07:50
created

it_allows_to_partially_update_product_option_with_multiple_translations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 9.3191
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace Sylius\Tests\Controller;
13
14
use Lakion\ApiTestCase\JsonApiTestCase;
15
use Sylius\Component\Product\Model\ProductOptionInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Anna Walasek <[email protected]>
20
 */
21
final class ProductOptionApiTest extends JsonApiTestCase
22
{
23
    /**
24
     * @var array
25
     */
26
    private static $authorizedHeaderWithContentType = [
27
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
28
        'CONTENT_TYPE' => 'application/json',
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    private static $authorizedHeaderWithAccept = [
35
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
36
        'ACCEPT' => 'application/json',
37
    ];
38
39
    /**
40
     * @test
41
     */
42
    public function it_does_not_allow_to_show_product_options_list_when_access_is_denied()
43
    {
44
        $this->loadFixturesFromFile('resources/product_options.yml');
45
46
        $this->client->request('GET', '/api/v1/product-options/');
47
48
        $response = $this->client->getResponse();
49
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function it_allows_indexing_product_options()
56
    {
57
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
58
        $this->loadFixturesFromFile('resources/product_options.yml');
59
60
61
        $this->client->request('GET', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithAccept);
62
63
        $response = $this->client->getResponse();
64
        $this->assertResponse($response, 'product_option/index_response', Response::HTTP_OK);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function it_does_not_allow_to_show_product_option_when_it_does_not_exist()
71
    {
72
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
73
74
        $this->client->request('GET', '/api/v1/product-options/-1', [], [], static::$authorizedHeaderWithAccept);
75
76
        $response = $this->client->getResponse();
77
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function it_allows_showing_product_option()
84
    {
85
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
86
        $productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
87
        $productOption = $productOptions['mug-size'];
88
89
        $this->client->request('GET', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithAccept);
90
91
        $response = $this->client->getResponse();
92
        $this->assertResponse($response, 'product_option/show_response', Response::HTTP_OK);
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function it_does_not_allow_to_delete_product_option_if_it_does_not_exist()
99
    {
100
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
101
102
        $this->client->request('DELETE', '/api/v1/product-options/-1', [], [], static::$authorizedHeaderWithAccept);
103
104
        $response = $this->client->getResponse();
105
106
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_allows_to_delete_product_option()
113
    {
114
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
115
        $productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
116
        $productOption = $productOptions['mug-size'];
117
118
        $this->client->request('DELETE', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithContentType, []);
119
120
        $response = $this->client->getResponse();
121
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
122
123
        $this->client->request('GET', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithAccept);
124
125
        $response = $this->client->getResponse();
126
        $this->assertResponse($response, 'product_option/index_response_after_delete', Response::HTTP_OK);
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function it_allows_to_create_product_option_with_multiple_translations()
133
    {
134
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
135
        $this->loadFixturesFromFile('resources/locales.yml');
136
137
        $data = <<<EOT
138
{
139
    "code": "MUG_SIZE",
140
    "translations": {
141
        "de_CH": {
142
            "name": "Bechergröße"
143
        },
144
        "en_US": {
145
            "name": "Mug size"
146
        }
147
    },
148
    "values": [
149
        {
150
            "code": "MUG_SIZE_S",
151
            "translations": {
152
                "de_CH": {
153
                    "value": "Klein"
154
                },
155
                "en_US": {
156
                    "value": "Small"
157
                }
158
            }
159
        },
160
        {
161
            "code": "MUG_SIZE_L",
162
            "translations": {
163
                "de_CH": {
164
                    "value": "Groß"
165
                },
166
                "en_US": {
167
                    "value": "Large"
168
                }
169
            }
170
        }
171
    ]
172
}
173
EOT;
174
175
        $this->client->request('POST', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithContentType, $data);
176
177
        $response = $this->client->getResponse();
178
        $this->assertResponse($response, 'product_option/create_response', Response::HTTP_CREATED);
179
    }
180
181
    /**
182
     * @test
183
     */
184
    public function it_does_not_allow_to_create_product_option_without_required_fields()
185
    {
186
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
187
188
        $this->client->request('POST', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithContentType, []);
189
190
        $response = $this->client->getResponse();
191
        $this->assertResponse($response, 'product_option/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
192
    }
193
194
    /**
195
     * @test
196
     */
197
    public function it_allows_to_update_product_option_with_multiple_translations()
198
    {
199
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
200
        $this->loadFixturesFromFile('resources/locales.yml');
201
        $productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
202
        $productOption = $productOptions['mug-size'];
203
204
        $data = <<<EOT
205
{
206
    "code": "MUG_SIZE",
207
    "position": 2,
208
    "translations": {
209
        "de_CH": {
210
            "name": "Bechergröße"
211
        },
212
        "en_US": {
213
            "name": "Mug size"
214
        }
215
    },
216
    "values": [
217
        {
218
            "code": "MUG_SIZE_S",
219
            "translations": {
220
                "de_CH": {
221
                    "value": "Klein"
222
                },
223
                "en_US": {
224
                    "value": "Small"
225
                }
226
            }
227
        },
228
        {
229
            "code": "MUG_SIZE_L",
230
            "translations": {
231
                "de_CH": {
232
                    "value": "Groß"
233
                },
234
                "en_US": {
235
                    "value": "Large"
236
                }
237
            }
238
        },
239
        {
240
            "code": "MUG_SIZE_M",
241
            "translations": {
242
                "de_CH": {
243
                    "value": "Mittel"
244
                },
245
                "en_US": {
246
                    "value": "Medium"
247
                }
248
            }
249
        }
250
    ]
251
}
252
EOT;
253
254
        $this->client->request('PUT', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithContentType, $data);
255
256
        $response = $this->client->getResponse();
257
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
258
259
        $this->client->request('GET', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithAccept);
260
261
        $response = $this->client->getResponse();
262
        $this->assertResponse($response, 'product_option/show_response_after_update', Response::HTTP_OK);
263
    }
264
265
    /**
266
     * @test
267
     */
268
    public function it_allows_to_partially_update_product_option_with_multiple_translations()
269
    {
270
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
271
        $this->loadFixturesFromFile('resources/locales.yml');
272
        $productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
273
        $productOption = $productOptions['mug-size'];
274
275
        $data = <<<EOT
276
{
277
    "code": "MUG_SIZE",
278
    "translations": {
279
        "de_CH": {
280
            "name": "Bechergröße"
281
        },
282
        "en_US": {
283
            "name": "Mug size"
284
        }
285
    },
286
    "values": [
287
        {
288
            "code": "MUG_SIZE_S",
289
            "translations": {
290
                "de_CH": {
291
                    "value": "Klein"
292
                },
293
                "en_US": {
294
                    "value": "Small"
295
                }
296
            }
297
        },
298
        {
299
            "code": "MUG_SIZE_L",
300
            "translations": {
301
                "de_CH": {
302
                    "value": "Groß"
303
                },
304
                "en_US": {
305
                    "value": "Large"
306
                }
307
            }
308
        },
309
        {
310
            "code": "MUG_SIZE_M",
311
            "translations": {
312
                "de_CH": {
313
                    "value": "Mittel"
314
                },
315
                "en_US": {
316
                    "value": "Medium"
317
                }
318
            }
319
        }
320
    ]
321
}
322
EOT;
323
324
        $this->client->request('PATCH', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithContentType, $data);
325
326
        $response = $this->client->getResponse();
327
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
328
329
        $this->client->request('GET', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithAccept);
330
331
        $response = $this->client->getResponse();
332
        $this->assertResponse($response, 'product_option/show_response_after_partial_update', Response::HTTP_OK);
333
    }
334
335
    /**
336
     * @param ProductOptionInterface $productOption
337
     *
338
     * @return string
339
     */
340
    private function getProductOptionUrl(ProductOptionInterface $productOption)
341
    {
342
        return '/api/v1/product-options/' . $productOption->getCode();
343
    }
344
}
345