Completed
Push — master ( 0541e3...1b0cb6 )
by Ilya
55s queued 18s
created

assertResponseContainsBundleItemBySku()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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;
11
12
use Generated\Shared\DataBuilder\QuoteBuilder;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\DataBuilder\QuoteBuilder 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...
13
use Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer 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\MoneyValueTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\MoneyValueTransfer 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 Generated\Shared\Transfer\PriceProductTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PriceProductTransfer 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...
16
use Generated\Shared\Transfer\ProductConcreteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ProductConcreteTransfer 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...
17
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
18
use Generated\Shared\Transfer\RestItemProductOptionsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...mProductOptionsTransfer 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...
19
use Generated\Shared\Transfer\RestItemsAttributesTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ItemsAttributesTransfer 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...
20
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...
21
use Generated\Shared\Transfer\StockProductTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\StockProductTransfer 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...
22
use Generated\Shared\Transfer\StoreTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\StoreTransfer 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...
23
use Spryker\Glue\CartReorderRestApi\CartReorderRestApiConfig;
24
use Spryker\Glue\CartsRestApi\CartsRestApiConfig;
25
use SprykerTest\Glue\Testify\Tester\ApiEndToEndTester;
26
27
/**
28
 * Inherited Methods
29
 *
30
 * @method void wantTo($text)
31
 * @method void wantToTest($text)
32
 * @method void execute($callable)
33
 * @method void expectTo($prediction)
34
 * @method void expect($prediction)
35
 * @method void amGoingTo($argumentation)
36
 * @method void am($role)
37
 * @method void lookForwardTo($achieveValue)
38
 * @method void comment($description)
39
 * @method void pause($vars = [])
40
 *
41
 * @SuppressWarnings(PHPMD)
42
 */
43
class CartReorderApiTester extends ApiEndToEndTester
44
{
45
    use _generated\CartReorderApiTesterActions;
0 ignored issues
show
Bug introduced by
The type PyzTest\Glue\CartReorder...ReorderApiTesterActions 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...
46
47
    /**
48
     * @uses \Spryker\Glue\CartsRestApi\CartsRestApiConfig::RESOURCE_CART_ITEMS
49
     *
50
     * @var string
51
     */
52
    public const RESOURCE_CARTS = 'carts';
53
54
    /**
55
     * @uses \Spryker\Glue\CartsRestApi\CartsRestApiConfig::RESOURCE_CART_ITEMS
56
     *
57
     * @var string
58
     */
59
    public const RESOURCE_CART_ITEMS = 'items';
60
61
    /**
62
     * @uses \Spryker\Glue\ProductBundleCartsRestApi\ProductBundleCartsRestApiConfig::RESOURCE_BUNDLE_ITEMS
63
     *
64
     * @var string
65
     */
66
    public const RESOURCE_BUNDLE_ITEMS = 'bundle-items';
67
68
    /**
69
     * @uses \Spryker\Shared\PriceProduct\PriceProductConfig::PRICE_TYPE_DEFAULT
70
     *
71
     * @var string
72
     */
73
    public const PRICE_TYPE = 'DEFAULT';
74
75
    /**
76
     * @var string
77
     */
78
    protected const PRICE_MODE = 'GROSS_MODE';
79
80
    /**
81
     * @var string
82
     */
83
    protected const STATE_MACHINE_NAME = 'DummyPayment01';
84
85
    /**
86
     * @var string
87
     */
88
    protected const TEST_CUSTOMER_PASSWORD = 'change123';
89
90
    /**
91
     * @return void
92
     */
93
    public function configureStateMachine(): void
94
    {
95
        $this->configureTestStateMachine([static::STATE_MACHINE_NAME]);
96
    }
97
98
    /**
99
     * @return \Generated\Shared\Transfer\StoreTransfer
100
     */
101
    public function getCurrentStore(): StoreTransfer
102
    {
103
        return $this->getLocator()->store()->facade()->getCurrentStore();
104
    }
105
106
    /**
107
     * @param string $customerName
108
     *
109
     * @return \Generated\Shared\Transfer\CustomerTransfer
110
     */
111
    public function createCustomer(string $customerName): CustomerTransfer
112
    {
113
        $customerTransfer = $this->haveCustomer([
114
            CustomerTransfer::USERNAME => $customerName,
115
            CustomerTransfer::PASSWORD => static::TEST_CUSTOMER_PASSWORD,
116
            CustomerTransfer::NEW_PASSWORD => static::TEST_CUSTOMER_PASSWORD,
117
        ]);
118
119
        return $this->confirmCustomer($customerTransfer);
120
    }
121
122
    /**
123
     * @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer
124
     *
125
     * @return \Generated\Shared\Transfer\ProductConcreteTransfer
126
     */
127
    public function createProductWithPriceAndStock(StoreTransfer $storeTransfer): ProductConcreteTransfer
128
    {
129
        $productConcreteTransfer = $this->haveFullProduct();
130
131
        $this->haveProductInStockForStore($storeTransfer, [
132
            StockProductTransfer::SKU => $productConcreteTransfer->getSku(),
133
            StockProductTransfer::IS_NEVER_OUT_OF_STOCK => 1,
134
        ]);
135
136
        $this->havePriceProduct([
137
            PriceProductTransfer::SKU_PRODUCT_ABSTRACT => $productConcreteTransfer->getAbstractSkuOrFail(),
138
            PriceProductTransfer::SKU_PRODUCT => $productConcreteTransfer->getSkuOrFail(),
139
            PriceProductTransfer::ID_PRODUCT => $productConcreteTransfer->getIdProductConcreteOrFail(),
140
            PriceProductTransfer::PRICE_TYPE_NAME => static::PRICE_TYPE,
141
            PriceProductTransfer::MONEY_VALUE => [
142
                MoneyValueTransfer::NET_AMOUNT => 777,
143
                MoneyValueTransfer::GROSS_AMOUNT => 888,
144
            ],
145
        ]);
146
147
        return $productConcreteTransfer;
148
    }
149
150
    /**
151
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
152
     * @param array<string, mixed> $seedData
153
     * @param list<\Spryker\Zed\CheckoutExtension\Dependency\Plugin\CheckoutDoSaveOrderInterface> $checkoutDoSaveOrderPlugins
0 ignored issues
show
Bug introduced by
The type PyzTest\Glue\CartReorder\list 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...
154
     *
155
     * @return \Generated\Shared\Transfer\SaveOrderTransfer
156
     */
157
    public function createOrder(
158
        CustomerTransfer $customerTransfer,
159
        array $seedData,
160
        array $checkoutDoSaveOrderPlugins = [],
161
    ): SaveOrderTransfer {
162
        $quoteTransfer = $this->createQuoteTransfer($customerTransfer, $seedData);
163
        $quoteTransfer->setPriceMode(static::PRICE_MODE);
164
165
        return $this->haveOrderFromQuote(
166
            $quoteTransfer,
167
            static::STATE_MACHINE_NAME,
168
            $checkoutDoSaveOrderPlugins,
169
        );
170
    }
171
172
    /**
173
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
174
     *
175
     * @return void
176
     */
177
    public function authorizeCustomerToGlue(CustomerTransfer $customerTransfer): void
178
    {
179
        $oauthResponseTransfer = $this->haveAuthorizationToGlue($customerTransfer);
180
        $this->amBearerAuthenticated($oauthResponseTransfer->getAccessToken());
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getCartReorderUrl(): string
187
    {
188
        $url = sprintf('{cartReorderResource}?include=%s,%s', static::RESOURCE_CART_ITEMS, static::RESOURCE_BUNDLE_ITEMS);
189
190
        return $this->formatUrl($url, [
191
            'cartReorderResource' => CartReorderRestApiConfig::RESOURCE_CART_REORDER,
192
        ]);
193
    }
194
195
    /**
196
     * @param string $sku
197
     *
198
     * @return void
199
     */
200
    public function assertResponseContainsItemBySku(string $sku): void
201
    {
202
        $this->assertNotNull($this->findIncludedItemsResourceBySku($sku));
203
    }
204
205
    /**
206
     * @param string $sku
207
     *
208
     * @return void
209
     */
210
    public function assertResponseDoesNotContainItemBySku(string $sku): void
211
    {
212
        $this->assertNull($this->findIncludedItemsResourceBySku($sku));
213
    }
214
215
    /**
216
     * @param string $sku
217
     *
218
     * @return void
219
     */
220
    public function assertResponseContainsBundleItemBySku(string $sku): void
221
    {
222
        $this->assertNotNull($this->findIncludedBundleItemsResourceBySku($sku));
223
    }
224
225
    /**
226
     * @param string $sku
227
     * @param int $quantity
228
     *
229
     * @return void
230
     */
231
    public function assertItemHasCorrectQuantity(string $sku, int $quantity): void
232
    {
233
        $itemsResourceAttributes = $this->findIncludedItemsResourceBySku($sku)['attributes'];
234
235
        $this->assertArrayHasKey(RestItemsAttributesTransfer::QUANTITY, $itemsResourceAttributes);
236
        $this->assertSame($quantity, $itemsResourceAttributes[RestItemsAttributesTransfer::QUANTITY]);
237
    }
238
239
    /**
240
     * @param string $sku
241
     * @param int $quantity
242
     *
243
     * @return void
244
     */
245
    public function assertBundleItemHasCorrectQuantity(string $sku, int $quantity): void
246
    {
247
        $bundleItemsResourceAttributes = $this->findIncludedBundleItemsResourceBySku($sku)['attributes'];
248
249
        $this->assertArrayHasKey(RestItemsAttributesTransfer::QUANTITY, $bundleItemsResourceAttributes);
250
        $this->assertSame($quantity, $bundleItemsResourceAttributes[RestItemsAttributesTransfer::QUANTITY]);
251
    }
252
253
    /**
254
     * @param string $productConcreteSku
255
     * @param string $productOptionSku
256
     *
257
     * @return void
258
     */
259
    public function assertItemHasProductOption(string $productConcreteSku, string $productOptionSku): void
260
    {
261
        $itemsResourceAttributes = $this->findIncludedItemsResourceBySku($productConcreteSku)['attributes'];
262
263
        $this->assertArrayHasKey(RestItemsAttributesTransfer::SELECTED_PRODUCT_OPTIONS, $itemsResourceAttributes);
264
        $this->assertCount(1, $itemsResourceAttributes[RestItemsAttributesTransfer::SELECTED_PRODUCT_OPTIONS]);
265
266
        $productOption = $itemsResourceAttributes[RestItemsAttributesTransfer::SELECTED_PRODUCT_OPTIONS][0];
267
268
        $this->assertArrayHasKey(RestItemProductOptionsTransfer::SKU, $productOption);
269
        $this->assertSame($productOptionSku, $productOption[RestItemProductOptionsTransfer::SKU]);
270
    }
271
272
    /**
273
     * @param string $cartUuid
274
     *
275
     * @return string
276
     */
277
    public function buildCartsUrl(string $cartUuid): string
278
    {
279
        return $this->formatFullUrl(
280
            '{resourceCarts}/{cartUuid}',
281
            [
282
                'resourceCarts' => CartsRestApiConfig::RESOURCE_CARTS,
283
                'cartUuid' => $cartUuid,
284
            ],
285
        );
286
    }
287
288
    /**
289
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
290
     * @param array<string, mixed> $seedData
291
     *
292
     * @return \Generated\Shared\Transfer\QuoteTransfer
293
     */
294
    protected function createQuoteTransfer(CustomerTransfer $customerTransfer, array $seedData): QuoteTransfer
295
    {
296
        return (new QuoteBuilder($seedData))
297
            ->withCustomer([CustomerTransfer::CUSTOMER_REFERENCE => $customerTransfer->getCustomerReference()])
298
            ->withTotals()
299
            ->withShippingAddress()
300
            ->withBillingAddress()
301
            ->withCurrency()
302
            ->withPayment()
303
            ->build();
304
    }
305
306
    /**
307
     * @param string $sku
308
     *
309
     * @return array<string, mixed>|null
310
     */
311
    protected function findIncludedItemsResourceBySku(string $sku): ?array
312
    {
313
        $jsonPath = sprintf(
314
            '$..included[?(@.type == \'%s\')]',
315
            static::RESOURCE_CART_ITEMS,
316
        );
317
318
        $itemsResources = $this->getDataFromResponseByJsonPath($jsonPath);
319
        foreach ($itemsResources as $itemsResource) {
320
            if ($itemsResource['attributes']['sku'] === $sku) {
321
                return $itemsResource;
322
            }
323
        }
324
325
        return null;
326
    }
327
328
    /**
329
     * @param string $sku
330
     *
331
     * @return array<string, mixed>|null
332
     */
333
    protected function findIncludedBundleItemsResourceBySku(string $sku): ?array
334
    {
335
        $jsonPath = sprintf(
336
            '$..included[?(@.type == \'%s\')]',
337
            static::RESOURCE_BUNDLE_ITEMS,
338
        );
339
340
        $bundleItemsResources = $this->getDataFromResponseByJsonPath($jsonPath);
341
        foreach ($bundleItemsResources as $bundleItemsResource) {
342
            if ($bundleItemsResource['attributes']['sku'] === $sku) {
343
                return $bundleItemsResource;
344
            }
345
        }
346
347
        return null;
348
    }
349
}
350