Completed
Push — master ( 1e17d8...fcfafa )
by Paweł
06:03 queued 05:46
created

it_does_not_allow_to_create_product_attribute_without_required_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
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
namespace Sylius\Tests\Controller;
13
14
use Lakion\ApiTestCase\JsonApiTestCase;
15
use Sylius\Component\Product\Model\ProductAttributeInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Anna Walasek <[email protected]>
20
 */
21
final class ProductAttributeApiTest 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_attributes_list_when_access_is_denied()
43
    {
44
        $this->loadFixturesFromFile('resources/product_attributes.yml');
45
46
        $this->client->request('GET', '/api/v1/product-attributes/');
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_attributes()
56
    {
57
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
58
        $this->loadFixturesFromFile('resources/product_attributes.yml');
59
60
61
        $this->client->request('GET', '/api/v1/product-attributes/', [], [], static::$authorizedHeaderWithAccept);
62
63
        $response = $this->client->getResponse();
64
        $this->assertResponse($response, 'product_attribute/index_response', Response::HTTP_OK);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function it_does_not_allow_to_show_product_attribute_when_it_does_not_exist()
71
    {
72
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
73
74
        $this->client->request('GET', '/api/v1/product-attributes/-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_attribute()
84
    {
85
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
86
        $productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml');
87
        $productAttribute = $productAttributes['productAttribute1'];
88
89
        $this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept);
90
91
        $response = $this->client->getResponse();
92
        $this->assertResponse($response, 'product_attribute/show_response', Response::HTTP_OK);
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function it_does_not_allow_to_delete_product_attribute_if_it_does_not_exist()
99
    {
100
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
101
102
        $this->client->request('DELETE', '/api/v1/product-attributes/-1', [], [], static::$authorizedHeaderWithContentType);
103
104
        $response = $this->client->getResponse();
105
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function it_allows_to_delete_product_attribute()
112
    {
113
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
114
        $productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml');
115
        $productAttribute = $productAttributes['productAttribute1'];
116
117
        $this->client->request('DELETE', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType);
118
119
        $response = $this->client->getResponse();
120
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
121
122
        $this->client->request('GET', '/api/v1/product-attributes/', [], [], static::$authorizedHeaderWithAccept);
123
124
        $response = $this->client->getResponse();
125
        $this->assertResponse($response, 'product_attribute/index_response_after_delete', Response::HTTP_OK);
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function it_allows_to_create_product_attribute()
132
    {
133
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
134
        $this->loadFixturesFromFile('resources/locales.yml');
135
136
        $data =
137
<<<EOT
138
        {
139
            "code": "mug_material",
140
            "translations": {
141
                "de_CH": {
142
                    "name": "Becher Material"
143
                },
144
                "en_US": {
145
                    "name": "Mug material"
146
                }
147
            }
148
        }
149
EOT;
150
151
        $this->client->request('POST', '/api/v1/product-attributes/text', [], [], static::$authorizedHeaderWithContentType, $data);
152
153
        $response = $this->client->getResponse();
154
        $this->assertResponse($response, 'product_attribute/create_response', Response::HTTP_CREATED);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function it_does_not_allow_to_create_product_attribute_without_required_fields()
161
    {
162
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
163
164
        $this->client->request('POST', '/api/v1/product-attributes/text', [], [], static::$authorizedHeaderWithContentType, []);
165
166
        $response = $this->client->getResponse();
167
        $this->assertResponse($response, 'product_attribute/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
168
    }
169
170
    /**
171
     * @test
172
     */
173
    public function it_does_not_allow_to_create_product_attribute_without_type()
174
    {
175
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
176
177
        $this->client->request('POST', '/api/v1/product-attributes', [], [], static::$authorizedHeaderWithContentType, []);
178
179
        $response = $this->client->getResponse();
180
        $this->assertResponseCode($response, Response::HTTP_METHOD_NOT_ALLOWED);
181
    }
182
183
    /**
184
     * @test
185
     */
186
    public function it_allows_to_update_product_attribute()
187
    {
188
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
189
        $this->loadFixturesFromFile('resources/locales.yml');
190
        $productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml');
191
        $productAttribute = $productAttributes['productAttribute1'];
192
193
        $data =
194
<<<EOT
195
        {
196
            "position": 2,
197
            "translations": {
198
                "de_CH": {
199
                    "name": "Becher Material"
200
                },
201
                "en_US": {
202
                    "name": "Mug material"
203
                }
204
            }
205
        }
206
EOT;
207
208
        $this->client->request('PUT', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data);
209
210
        $response = $this->client->getResponse();
211
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
212
213
        $this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept);
214
215
        $response = $this->client->getResponse();
216
        $this->assertResponse($response, 'product_attribute/show_response_after_update', Response::HTTP_OK);
217
    }
218
219
    /**
220
     * @test
221
     */
222
    public function it_allows_to_partially_update_product_attribute()
223
    {
224
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
225
        $this->loadFixturesFromFile('resources/locales.yml');
226
        $productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml');
227
        $productAttribute = $productAttributes['productAttribute1'];
228
229
        $data =
230
<<<EOT
231
        {
232
            "translations": {
233
                "de_CH": {
234
                    "name": "Becher Material"
235
                },
236
                "en_US": {
237
                    "name": "Mug material"
238
                }
239
            }
240
        }
241
EOT;
242
243
        $this->client->request('PATCH', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data);
244
245
        $response = $this->client->getResponse();
246
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
247
248
        $this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept);
249
250
        $response = $this->client->getResponse();
251
        $this->assertResponse($response, 'product_attribute/show_response_after_partial_update', Response::HTTP_OK);
252
    }
253
254
    /**
255
     * @test
256
     */
257
    public function it_does_not_allow_to_change_product_attribute_type()
258
    {
259
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
260
        $this->loadFixturesFromFile('resources/locales.yml');
261
        $productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml');
262
        $productAttribute = $productAttributes['productAttribute1'];
263
264
        $data =
265
<<<EOT
266
        {
267
            "type": "integer"
268
        }
269
EOT;
270
271
        $this->client->request('PATCH', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data);
272
273
        $response = $this->client->getResponse();
274
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
275
276
        $this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept);
277
278
        $response = $this->client->getResponse();
279
        $this->assertResponse($response, 'product_attribute/show_response', Response::HTTP_OK);
280
    }
281
282
    /**
283
     * @param ProductAttributeInterface $productAttribute
284
     *
285
     * @return string
286
     */
287
    private function getProductAttributeUrl(ProductAttributeInterface $productAttribute)
288
    {
289
        return '/api/v1/product-attributes/' . $productAttribute->getCode();
290
    }
291
}
292