Issues (3641)

tests/SprykerTest/Client/Cart/CartClientTest.php (2 issues)

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\Cart;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\Transfer\CartChangeTransfer;
12
use Generated\Shared\Transfer\ItemTransfer;
13
use Generated\Shared\Transfer\QuoteTransfer;
14
use Spryker\Client\Cart\CartChangeRequestExpander\CartChangeRequestExpander;
15
use Spryker\Client\Cart\CartClient;
16
use Spryker\Client\Cart\Dependency\Client\CartToQuoteInterface;
17
use Spryker\Client\Cart\Plugin\ItemCountPlugin;
18
use Spryker\Client\Cart\Plugin\SessionQuoteStorageStrategyPlugin;
19
use Spryker\Client\Cart\Plugin\SimpleProductQuoteItemFinderPlugin;
20
use Spryker\Client\Cart\Zed\CartStubInterface;
21
use Spryker\Client\CartExtension\Dependency\Plugin\QuoteStorageStrategyPluginInterface;
22
use Spryker\Client\Kernel\AbstractFactory;
23
24
/**
25
 * Auto-generated group annotations
26
 *
27
 * @group SprykerTest
28
 * @group Client
29
 * @group Cart
30
 * @group CartClientTest
31
 * Add your own group annotations below this line
32
 */
33
class CartClientTest extends Unit
34
{
35
    /**
36
     * @var string
37
     */
38
    protected const PARAM_SEPARATE_PRODUCT = 'separate_product';
39
40
    /**
41
     * @var string
42
     */
43
    protected const ITEM_SKU = 'sku';
44
45
    /**
46
     * @var \SprykerTest\Client\Cart\CartClientTest
47
     */
48
    protected $tester;
49
50
    /**
51
     * @return void
52
     */
53
    public function testGetCartMustReturnInstanceOfQuoteTransfer(): void
54
    {
55
        $quoteTransfer = new QuoteTransfer();
56
        $quoteMock = $this->getQuoteMock();
57
        $quoteMock->expects($this->once())
58
            ->method('getQuote')
59
            ->will($this->returnValue($quoteTransfer));
60
61
        $factoryMock = $this->getFactoryMock($quoteMock);
62
        $cartClientMock = $this->getCartClientMock($factoryMock);
63
64
        $this->assertSame($quoteTransfer, $cartClientMock->getQuote());
65
    }
66
67
    /**
68
     * @return void
69
     */
70
    public function testClearCartMustSetItemCountInSessionToZero(): void
71
    {
72
        $quoteMock = $this->getQuoteMock();
73
        $quoteMock->expects($this->once())
74
            ->method('clearQuote')
75
            ->will($this->returnValue($quoteMock));
76
77
        $factoryMock = $this->getFactoryMock($quoteMock);
78
        $cartClientMock = $this->getCartClientMock($factoryMock);
79
80
        $cartClientMock->clearQuote();
81
    }
82
83
    /**
84
     * @return void
85
     */
86
    public function testClearCartMustSetCartTransferInSessionToAnEmptyInstance(): void
87
    {
88
        $quoteMock = $this->getQuoteMock();
89
        $quoteMock->expects($this->once())
90
            ->method('clearQuote')
91
            ->will($this->returnValue($quoteMock));
92
93
        $factoryMock = $this->getFactoryMock($quoteMock);
94
        $cartClientMock = $this->getCartClientMock($factoryMock);
95
96
        $cartClientMock->clearQuote();
97
    }
98
99
    /**
100
     * @return void
101
     */
102
    public function testAddItemMustOnlyExceptTransferInterfaceAsArgument(): void
103
    {
104
        $itemTransfer = new ItemTransfer();
105
        $quoteTransfer = new QuoteTransfer();
106
        $quoteMock = $this->getQuoteMock();
107
        $quoteMock->expects($this->once())
108
            ->method('getQuote')
109
            ->will($this->returnValue($quoteTransfer));
110
111
        $stubMock = $this->getStubMock();
112
        $stubMock->expects($this->once())
113
            ->method('addItem')
114
            ->will($this->returnValue($quoteTransfer));
115
116
        $sessionQuoteStorageStrategyPluginMock = $this->getSessionQuoteStorageStrategyPluginMock();
117
        $factoryMock = $this->getFactoryMock($quoteMock, $stubMock, $sessionQuoteStorageStrategyPluginMock);
118
        $cartClientMock = $this->getCartClientMock($factoryMock);
119
120
        $quoteTransfer = $cartClientMock->addItem($itemTransfer);
121
122
        $this->assertInstanceOf(QuoteTransfer::class, $quoteTransfer);
123
    }
124
125
    /**
126
     * @return void
127
     */
128
    public function testChangeItemQuantityMustCallRemoveItemQuantityWhenPassedItemQuantityIsLowerThenInCartGivenItem(): void
129
    {
130
        $itemTransfer = new ItemTransfer();
131
        $itemTransfer->setQuantity(2);
132
        $itemTransfer->setSku('sku');
133
134
        $quoteTransfer = new QuoteTransfer();
135
        $quoteTransfer->addItem($itemTransfer);
136
137
        $quoteMock = $this->getQuoteMock();
138
        $quoteMock->expects($this->exactly(3))
139
            ->method('getQuote')
140
            ->will($this->returnValue($quoteTransfer));
141
142
        $stubMock = $this->getStubMock();
143
        $stubMock->expects($this->once())
144
            ->method('removeItem')
145
            ->will($this->returnValue($quoteTransfer));
146
        $stubMock->expects($this->never())
147
            ->method('addItem')
148
            ->will($this->returnValue($quoteTransfer));
149
150
        $sessionQuoteStorageStrategyPluginMock = $this->getSessionQuoteStorageStrategyPluginMock();
151
        $factoryMock = $this->getFactoryMock($quoteMock, $stubMock, $sessionQuoteStorageStrategyPluginMock);
152
        $cartClientMock = $this->getCartClientMock($factoryMock);
153
154
        $itemTransfer = new ItemTransfer();
155
        $itemTransfer->setSku('sku');
156
157
        $quoteTransfer = $cartClientMock->changeItemQuantity('sku', null, 1);
158
159
        $this->assertInstanceOf(QuoteTransfer::class, $quoteTransfer);
160
    }
161
162
    /**
163
     * @return void
164
     */
165
    public function testChangeItemQuantityMustCallAddItemQuantityWhenPassedItemQuantityIsLowerThenInCartGivenItem(): void
166
    {
167
        $itemTransfer = new ItemTransfer();
168
        $itemTransfer->setQuantity(1);
169
        $itemTransfer->setSku('sku');
170
171
        $quoteTransfer = new QuoteTransfer();
172
        $quoteTransfer->addItem($itemTransfer);
173
174
        $quoteMock = $this->getQuoteMock();
175
        $quoteMock->expects($this->exactly(3))
176
            ->method('getQuote')
177
            ->will($this->returnValue($quoteTransfer));
178
179
        $stubMock = $this->getStubMock();
180
        $stubMock->expects($this->never())
181
            ->method('removeItem')
182
            ->will($this->returnValue($quoteTransfer));
183
184
        $stubMock->expects($this->once())
185
            ->method('addItem')
186
            ->will($this->returnValue($quoteTransfer));
187
        $sessionQuoteStorageStrategyPluginMock = $this->getSessionQuoteStorageStrategyPluginMock();
188
        $factoryMock = $this->getFactoryMock($quoteMock, $stubMock, $sessionQuoteStorageStrategyPluginMock);
189
        $cartClientMock = $this->getCartClientMock($factoryMock);
190
191
        $itemTransfer = new ItemTransfer();
192
        $itemTransfer->setSku('sku');
193
194
        $quoteTransfer = $cartClientMock->changeItemQuantity('sku', null, 2);
195
196
        $this->assertInstanceOf(QuoteTransfer::class, $quoteTransfer);
197
    }
198
199
    /**
200
     * @return void
201
     */
202
    public function testGetItemCountReturnNumberOfItemsInCart(): void
203
    {
204
        $itemTransfer = new ItemTransfer();
205
        $itemTransfer->setQuantity(1);
206
        $itemTransfer->setSku('sku');
207
208
        $quoteTransfer = new QuoteTransfer();
209
        $quoteTransfer->addItem($itemTransfer);
210
211
        $mockBuilder = $this->getMockBuilder(CartClient::class);
212
        $mockBuilder->setMethods(['getQuote', 'getItemCounter']);
213
        /** @var \Spryker\Client\Cart\CartClientInterface|\PHPUnit\Framework\MockObject\MockObject $cartClientMock */
214
        $cartClientMock = $mockBuilder->getMock();
215
        $cartClientMock->method('getQuote')->willReturn($quoteTransfer);
216
        $cartClientMock->method('getItemCounter')->willReturn(new ItemCountPlugin());
217
218
        $this->assertSame(1, $cartClientMock->getItemCount());
219
    }
220
221
    /**
222
     * @return void
223
     */
224
    public function testExpandCartItemsWithGroupKeyPrefixWhenParamSeparateProductProvided(): void
225
    {
226
        // Arrange
227
        $cartChangeTransfer = $this->createCartChangeTransfer();
228
        $params = [static::PARAM_SEPARATE_PRODUCT => 1];
229
230
        // Act
231
        $cartChangeTransfer = $this->tester->getClient()->expandCartItemsWithGroupKeyPrefix($cartChangeTransfer, $params);
232
233
        // Assert
234
        /** @var \Generated\Shared\Transfer\ItemTransfer $expandCartItem */
235
        $expandCartItem = $cartChangeTransfer->getItems()->getIterator()->current();
236
        $this->assertNotNull($expandCartItem->getGroupKeyPrefix());
237
    }
238
239
    /**
240
     * @return void
241
     */
242
    public function testExpandCartItemsWithGroupKeyPrefixWhenParamSeparateProductNotProvided(): void
243
    {
244
        // Arrange
245
        $cartChangeTransfer = $this->createCartChangeTransfer();
246
247
        // Act
248
        $cartChangeTransfer = $this->tester->getClient()->expandCartItemsWithGroupKeyPrefix($cartChangeTransfer, []);
249
250
        // Assert
251
        /** @var \Generated\Shared\Transfer\ItemTransfer $expandCartItem */
252
        $expandCartItem = $cartChangeTransfer->getItems()->getIterator()->current();
253
        $this->assertNull($expandCartItem->getGroupKeyPrefix());
254
    }
255
256
    /**
257
     * @param \Spryker\Client\Cart\Dependency\Client\CartToQuoteInterface|null $quote
258
     * @param \Spryker\Client\Cart\Zed\CartStubInterface|null $cartStub
259
     * @param \Spryker\Client\CartExtension\Dependency\Plugin\QuoteStorageStrategyPluginInterface|null $quoteStorageStrategyPlugin
260
     *
261
     * @return \Spryker\Client\Kernel\AbstractFactory|\PHPUnit\Framework\MockObject\MockObject
262
     */
263
    private function getFactoryMock(
264
        ?CartToQuoteInterface $quote = null,
265
        ?CartStubInterface $cartStub = null,
266
        ?QuoteStorageStrategyPluginInterface $quoteStorageStrategyPlugin = null
267
    ): AbstractFactory {
268
        $factoryMock = $this->getMockBuilder(AbstractFactory::class)
269
            ->setMethods(['getQuoteClient', 'createZedStub', 'createQuoteStorageStrategyProxy', 'createCartChangeRequestExpander', 'getQuoteItemFinderPlugin'])
270
            ->disableOriginalConstructor()->getMock();
271
272
        if ($quote !== null) {
273
            $factoryMock->expects($this->any())
274
                ->method('getQuoteClient')
275
                ->will($this->returnValue($quote));
276
        }
277
        if ($cartStub !== null) {
278
            $factoryMock->expects($this->any())
279
                ->method('createZedStub')
280
                ->will($this->returnValue($cartStub));
281
        }
282
        if ($cartStub !== null) {
283
            $quoteStorageStrategyPlugin->expects($this->any())
0 ignored issues
show
The method expects() does not exist on null. ( Ignorable by Annotation )

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

283
            $quoteStorageStrategyPlugin->/** @scrutinizer ignore-call */ 
284
                                         expects($this->any())

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...
The method expects() does not exist on Spryker\Client\CartExten...StrategyPluginInterface. ( Ignorable by Annotation )

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

283
            $quoteStorageStrategyPlugin->/** @scrutinizer ignore-call */ 
284
                                         expects($this->any())

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...
284
                ->method('getFactory')
285
                ->will($this->returnValue($factoryMock));
286
            $factoryMock->expects($this->any())
287
                ->method('createQuoteStorageStrategyProxy')
288
                ->will($this->returnValue($quoteStorageStrategyPlugin));
289
        }
290
291
        $factoryMock->expects($this->any())
292
            ->method('createCartChangeRequestExpander')
293
            ->will($this->returnValue(new CartChangeRequestExpander([], [])));
294
295
        $factoryMock->expects($this->any())
296
            ->method('getQuoteItemFinderPlugin')
297
            ->will($this->returnValue(new SimpleProductQuoteItemFinderPlugin()));
298
299
        return $factoryMock;
300
    }
301
302
    /**
303
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\CartExtension\Dependency\Plugin\QuoteStorageStrategyPluginInterface
304
     */
305
    private function getSessionQuoteStorageStrategyPluginMock(): QuoteStorageStrategyPluginInterface
306
    {
307
        $sessionQuoteStorageStrategyPluginMock = $this->getMockBuilder(SessionQuoteStorageStrategyPlugin::class)
308
            ->setMethods(['getFactory'])->disableOriginalConstructor()->getMock();
309
310
        return $sessionQuoteStorageStrategyPluginMock;
311
    }
312
313
    /**
314
     * @param \Spryker\Client\Kernel\AbstractFactory|\PHPUnit\Framework\MockObject\MockObject $factoryMock
315
     *
316
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\Cart\CartClient
317
     */
318
    private function getCartClientMock($factoryMock): CartClient
319
    {
320
        $cartClientMock = $this->getMockBuilder(CartClient::class)->setMethods(['getFactory'])->disableOriginalConstructor()->getMock();
321
322
        $cartClientMock->expects($this->any())
323
            ->method('getFactory')
324
            ->will($this->returnValue($factoryMock));
325
326
        return $cartClientMock;
327
    }
328
329
    /**
330
     * @return \Spryker\Client\Cart\Dependency\Client\CartToQuoteInterface|\PHPUnit\Framework\MockObject\MockObject
331
     */
332
    private function getQuoteMock(): CartToQuoteInterface
333
    {
334
        $quoteMock = $this->getMockBuilder(CartToQuoteInterface::class)->setMethods([
335
            'getQuote',
336
            'setQuote',
337
            'clearQuote',
338
            'getStorageStrategy',
339
            'reloadItems',
340
            'isQuoteLocked',
341
            'lockQuote',
342
        ])->getMock();
343
344
        $quoteMock->method('getStorageStrategy')
345
            ->will($this->returnValue('session'));
346
347
        return $quoteMock;
348
    }
349
350
    /**
351
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\Cart\Zed\CartStubInterface
352
     */
353
    private function getStubMock(): CartStubInterface
354
    {
355
        return $this->getMockBuilder(CartStubInterface::class)->setMethods([
356
            'addValidItems',
357
            'addItem',
358
            'removeItem',
359
            'reloadItems',
360
            'changeItemQuantity',
361
            'validateQuote',
362
            'addFlashMessagesFromLastZedRequest',
363
            'addResponseMessagesToMessenger',
364
            'resetQuoteLock',
365
            'addToCart',
366
            'removeFromCart',
367
            'replaceItem',
368
        ])->getMock();
369
    }
370
371
    /**
372
     * @return \Generated\Shared\Transfer\CartChangeTransfer
373
     */
374
    protected function createCartChangeTransfer(): CartChangeTransfer
375
    {
376
        $cartItem = new ItemTransfer();
377
        $cartItem->setSku(static::ITEM_SKU);
378
379
        $quoteTransfer = new QuoteTransfer();
380
        $quoteTransfer->addItem($cartItem);
381
382
        $cartChangeTransfer = new CartChangeTransfer();
383
        $cartChangeTransfer->setQuote($quoteTransfer);
384
        $cartChangeTransfer->addItem($cartItem);
385
386
        return $cartChangeTransfer;
387
    }
388
}
389