Completed
Push — master ( f465c8...ce649b )
by Ilya
52s queued 19s
created

ProductConcreteCartReorderRestApiCest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 377
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
eloc 145
c 1
b 1
f 0
dl 0
loc 377
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A requestCreateReorderWithNonExistingOrderReference() 0 26 1
A requestCreateReorderWithEmptyOrderReferenceParameter() 0 26 1
A requestCreateCartReorderWithReorderStrategyNew() 0 23 1
A assertCreateCartReorder() 0 34 2
A loadFixtures() 0 6 1
A deleteLastResponseCart() 0 3 1
A requestCreateReorderWithNonExistingReorderStrategy() 0 28 1
A requestCreateCartReorderWithReorderStrategyReplace() 0 21 1
A requestCreateCartReorderWithNotAvailableProduct() 0 32 1
A requestCreateReorderWithAnotherCustomersOrderReference() 0 26 1
A requestCreateCartReorder() 0 20 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Glue\CartReorder\RestApi;
11
12
use Codeception\Util\HttpCode;
13
use Generated\Shared\Transfer\RestCheckoutErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...stCheckoutErrorTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\SaveOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SaveOrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use PyzTest\Glue\CartReorder\CartReorderApiTester;
16
use PyzTest\Glue\CartReorder\RestApi\Fixtures\ProductConcreteCartReorderRestApiFixtures;
17
use Spryker\Glue\CartReorderRestApi\CartReorderRestApiConfig;
18
19
/**
20
 * Auto-generated group annotations
21
 *
22
 * @group PyzTest
23
 * @group Glue
24
 * @group CartReorder
25
 * @group RestApi
26
 * @group ProductConcreteCartReorderRestApiCest
27
 * Add your own group annotations below this line
28
 * @group EndToEnd
29
 */
30
class ProductConcreteCartReorderRestApiCest
31
{
32
    /**
33
     * @var string
34
     */
35
    protected const RESPONSE_CODE_PARAMETER_ORDER_REFERENCE_INVALID = '901';
36
37
    /**
38
     * @var string
39
     */
40
    protected const RESPONSE_DETAIL_PARAMETER_ORDER_REFERENCE_INVALID = 'orderReference => This value should not be blank.';
41
42
    /**
43
     * @uses \Spryker\Glue\CartReorderRestApi\CartReorderRestApiConfig::ERROR_CODE_ORDER_NOT_FOUND
44
     *
45
     * @var string
46
     */
47
    protected const RESPONSE_CODE_ORDER_NOT_FOUND = '5801';
48
49
    /**
50
     * @uses \Spryker\Glue\CartReorderRestApi\CartReorderRestApiConfig::ERROR_CODE_QUOTE_NOT_PROVIDED
51
     *
52
     * @var string
53
     */
54
    protected const ERROR_CODE_QUOTE_NOT_PROVIDED = '5802';
55
56
    /**
57
     * @var string
58
     */
59
    protected const RESPONSE_DETAIL_ORDER_NOT_FOUND = 'Order not found.';
60
61
    /**
62
     * @var string
63
     */
64
    protected const RESPONSE_DETAIL_QUOTE_NOT_PROVIDED = 'Quote not provided.';
65
66
    /**
67
     * @uses \Spryker\Zed\PersistentCart\Communication\Plugin\CartReorder\ReplacePersistentCartReorderQuoteProviderStrategyPlugin::REORDER_STRATEGY_REPLACE
68
     *
69
     * @var string
70
     */
71
    protected const REORDER_STRATEGY_REPLACE = 'replace';
72
73
    /**
74
     * @uses \Spryker\Zed\MultiCart\Communication\Plugin\CartReorder\NewPersistentCartReorderQuoteProviderStrategyPlugin::REORDER_STRATEGY_NEW
75
     *
76
     * @var string
77
     */
78
    protected const REORDER_STRATEGY_NEW = 'new';
79
80
    /**
81
     * @var \PyzTest\Glue\CartReorder\RestApi\Fixtures\ProductConcreteCartReorderRestApiFixtures
82
     */
83
    protected ProductConcreteCartReorderRestApiFixtures $fixtures;
84
85
    /**
86
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
87
     *
88
     * @return void
89
     */
90
    public function loadFixtures(CartReorderApiTester $I): void
91
    {
92
        /** @var \PyzTest\Glue\CartReorder\RestApi\Fixtures\ProductConcreteCartReorderRestApiFixtures $fixtures */
93
        $fixtures = $I->loadFixtures(ProductConcreteCartReorderRestApiFixtures::class);
94
95
        $this->fixtures = $fixtures;
96
    }
97
98
    /**
99
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
100
     *
101
     * @return void
102
     */
103
    public function requestCreateCartReorder(CartReorderApiTester $I): void
104
    {
105
        // Arrange
106
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
107
108
        $saveOrderTransfer = $this->fixtures->getOrderWithConcreteProducts();
109
        $requestPayload = [
110
            'data' => [
111
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
112
                'attributes' => [
113
                    'orderReference' => $saveOrderTransfer->getOrderReferenceOrFail(),
114
                ],
115
            ],
116
        ];
117
118
        // Act
119
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
120
121
        // Assert
122
        $this->assertCreateCartReorder($I, $saveOrderTransfer);
123
    }
124
125
    /**
126
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
127
     *
128
     * @return void
129
     */
130
    public function requestCreateCartReorderWithReorderStrategyReplace(CartReorderApiTester $I): void
131
    {
132
        // Arrange
133
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
134
135
        $saveOrderTransfer = $this->fixtures->getOrderWithConcreteProducts();
136
        $requestPayload = [
137
            'data' => [
138
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
139
                'attributes' => [
140
                    'orderReference' => $saveOrderTransfer->getOrderReferenceOrFail(),
141
                    'reorderStrategy' => static::REORDER_STRATEGY_REPLACE,
142
                ],
143
            ],
144
        ];
145
146
        // Act
147
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
148
149
        // Assert
150
        $this->assertCreateCartReorder($I, $saveOrderTransfer);
151
    }
152
153
    /**
154
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
155
     *
156
     * @return void
157
     */
158
    public function requestCreateCartReorderWithReorderStrategyNew(CartReorderApiTester $I): void
159
    {
160
        // Arrange
161
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
162
163
        $saveOrderTransfer = $this->fixtures->getOrderWithConcreteProducts();
164
        $requestPayload = [
165
            'data' => [
166
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
167
                'attributes' => [
168
                    'orderReference' => $saveOrderTransfer->getOrderReferenceOrFail(),
169
                    'reorderStrategy' => static::REORDER_STRATEGY_NEW,
170
                ],
171
            ],
172
        ];
173
174
        // Act
175
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
176
177
        // Assert
178
        $this->assertCreateCartReorder($I, $saveOrderTransfer, '1');
179
180
        $this->deleteLastResponseCart($I);
181
    }
182
183
    /**
184
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
185
     *
186
     * @return void
187
     */
188
    public function requestCreateCartReorderWithNotAvailableProduct(CartReorderApiTester $I): void
189
    {
190
        // Arrange
191
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
192
193
        $saveOrderTransfer = $this->fixtures->getOrderWithNotAvailableConcreteProduct();
194
        $requestPayload = [
195
            'data' => [
196
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
197
                'attributes' => [
198
                    'orderReference' => $saveOrderTransfer->getOrderReferenceOrFail(),
199
                ],
200
            ],
201
        ];
202
203
        // Act
204
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
205
206
        // Assert
207
        $I->seeResponseCodeIs(HttpCode::CREATED);
208
        $I->seeResponseIsJson();
209
        $I->seeResponseMatchesOpenApiSchema();
210
211
        $I->amSure('The returned response includes first item.')
212
            ->whenI()
213
            ->assertResponseContainsItemBySku($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail());
0 ignored issues
show
Bug introduced by
It seems like assertResponseContainsItemBySku() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
            ->/** @scrutinizer ignore-call */ assertResponseContainsItemBySku($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail());
Loading history...
214
        $I->amSure('The first item has correct quantity.')
215
            ->whenI()
216
            ->assertItemHasCorrectQuantity($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail(), 2);
0 ignored issues
show
Bug introduced by
It seems like assertItemHasCorrectQuantity() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
            ->/** @scrutinizer ignore-call */ assertItemHasCorrectQuantity($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail(), 2);
Loading history...
217
        $I->amSure('The returned response does not include second item.')
218
            ->whenI()
219
            ->assertResponseDoesNotContainItemBySku($this->fixtures->getNotAvailableProductConcreteTransfer()->getSkuOrFail());
0 ignored issues
show
Bug introduced by
It seems like assertResponseDoesNotContainItemBySku() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            ->/** @scrutinizer ignore-call */ assertResponseDoesNotContainItemBySku($this->fixtures->getNotAvailableProductConcreteTransfer()->getSkuOrFail());
Loading history...
220
    }
221
222
    /**
223
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
224
     *
225
     * @return void
226
     */
227
    public function requestCreateReorderWithEmptyOrderReferenceParameter(CartReorderApiTester $I): void
228
    {
229
        //Arrange
230
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
231
232
        $requestPayload = [
233
            'data' => [
234
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
235
                'attributes' => [
236
                    'orderReference' => '',
237
                ],
238
            ],
239
        ];
240
241
        // Act
242
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
243
244
        // Assert
245
        $I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
246
        $I->seeResponseIsJson();
247
        $I->seeResponseMatchesOpenApiSchema();
248
249
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
250
        $I->assertEquals($errors[RestCheckoutErrorTransfer::CODE], static::RESPONSE_CODE_PARAMETER_ORDER_REFERENCE_INVALID);
251
        $I->assertEquals($errors[RestCheckoutErrorTransfer::STATUS], HttpCode::UNPROCESSABLE_ENTITY);
252
        $I->assertEquals($errors[RestCheckoutErrorTransfer::DETAIL], static::RESPONSE_DETAIL_PARAMETER_ORDER_REFERENCE_INVALID);
253
    }
254
255
    /**
256
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
257
     *
258
     * @return void
259
     */
260
    public function requestCreateReorderWithNonExistingOrderReference(CartReorderApiTester $I): void
261
    {
262
        //Arrange
263
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
264
265
        $requestPayload = [
266
            'data' => [
267
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
268
                'attributes' => [
269
                    'orderReference' => 'non-existing-order-reference',
270
                ],
271
            ],
272
        ];
273
274
        // Act
275
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
276
277
        // Assert
278
        $I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
279
        $I->seeResponseIsJson();
280
        $I->seeResponseMatchesOpenApiSchema();
281
282
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
283
        $I->assertEquals($errors[RestCheckoutErrorTransfer::CODE], static::RESPONSE_CODE_ORDER_NOT_FOUND);
284
        $I->assertEquals($errors[RestCheckoutErrorTransfer::STATUS], HttpCode::UNPROCESSABLE_ENTITY);
285
        $I->assertEquals($errors[RestCheckoutErrorTransfer::DETAIL], static::RESPONSE_DETAIL_ORDER_NOT_FOUND);
286
    }
287
288
    /**
289
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
290
     *
291
     * @return void
292
     */
293
    public function requestCreateReorderWithAnotherCustomersOrderReference(CartReorderApiTester $I): void
294
    {
295
        //Arrange
296
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
297
298
        $requestPayload = [
299
            'data' => [
300
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
301
                'attributes' => [
302
                    'orderReference' => $this->fixtures->getOrderFromAnotherCustomer()->getOrderReferenceOrFail(),
303
                ],
304
            ],
305
        ];
306
307
        // Act
308
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
309
310
        // Assert
311
        $I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
312
        $I->seeResponseIsJson();
313
        $I->seeResponseMatchesOpenApiSchema();
314
315
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
316
        $I->assertEquals($errors[RestCheckoutErrorTransfer::CODE], static::RESPONSE_CODE_ORDER_NOT_FOUND);
317
        $I->assertEquals($errors[RestCheckoutErrorTransfer::STATUS], HttpCode::UNPROCESSABLE_ENTITY);
318
        $I->assertEquals($errors[RestCheckoutErrorTransfer::DETAIL], static::RESPONSE_DETAIL_ORDER_NOT_FOUND);
319
    }
320
321
    /**
322
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
323
     *
324
     * @return void
325
     */
326
    public function requestCreateReorderWithNonExistingReorderStrategy(CartReorderApiTester $I): void
327
    {
328
        //Arrange
329
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
330
331
        $saveOrderTransfer = $this->fixtures->getOrderWithConcreteProducts();
332
        $requestPayload = [
333
            'data' => [
334
                'type' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
335
                'attributes' => [
336
                    'orderReference' => $saveOrderTransfer->getOrderReferenceOrFail(),
337
                    'reorderStrategy' => 'non-existing-reorder-strategy',
338
                ],
339
            ],
340
        ];
341
342
        // Act
343
        $I->sendPost($I->getCartReorderUrl(), $requestPayload);
344
345
        // Assert
346
        $I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
347
        $I->seeResponseIsJson();
348
        $I->seeResponseMatchesOpenApiSchema();
349
350
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
351
        $I->assertEquals($errors[RestCheckoutErrorTransfer::CODE], static::ERROR_CODE_QUOTE_NOT_PROVIDED);
352
        $I->assertEquals($errors[RestCheckoutErrorTransfer::STATUS], HttpCode::UNPROCESSABLE_ENTITY);
353
        $I->assertEquals($errors[RestCheckoutErrorTransfer::DETAIL], static::RESPONSE_DETAIL_QUOTE_NOT_PROVIDED);
354
    }
355
356
    /**
357
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
358
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
359
     * @param string|null $cartNamePostfix
360
     *
361
     * @return void
362
     */
363
    protected function assertCreateCartReorder(
364
        CartReorderApiTester $I,
365
        SaveOrderTransfer $saveOrderTransfer,
366
        ?string $cartNamePostfix = null,
367
    ): void {
368
        $I->seeResponseCodeIs(HttpCode::CREATED);
369
        $I->seeResponseIsJson();
370
        $I->seeResponseMatchesOpenApiSchema();
371
372
        $I->amSure('The returned resource is of correct type.')
373
            ->whenI()
374
            ->seeResponseDataContainsSingleResourceOfType(CartReorderApiTester::RESOURCE_CARTS);
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsSingleResourceOfType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

374
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsSingleResourceOfType(CartReorderApiTester::RESOURCE_CARTS);
Loading history...
375
376
        $I->amSure('The returned response data contains correct cart name.')
377
            ->whenI()
378
            ->assertResponseContainsCorrectCartName(
0 ignored issues
show
Bug introduced by
It seems like assertResponseContainsCorrectCartName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

378
            ->/** @scrutinizer ignore-call */ assertResponseContainsCorrectCartName(
Loading history...
379
                $cartNamePostfix
380
                    ? sprintf('Reorder from Order %s %s', $saveOrderTransfer->getOrderReferenceOrFail(), $cartNamePostfix)
381
                    : sprintf('Reorder from Order %s', $saveOrderTransfer->getOrderReferenceOrFail()),
382
            );
383
384
        $I->amSure('The returned response includes first item.')
385
            ->whenI()
386
            ->assertResponseContainsItemBySku($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail());
387
        $I->amSure('The first item has correct quantity.')
388
            ->whenI()
389
            ->assertItemHasCorrectQuantity($this->fixtures->getProductConcreteTransfer1()->getSkuOrFail(), 2);
390
391
        $I->amSure('The returned response includes second item.')
392
            ->whenI()
393
            ->assertResponseContainsItemBySku($this->fixtures->getProductConcreteTransfer2()->getSkuOrFail());
394
        $I->amSure('The second item has correct quantity.')
395
            ->whenI()
396
            ->assertItemHasCorrectQuantity($this->fixtures->getProductConcreteTransfer2()->getSkuOrFail(), 1);
397
    }
398
399
    /**
400
     * @param \PyzTest\Glue\CartReorder\CartReorderApiTester $I
401
     *
402
     * @return void
403
     */
404
    protected function deleteLastResponseCart(CartReorderApiTester $I): void
405
    {
406
        $I->sendDelete($I->buildCartsUrl($I->getDataFromResponseByJsonPath('$.data.id')));
407
    }
408
}
409