Issues (3641)

ConfigurableBundleCartWriterTest.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Client\ConfigurableBundleCart;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\DataBuilder\ConfigurableBundleTemplateBuilder;
12
use Generated\Shared\DataBuilder\ConfiguredBundleBuilder;
13
use Generated\Shared\DataBuilder\ProductConcreteBuilder;
14
use Generated\Shared\DataBuilder\QuoteBuilder;
15
use Generated\Shared\Transfer\ConfigurableBundleTemplateSlotTransfer;
16
use Generated\Shared\Transfer\ConfiguredBundleItemTransfer;
17
use Generated\Shared\Transfer\ConfiguredBundleTransfer;
18
use Generated\Shared\Transfer\ItemTransfer;
19
use Generated\Shared\Transfer\QuoteResponseTransfer;
20
use Generated\Shared\Transfer\QuoteTransfer;
21
use Generated\Shared\Transfer\UpdateConfiguredBundleRequestTransfer;
22
use Spryker\Client\ConfigurableBundleCart\Dependency\Client\ConfigurableBundleCartToCartClientBridge;
23
use Spryker\Client\ConfigurableBundleCart\Reader\QuoteItemReader;
24
use Spryker\Client\ConfigurableBundleCart\Updater\QuoteItemUpdater;
25
use Spryker\Client\ConfigurableBundleCart\Writer\CartWriter;
26
use Spryker\Shared\Kernel\Transfer\Exception\RequiredTransferPropertyException;
27
28
/**
29
 * Auto-generated group annotations
30
 *
31
 * @group SprykerTest
32
 * @group Client
33
 * @group ConfigurableBundleCart
34
 * @group ConfigurableBundleCartWriterTest
35
 * Add your own group annotations below this line
36
 */
37
class ConfigurableBundleCartWriterTest extends Unit
38
{
39
    /**
40
     * @var string
41
     */
42
    protected const FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1 = 'FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1';
43
44
    /**
45
     * @var string
46
     */
47
    protected const FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_2 = 'FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_2';
48
49
    /**
50
     * @var string
51
     */
52
    protected const FAKE_CONFIGURABLE_BUNDLE_UUID_1 = 'FAKE_CONFIGURABLE_BUNDLE_UUID_1';
53
54
    /**
55
     * @var string
56
     */
57
    protected const FAKE_CONFIGURABLE_BUNDLE_UUID_2 = 'FAKE_CONFIGURABLE_BUNDLE_UUID_2';
58
59
    /**
60
     * @var string
61
     */
62
    protected const FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_1 = 'FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_1';
63
64
    /**
65
     * @var string
66
     */
67
    protected const FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_2 = 'FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_2';
68
69
    /**
70
     * @var string
71
     */
72
    protected const FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_3 = 'FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_3';
73
74
    /**
75
     * @var string
76
     */
77
    protected const FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_4 = 'FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_4';
78
79
    /**
80
     * @var string
81
     */
82
    protected const FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_5 = 'FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_5';
83
84
    /**
85
     * @see \Spryker\Client\ConfigurableBundleCart\Writer\CartWriter::GLOSSARY_KEY_CONFIGURED_BUNDLE_NOT_FOUND
86
     *
87
     * @var string
88
     */
89
    protected const GLOSSARY_KEY_CONFIGURED_BUNDLE_NOT_FOUND = 'configured_bundle_cart.error.configured_bundle_not_found';
90
91
    /**
92
     * @see \Spryker\Client\ConfigurableBundleCart\Writer\CartWriter::GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_REMOVED
93
     *
94
     * @var string
95
     */
96
    protected const GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_REMOVED = 'configured_bundle_cart.error.configured_bundle_cannot_be_removed';
97
98
    /**
99
     * @see \Spryker\Client\ConfigurableBundleCart\Writer\CartWriter::GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_UPDATED
100
     *
101
     * @var string
102
     */
103
    protected const GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_UPDATED = 'configured_bundle_cart.error.configured_bundle_cannot_be_updated';
104
105
    /**
106
     * @return void
107
     */
108
    public function testRemoveConfiguredBundleRemovesConfiguredBundleFromCart(): void
109
    {
110
        // Arrange
111
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
112
113
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
114
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1)
115
            ->setQuote($quoteTransfer);
116
117
        $cartClientMock = $this->createCartClientMock();
118
119
        $cartClientMock
120
            ->method('removeFromCart')
0 ignored issues
show
The method method() does not exist on Spryker\Client\Configura...eCartToCartClientBridge. ( Ignorable by Annotation )

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

120
            ->/** @scrutinizer ignore-call */ 
121
              method('removeFromCart')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            ->willReturn((new QuoteResponseTransfer())->setIsSuccessful(true));
122
123
        $cartWriterMock = $this->createCartWriterMock($cartClientMock);
124
125
        // Act
126
        $quoteResponseTransfer = $cartWriterMock->removeConfiguredBundle($updateConfiguredBundleRequestTransfer);
127
128
        // Assert
129
        $this->assertTrue($quoteResponseTransfer->getIsSuccessful());
130
    }
131
132
    /**
133
     * @return void
134
     */
135
    public function testRemoveConfiguredBundleThrowsExceptionWithoutGroupKey(): void
136
    {
137
        // Arrange
138
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
139
140
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
141
            ->setQuote($quoteTransfer);
142
143
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
144
145
        // Assert
146
        $this->expectException(RequiredTransferPropertyException::class);
147
148
        // Act
149
        $cartWriterMock->removeConfiguredBundle($updateConfiguredBundleRequestTransfer);
150
    }
151
152
    /**
153
     * @return void
154
     */
155
    public function testRemoveConfiguredBundleThrowsExceptionWithoutQuote(): void
156
    {
157
        // Arrange
158
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
159
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1);
160
161
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
162
163
        // Assert
164
        $this->expectException(RequiredTransferPropertyException::class);
165
166
        // Act
167
        $cartWriterMock->removeConfiguredBundle($updateConfiguredBundleRequestTransfer);
168
    }
169
170
    /**
171
     * @return void
172
     */
173
    public function testRemoveConfiguredBundleThrowsErrorMessageWithWrongConfiguredBundleGroupKey(): void
174
    {
175
        // Arrange
176
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
177
178
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
179
            ->setGroupKey('FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_3')
180
            ->setQuote($quoteTransfer);
181
182
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
183
184
        // Act
185
        $quoteResponseTransfer = $cartWriterMock->removeConfiguredBundle($updateConfiguredBundleRequestTransfer);
186
187
        // Assert
188
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
189
        $this->assertEquals(
190
            static::GLOSSARY_KEY_CONFIGURED_BUNDLE_NOT_FOUND,
191
            $quoteResponseTransfer->getErrors()[0]->getMessage(),
192
        );
193
    }
194
195
    /**
196
     * @return void
197
     */
198
    public function testRemoveConfiguredBundleThrowsErrorMessageWhenCartClientReturnError(): void
199
    {
200
        // Arrange
201
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
202
203
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
204
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1)
205
            ->setQuote($quoteTransfer);
206
207
        $cartClientMock = $this->createCartClientMock();
208
209
        $cartClientMock
210
            ->method('removeFromCart')
211
            ->willReturn((new QuoteResponseTransfer())->setIsSuccessful(false));
212
213
        $cartWriterMock = $this->createCartWriterMock($cartClientMock);
214
215
        // Act
216
        $quoteResponseTransfer = $cartWriterMock->removeConfiguredBundle($updateConfiguredBundleRequestTransfer);
217
218
        // Assert
219
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
220
        $this->assertEquals(
221
            static::GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_REMOVED,
222
            $quoteResponseTransfer->getErrors()[0]->getMessage(),
223
        );
224
    }
225
226
    /**
227
     * @return void
228
     */
229
    public function testUpdateConfiguredBundleQuantityChangesConfiguredBundleQuantity(): void
230
    {
231
        // Arrange
232
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
233
234
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
235
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1)
236
            ->setQuantity(5)
237
            ->setQuote($quoteTransfer);
238
239
        $cartClientMock = $this->createCartClientMock();
240
241
        $cartClientMock
242
            ->method('updateQuantity')
243
            ->willReturn((new QuoteResponseTransfer())->setIsSuccessful(true));
244
245
        $cartWriterMock = $this->createCartWriterMock($cartClientMock);
246
247
        // Act
248
        $quoteResponseTransfer = $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
249
250
        // Assert
251
        $this->assertTrue($quoteResponseTransfer->getIsSuccessful());
252
    }
253
254
    /**
255
     * @return void
256
     */
257
    public function testUpdateConfiguredBundleThrowsExceptionWithoutQuantity(): void
258
    {
259
        // Arrange
260
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
261
262
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
263
            ->setQuote($quoteTransfer)
264
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1);
265
266
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
267
268
        // Assert
269
        $this->expectException(RequiredTransferPropertyException::class);
270
271
        // Act
272
        $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
273
    }
274
275
    /**
276
     * @return void
277
     */
278
    public function testUpdateConfiguredBundleThrowsExceptionWithoutGroupKey(): void
279
    {
280
        // Arrange
281
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
282
283
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
284
            ->setQuote($quoteTransfer)
285
            ->setQuantity(5);
286
287
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
288
289
        // Assert
290
        $this->expectException(RequiredTransferPropertyException::class);
291
292
        // Act
293
        $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
294
    }
295
296
    /**
297
     * @return void
298
     */
299
    public function testUpdateConfiguredBundleThrowsExceptionWithoutQuote(): void
300
    {
301
        // Arrange
302
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
303
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1)
304
            ->setQuantity(5);
305
306
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
307
308
        // Assert
309
        $this->expectException(RequiredTransferPropertyException::class);
310
311
        // Act
312
        $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
313
    }
314
315
    /**
316
     * @return void
317
     */
318
    public function testUpdateConfiguredBundleThrowsErrorMessageWithWrongConfiguredBundleGroupKey(): void
319
    {
320
        // Arrange
321
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
322
323
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
324
            ->setGroupKey('FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_3')
325
            ->setQuantity(5)
326
            ->setQuote($quoteTransfer);
327
328
        $cartWriterMock = $this->createCartWriterMock($this->createCartClientMock());
329
330
        // Act
331
        $quoteResponseTransfer = $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
332
333
        // Assert
334
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
335
        $this->assertEquals(
336
            static::GLOSSARY_KEY_CONFIGURED_BUNDLE_NOT_FOUND,
337
            $quoteResponseTransfer->getErrors()[0]->getMessage(),
338
        );
339
    }
340
341
    /**
342
     * @return void
343
     */
344
    public function testUpdateConfiguredBundleThrowsErrorMessageWhenCartClientReturnError(): void
345
    {
346
        // Arrange
347
        $quoteTransfer = $this->getFakeQuoteWithConfiguredBundleItems();
348
349
        $updateConfiguredBundleRequestTransfer = (new UpdateConfiguredBundleRequestTransfer())
350
            ->setGroupKey(static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1)
351
            ->setQuantity(5)
352
            ->setQuote($quoteTransfer);
353
354
        $cartClientMock = $this->createCartClientMock();
355
356
        $cartClientMock
357
            ->method('updateQuantity')
358
            ->willReturn((new QuoteResponseTransfer())->setIsSuccessful(false));
359
360
        $cartWriterMock = $this->createCartWriterMock($cartClientMock);
361
362
        // Act
363
        $quoteResponseTransfer = $cartWriterMock->updateConfiguredBundleQuantity($updateConfiguredBundleRequestTransfer);
364
365
        // Assert
366
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
367
        $this->assertEquals(
368
            static::GLOSSARY_KEY_CONFIGURED_BUNDLE_CANNOT_BE_UPDATED,
369
            $quoteResponseTransfer->getErrors()[0]->getMessage(),
370
        );
371
    }
372
373
    /**
374
     * @return \Generated\Shared\Transfer\QuoteTransfer
375
     */
376
    protected function getFakeQuoteWithConfiguredBundleItems(): QuoteTransfer
377
    {
378
        return (new QuoteBuilder())
379
            ->withItem([
380
                ItemTransfer::SKU => (new ProductConcreteBuilder())->build()->getSku(),
381
                ItemTransfer::UNIT_PRICE => 1,
382
                ItemTransfer::QUANTITY => 1,
383
                ItemTransfer::CONFIGURED_BUNDLE_ITEM => $this->createConfiguredBundleItem(static::FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_1),
384
                ItemTransfer::CONFIGURED_BUNDLE => $this->createConfiguredBundle(
385
                    static::FAKE_CONFIGURABLE_BUNDLE_UUID_1,
386
                    static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1,
387
                ),
388
            ])
389
            ->withItem([
390
                ItemTransfer::SKU => (new ProductConcreteBuilder())->build()->getSku(),
391
                ItemTransfer::UNIT_PRICE => 1,
392
                ItemTransfer::QUANTITY => 1,
393
                ItemTransfer::CONFIGURED_BUNDLE_ITEM => $this->createConfiguredBundleItem(static::FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_2),
394
                ItemTransfer::CONFIGURED_BUNDLE => $this->createConfiguredBundle(
395
                    static::FAKE_CONFIGURABLE_BUNDLE_UUID_1,
396
                    static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_1,
397
                ),
398
            ])
399
            ->withItem([
400
                ItemTransfer::SKU => (new ProductConcreteBuilder())->build()->getSku(),
401
                ItemTransfer::UNIT_PRICE => 1,
402
                ItemTransfer::QUANTITY => 1,
403
                ItemTransfer::CONFIGURED_BUNDLE_ITEM => $this->createConfiguredBundleItem(static::FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_3),
404
                ItemTransfer::CONFIGURED_BUNDLE => $this->createConfiguredBundle(
405
                    static::FAKE_CONFIGURABLE_BUNDLE_UUID_2,
406
                    static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_2,
407
                ),
408
            ])
409
            ->withItem([
410
                ItemTransfer::SKU => (new ProductConcreteBuilder())->build()->getSku(),
411
                ItemTransfer::UNIT_PRICE => 1,
412
                ItemTransfer::QUANTITY => 1,
413
                ItemTransfer::CONFIGURED_BUNDLE_ITEM => $this->createConfiguredBundleItem(static::FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_4),
414
                ItemTransfer::CONFIGURED_BUNDLE => $this->createConfiguredBundle(
415
                    static::FAKE_CONFIGURABLE_BUNDLE_UUID_2,
416
                    static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_2,
417
                ),
418
            ])
419
            ->withItem([
420
                ItemTransfer::SKU => (new ProductConcreteBuilder())->build()->getSku(),
421
                ItemTransfer::UNIT_PRICE => 1,
422
                ItemTransfer::QUANTITY => 1,
423
                ItemTransfer::CONFIGURED_BUNDLE_ITEM => $this->createConfiguredBundleItem(static::FAKE_CONFIGURABLE_BUNDLE_SLOT_UUID_5),
424
                ItemTransfer::CONFIGURED_BUNDLE => $this->createConfiguredBundle(
425
                    static::FAKE_CONFIGURABLE_BUNDLE_UUID_2,
426
                    static::FAKE_CONFIGURABLE_BUNDLE_GROUP_KEY_2,
427
                ),
428
            ])
429
            ->build();
430
    }
431
432
    /**
433
     * @param string|null $templateUuid
434
     * @param string|null $groupKey
435
     *
436
     * @return \Generated\Shared\Transfer\ConfiguredBundleTransfer
437
     */
438
    protected function createConfiguredBundle(?string $templateUuid = null, ?string $groupKey = null): ConfiguredBundleTransfer
439
    {
440
        return (new ConfiguredBundleBuilder())->build()
441
            ->setTemplate((new ConfigurableBundleTemplateBuilder())->build()->setUuid($templateUuid))
442
            ->setGroupKey($groupKey);
443
    }
444
445
    /**
446
     * @param string|null $slotUuid
447
     *
448
     * @return \Generated\Shared\Transfer\ConfiguredBundleItemTransfer
449
     */
450
    protected function createConfiguredBundleItem(?string $slotUuid = null): ConfiguredBundleItemTransfer
451
    {
452
        return (new ConfiguredBundleItemTransfer())
453
            ->setSlot((new ConfigurableBundleTemplateSlotTransfer())->setUuid($slotUuid))
454
            ->setQuantityPerSlot(1);
455
    }
456
457
    /**
458
     * @param \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\ConfigurableBundleCart\Dependency\Client\ConfigurableBundleCartToCartClientBridge $cartClientMock
459
     *
460
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\ConfigurableBundleCart\Writer\CartWriter
461
     */
462
    protected function createCartWriterMock($cartClientMock): CartWriter
463
    {
464
        return $this->getMockBuilder(CartWriter::class)
465
            ->setConstructorArgs([
466
                $cartClientMock,
467
                $this->createQuoteItemReaderMock(),
468
                $this->createQuoteItemUpdaterMock(),
469
            ])
470
            ->setMethods(null)
471
            ->getMock();
472
    }
473
474
    /**
475
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\ConfigurableBundleCart\Dependency\Client\ConfigurableBundleCartToCartClientBridge
476
     */
477
    protected function createCartClientMock(): ConfigurableBundleCartToCartClientBridge
478
    {
479
        return $this->getMockBuilder(ConfigurableBundleCartToCartClientBridge::class)
480
            ->setMethods([
481
                'removeFromCart',
482
                'updateQuantity',
483
            ])
484
            ->disableOriginalConstructor()
485
            ->getMock();
486
    }
487
488
    /**
489
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\ConfigurableBundleCart\Updater\QuoteItemUpdater
490
     */
491
    protected function createQuoteItemUpdaterMock(): QuoteItemUpdater
492
    {
493
        return $this->getMockBuilder(QuoteItemUpdater::class)
494
            ->setConstructorArgs([
495
                $this->createQuoteItemReaderMock(),
496
            ])
497
            ->setMethods(null)
498
            ->getMock();
499
    }
500
501
    /**
502
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\ConfigurableBundleCart\Reader\QuoteItemReader
503
     */
504
    protected function createQuoteItemReaderMock(): QuoteItemReader
505
    {
506
        return $this->getMockBuilder(QuoteItemReader::class)
507
            ->disableOriginalConstructor()
508
            ->setMethods(null)
509
            ->getMock();
510
    }
511
}
512