Completed
Push — master ( 5e83e1...bc9f64 )
by Paweł
50:52 queued 36:16
created

it_does_not_allow_to_create_product_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 Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Anna Walasek <[email protected]>
19
 */
20
final class ProductApiTest extends JsonApiTestCase
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $authorizedHeaderWithContentType = [
26
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
27
        'CONTENT_TYPE' => 'application/json',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $authorizedHeaderWithAccept = [
34
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
35
        'ACCEPT' => 'application/json',
36
    ];
37
38
    /**
39
     * @test
40
     */
41
    public function it_does_not_allow_to_show_products_list_when_access_is_denied()
42
    {
43
        $products = $this->loadFixturesFromFile('resources/products.yml');
44
        $this->client->request('GET', '/api/v1/products/'.$products['product1']->getId());
45
46
        $response = $this->client->getResponse();
47
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function it_does_not_allow_to_show_product_when_it_does_not_exist()
54
    {
55
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
56
57
        $this->client->request('GET', '/api/v1/products/-1', [], [], static::$authorizedHeaderWithAccept);
58
59
        $response = $this->client->getResponse();
60
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function it_allows_indexing_products()
67
    {
68
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
69
        $this->loadFixturesFromFile('resources/products.yml');
70
        $this->loadFixturesFromFile('resources/many_products.yml');
71
72
73
        $this->client->request('GET', '/api/v1/products/', [], [], static::$authorizedHeaderWithAccept);
74
75
        $response = $this->client->getResponse();
76
        $this->assertResponse($response, 'product/index_response', Response::HTTP_OK);
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function it_allows_showing_product()
83
    {
84
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
85
        $products = $this->loadFixturesFromFile('resources/products.yml');
86
87
        $this->client->request('GET', '/api/v1/products/'.$products['product1']->getId(), [], [], static::$authorizedHeaderWithAccept);
88
89
        $response = $this->client->getResponse();
90
        $this->assertResponse($response, 'product/show_response', Response::HTTP_OK);
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function it_does_not_allow_delete_product_if_it_does_not_exist()
97
    {
98
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
99
100
        $this->client->request('DELETE', '/api/v1/products/-1', [], [], static::$authorizedHeaderWithAccept);
101
102
        $response = $this->client->getResponse();
103
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function it_allows_delete_product()
110
    {
111
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
112
        $products = $this->loadFixturesFromFile('resources/products.yml');
113
114
        $this->client->request('DELETE', '/api/v1/products/'.$products['product1']->getId(), [], [], static::$authorizedHeaderWithContentType, []);
115
116
        $response = $this->client->getResponse();
117
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
118
119
        $this->client->request('GET', '/api/v1/products/'.$products['product1']->getId(), [], [], static::$authorizedHeaderWithAccept);
120
121
        $response = $this->client->getResponse();
122
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
123
    }
124
125
    /**
126
     * @test
127
     */
128
    public function it_allows_create_product_with_multiple_translations()
129
    {
130
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
131
        $this->loadFixturesFromFile('resources/locales.yml');
132
133
        $data =
134
<<<EOT
135
        {
136
            "code": "MUG_TH",
137
            "translations": {
138
                "nl": {
139
                    "name": "Theme Mug",
140
                    "slug": "theme-mug2"
141
                },
142
                "en": {
143
                    "name": "Theme Mug",
144
                    "slug": "theme-mug"
145
                }
146
            }
147
        }
148
EOT;
149
150
        $this->client->request('POST', '/api/v1/products/', [], [], static::$authorizedHeaderWithContentType, $data);
151
152
        $response = $this->client->getResponse();
153
        $this->assertResponse($response, 'product/create_response', Response::HTTP_CREATED);
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function it_does_not_allow_to_create_product_without_required_fields()
160
    {
161
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
162
163
        $this->client->request('POST', '/api/v1/products/', [], [], static::$authorizedHeaderWithContentType, []);
164
165
        $response = $this->client->getResponse();
166
        $this->assertResponse($response, 'product/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
167
    }
168
169
    /**
170
     * @test
171
     */
172
    public function it_allows_updating_product()
173
    {
174
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
175
        $products = $this->loadFixturesFromFile('resources/products.yml');
176
        $this->loadFixturesFromFile('resources/locales.yml');
177
178
        $data =
179
<<<EOT
180
        {
181
            "translations": {
182
                "en__US": {
183
                  "name": "Star Wars",
184
                  "slug": "star-wars"
185
                }
186
            }
187
        }
188
EOT;
189
        $this->client->request('PUT', '/api/v1/products/'. $products["product3"]->getId(), [], [], static::$authorizedHeaderWithContentType, $data);
190
        $response = $this->client->getResponse();
191
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
192
    }
193
194
    /**
195
     * @test
196
     */
197
    public function it_allows_updating_partial_information_about_product()
198
    {
199
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
200
        $products = $this->loadFixturesFromFile('resources/products.yml');
201
        $this->loadFixturesFromFile('resources/locales.yml');
202
203
        $data =
204
<<<EOT
205
        {
206
            "translations": {
207
                "en": {
208
                    "name": "Mug Star Wars Episode V"
209
                }
210
            }
211
        }
212
EOT;
213
        $this->client->request('PATCH', '/api/v1/products/'. $products["product1"]->getId(), [], [], static::$authorizedHeaderWithContentType, $data);
214
        $response = $this->client->getResponse();
215
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
216
    }
217
218
    /**
219
     * @test
220
     */
221
    public function it_allows_paginating_the_index_of_products()
222
    {
223
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
224
        $this->loadFixturesFromFile('resources/products.yml');
225
        $this->loadFixturesFromFile('resources/many_products.yml');
226
227
        $this->client->request('GET', '/api/v1/products/', ['page' => 2], [], static::$authorizedHeaderWithAccept);
228
        $response = $this->client->getResponse();
229
        $this->assertResponse($response, 'product/paginated_index_response');
230
    }
231
232
    /**
233
     * @test
234
     */
235
    public function it_allows_sorting_the_index_of_products()
236
    {
237
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
238
        $this->loadFixturesFromFile('resources/products.yml');
239
        $this->loadFixturesFromFile('resources/many_products.yml');
240
241
        $this->client->request('GET', '/api/v1/products/', ['sorting' => ['code' => 'asc']], [], static::$authorizedHeaderWithAccept);
242
        $response = $this->client->getResponse();
243
        $this->assertResponse($response, 'product/sorted_index_response');
244
    }
245
}
246