Completed
Push — master ( 117100...23b980 )
by Paweł
26:53 queued 15:54
created

it_returns_not_found_response_when_trying_to_ship_an_order_which_does_not_exist()   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\Core\Model\OrderInterface;
16
use Sylius\Component\Order\Model\OrderItemInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @author Łukasz Chruściel <[email protected]>
21
 */
22
final class OrderApiTest extends CheckoutApiTestCase
23
{
24
    /**
25
     * @test
26
     */
27
    public function it_denies_getting_an_order_for_non_authenticated_user()
28
    {
29
        $this->client->request('GET', $this->getOrderUrl(-1));
30
31
        $response = $this->client->getResponse();
32
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
33
    }
34
35
    /**
36
     * @test
37
     */
38
    public function it_returns_not_found_response_when_requesting_details_of_an_order_which_does_not_exist()
39
    {
40
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
41
42
        $this->client->request('GET', $this->getOrderUrl(-1), [], [], static::$authorizedHeaderWithAccept);
43
44
        $response = $this->client->getResponse();
45
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function it_allows_to_get_cart()
52
    {
53
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
54
        $this->loadFixturesFromFile('resources/checkout.yml');
55
56
        $cartId = $this->createCart();
57
        $this->addItemToCart($cartId);
58
59
        $this->client->request('GET', $this->getOrderUrl($cartId), [], [], static::$authorizedHeaderWithAccept);
60
61
        $response = $this->client->getResponse();
62
        $this->assertResponse($response, 'order/cart_show_response', Response::HTTP_OK);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function it_allows_to_get_an_order()
69
    {
70
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
71
        $this->loadFixturesFromFile('resources/checkout.yml');
72
73
        $orderId = $this->prepareOrder();
74
75
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithAccept);
76
77
        $response = $this->client->getResponse();
78
        $this->assertResponse($response, 'order/order_show_response', Response::HTTP_OK);
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function it_denies_canceling_an_order_for_non_authenticated_user()
85
    {
86
        $this->client->request('PUT', $this->getCancelUrl(-1));
87
88
        $response = $this->client->getResponse();
89
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function it_returns_not_found_response_when_canceling_an_order_which_does_not_exist()
96
    {
97
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
98
99
        $this->client->request('PUT', $this->getCancelUrl(-1), [], [], static::$authorizedHeaderWithAccept);
100
101
        $response = $this->client->getResponse();
102
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function it_allows_to_cancel_an_order()
109
    {
110
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
111
        $this->loadFixturesFromFile('resources/checkout.yml');
112
113
        $orderId = $this->prepareOrder();
114
115
        $this->client->request('PUT', $this->getCancelUrl($orderId), [], [], static::$authorizedHeaderWithAccept);
116
117
        $response = $this->client->getResponse();
118
        $this->assertResponse($response, 'order/order_canceled_show_response', Response::HTTP_OK);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function it_denies_shipping_an_order_for_non_authenticated_user()
125
    {
126
        $this->client->request('PUT', $this->getShipOrderShipmentUrl(-1, -1));
127
128
        $response = $this->client->getResponse();
129
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function it_returns_not_found_response_when_trying_to_ship_an_order_which_does_not_exist()
136
    {
137
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
138
139
        $this->client->request('PUT', $this->getShipOrderShipmentUrl(-1, -1), [], [], static::$authorizedHeaderWithAccept);
140
141
        $response = $this->client->getResponse();
142
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function it_returns_not_found_response_when_shipping_does_not_exist_for_the_order()
149
    {
150
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
151
        $this->loadFixturesFromFile('resources/checkout.yml');
152
153
        $orderId = $this->prepareOrder();
154
155
        $this->client->request('PUT', $this->getShipOrderShipmentUrl($orderId, -1), [], [], static::$authorizedHeaderWithAccept);
156
157
        $response = $this->client->getResponse();
158
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function it_allows_to_ship_an_order()
165
    {
166
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
167
        $this->loadFixturesFromFile('resources/checkout.yml');
168
169
        $orderId = $this->prepareOrder();
170
171
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithContentType);
172
173
        $response = $this->client->getResponse();
174
        $rawResponse = json_decode($response->getContent(), true);
175
176
        $this->client->request('PUT', $this->getShipOrderShipmentUrl($orderId, $rawResponse['shipments'][0]['id']), [], [], static::$authorizedHeaderWithAccept);
177
178
        $response = $this->client->getResponse();
179
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
180
181
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithAccept);
182
183
        $response = $this->client->getResponse();
184
        $this->assertResponse($response, 'order/order_shipped_show_response', Response::HTTP_OK);
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function it_allows_to_ship_an_order_with_shipment_code()
191
    {
192
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
193
        $this->loadFixturesFromFile('resources/checkout.yml');
194
195
        $orderId = $this->prepareOrder();
196
197
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithContentType);
198
199
        $response = $this->client->getResponse();
200
        $rawResponse = json_decode($response->getContent(), true);
201
202
203
        $data =
204
<<<EOT
205
        {
206
            "tracking": "BANANAS"
207
        }
208
EOT;
209
210
        $this->client->request('PUT', $this->getShipOrderShipmentUrl($orderId, $rawResponse['shipments'][0]['id']), [], [], static::$authorizedHeaderWithContentType, $data);
211
212
        $response = $this->client->getResponse();
213
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
214
215
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithAccept);
216
217
        $response = $this->client->getResponse();
218
        $this->assertResponse($response, 'order/order_shipped_with_tracking_show_response', Response::HTTP_OK);
219
    }
220
221
    /**
222
     * @test
223
     */
224
    public function it_denies_completing_the_payment_for_the_order_for_non_authenticated_user()
225
    {
226
        $this->client->request('PUT', $this->getCompleteOrderPaymentUrl(-1, -1));
227
228
        $response = $this->client->getResponse();
229
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
230
    }
231
232
    /**
233
     * @test
234
     */
235
    public function it_returns_not_found_response_when_completing_the_payment_for_the_order_which_does_not_exist()
236
    {
237
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
238
239
        $this->client->request('PUT', $this->getShipOrderShipmentUrl(-1, -1), [], [], static::$authorizedHeaderWithAccept);
240
241
        $response = $this->client->getResponse();
242
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
243
    }
244
245
    /**
246
     * @test
247
     */
248
    public function it_returns_not_found_response_when_completing_payment_does_not_exist_for_the_order()
249
    {
250
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
251
        $this->loadFixturesFromFile('resources/checkout.yml');
252
253
        $orderId = $this->prepareOrder();
254
255
        $this->client->request('PUT', $this->getCompleteOrderPaymentUrl($orderId, -1), [], [], static::$authorizedHeaderWithAccept);
256
257
        $response = $this->client->getResponse();
258
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
259
    }
260
261
    /**
262
     * @test
263
     */
264
    public function it_allows_to_complete_the_payment_for_the_order()
265
    {
266
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
267
        $this->loadFixturesFromFile('resources/checkout.yml');
268
269
        $orderId = $this->prepareOrder();
270
271
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithContentType);
272
273
        $response = $this->client->getResponse();
274
        $rawResponse = json_decode($response->getContent(), true);
275
276
        $this->client->request('PUT', $this->getCompleteOrderPaymentUrl($orderId, $rawResponse['payments'][0]['id']), [], [], static::$authorizedHeaderWithAccept);
277
278
        $response = $this->client->getResponse();
279
        $this->assertResponse($response, 'order/order_payed_show_response', Response::HTTP_OK);
280
    }
281
282
    /**
283
     * @test
284
     */
285
    public function it_allows_to_complete_the_payment_and_ship_the_order()
286
    {
287
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
288
        $this->loadFixturesFromFile('resources/checkout.yml');
289
290
        $orderId = $this->prepareOrder();
291
292
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithContentType);
293
294
        $response = $this->client->getResponse();
295
        $rawResponse = json_decode($response->getContent(), true);
296
297
        $this->client->request('PUT', $this->getShipOrderShipmentUrl($orderId, $rawResponse['shipments'][0]['id']), [], [], static::$authorizedHeaderWithAccept);
298
299
        $response = $this->client->getResponse();
300
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
301
302
        $this->client->request('PUT', $this->getCompleteOrderPaymentUrl($orderId, $rawResponse['payments'][0]['id']), [], [], static::$authorizedHeaderWithAccept);
303
304
        $response = $this->client->getResponse();
305
        $this->assertResponse($response, 'order/order_payed_show_response', Response::HTTP_OK);
306
307
        $this->client->request('GET', $this->getOrderUrl($orderId), [], [], static::$authorizedHeaderWithAccept);
308
309
        $response = $this->client->getResponse();
310
        $this->assertResponse($response, 'order/order_fulfilled_show_response', Response::HTTP_OK);
311
    }
312
313
    /**
314
     * @param mixed $orderId
315
     *
316
     * @return string
317
     */
318
    private function getOrderUrl($orderId)
319
    {
320
        return '/api/v1/orders/' . $orderId;
321
    }
322
323
    /**
324
     * @param mixed $orderId
325
     * @param mixed $shipmentId
326
     *
327
     * @return string
328
     */
329
    private function getShipOrderShipmentUrl($orderId, $shipmentId)
330
    {
331
        return sprintf('%s/shipments/%s/ship', $this->getOrderUrl($orderId), $shipmentId);
332
    }
333
334
    /**
335
     * @param mixed $orderId
336
     * @param mixed $paymentId
337
     *
338
     * @return string
339
     */
340
    private function getCompleteOrderPaymentUrl($orderId, $paymentId)
341
    {
342
        return sprintf('%s/payments/%s/complete', $this->getOrderUrl($orderId), $paymentId);
343
    }
344
345
    /**
346
     * @param mixed $orderId
347
     *
348
     * @return string
349
     */
350
    private function getCancelUrl($orderId)
351
    {
352
        return $this->getOrderUrl($orderId) . '/cancel';
353
    }
354
}
355