Failed Conditions
Push — master ( e0fde8...98a030 )
by
unknown
44:34 queued 15:21
created

testAttachSspAssetToQuoteItemWithSessionStorageStrategySuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 24
rs 9.8333
c 1
b 0
f 0
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 SprykerFeatureTest\Client\SelfServicePortal\Asset\Quote;
9
10
use ArrayObject;
11
use Codeception\Test\Unit;
12
use Generated\Shared\Transfer\CustomerTransfer;
13
use Generated\Shared\Transfer\ItemTransfer;
14
use Generated\Shared\Transfer\QuoteResponseTransfer;
15
use Generated\Shared\Transfer\QuoteTransfer;
16
use Generated\Shared\Transfer\SspAssetQuoteItemAttachmentRequestTransfer;
17
use Generated\Shared\Transfer\SspAssetTransfer;
18
use Spryker\Client\Quote\QuoteClientInterface;
19
use SprykerFeature\Client\SelfServicePortal\Asset\Quote\DatabaseQuoteStorageStrategy;
20
use SprykerFeature\Client\SelfServicePortal\Asset\Quote\QuoteItemFinderInterface;
21
use SprykerFeature\Client\SelfServicePortal\Asset\Quote\QuoteStorageStrategyInterface;
22
use SprykerFeature\Client\SelfServicePortal\Asset\Quote\SessionQuoteStorageStrategy;
23
use SprykerFeature\Client\SelfServicePortal\SelfServicePortalClient;
24
use SprykerFeature\Client\SelfServicePortal\SelfServicePortalFactory;
25
use SprykerFeature\Client\SelfServicePortal\Zed\SelfServicePortalStubInterface;
26
27
/**
28
 * Auto-generated group annotations
29
 *
30
 * @group SprykerFeatureTest
31
 * @group Client
32
 * @group SelfServicePortal
33
 * @group Asset
34
 * @group Quote
35
 * @group AttachSspAssetToQuoteItemTest
36
 * Add your own group annotations below this line
37
 */
38
class AttachSspAssetToQuoteItemTest extends Unit
39
{
40
    /**
41
     * @var string
42
     */
43
    protected const TEST_SSP_ASSET_REFERENCE = 'TEST_ASSET_001';
44
45
    /**
46
     * @var string
47
     */
48
    protected const TEST_SKU = 'TEST_SKU_001';
49
50
    /**
51
     * @var string
52
     */
53
    protected const TEST_GROUP_KEY = 'TEST_GROUP_001';
54
55
    /**
56
     * @var int
57
     */
58
    protected const TEST_QUOTE_ID = 1;
59
60
    /**
61
     * @var \SprykerFeatureTest\Client\SelfServicePortal\SelfServicePortalClientTester
62
     */
63
    protected $tester;
64
65
    public function testAttachSspAssetToQuoteItemWithSessionStorageStrategySuccess(): void
66
    {
67
        // Arrange
68
        $quoteTransfer = $this->createQuoteTransferWithItem();
69
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
70
71
        $quoteClientMock = $this->createQuoteClientMock();
72
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spryker\Client\Quote\QuoteClientInterface. ( Ignorable by Annotation )

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

72
        $quoteClientMock->/** @scrutinizer ignore-call */ 
73
                          method('getQuote')->willReturn($quoteTransfer);

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...
73
        $quoteClientMock->method('setQuote')->willReturnSelf();
74
75
        $quoteItemFinderPluginMock = $this->createQuoteItemFinderPluginMock();
76
        $quoteItemFinderPluginMock->method('findItem')->willReturn($quoteTransfer->getItems()->getIterator()->current());
0 ignored issues
show
Bug introduced by
The method method() does not exist on SprykerFeature\Client\Se...uoteItemFinderInterface. ( Ignorable by Annotation )

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

76
        $quoteItemFinderPluginMock->/** @scrutinizer ignore-call */ 
77
                                    method('findItem')->willReturn($quoteTransfer->getItems()->getIterator()->current());

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...
77
78
        $sessionQuoteStorageStrategy = new SessionQuoteStorageStrategy($quoteClientMock, $quoteItemFinderPluginMock);
79
80
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($sessionQuoteStorageStrategy);
81
82
        // Act
83
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
84
85
        // Assert
86
        $this->assertTrue($quoteResponseTransfer->getIsSuccessful());
87
        $this->assertNotNull($quoteTransfer->getItems()->getIterator()->current()->getSspAsset());
88
        $this->assertEquals(static::TEST_SSP_ASSET_REFERENCE, $quoteTransfer->getItems()->getIterator()->current()->getSspAsset()->getReference());
89
    }
90
91
    public function testAttachSspAssetToQuoteItemWithSessionStorageStrategyItemNotFound(): void
92
    {
93
        // Arrange
94
        $quoteTransfer = $this->createQuoteTransferWithItem();
95
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
96
97
        $quoteClientMock = $this->createQuoteClientMock();
98
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
99
100
        $quoteItemFinderPluginMock = $this->createQuoteItemFinderPluginMock();
101
        $quoteItemFinderPluginMock->method('findItem')->willReturn(null);
102
103
        $sessionQuoteStorageStrategy = new SessionQuoteStorageStrategy($quoteClientMock, $quoteItemFinderPluginMock);
104
105
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($sessionQuoteStorageStrategy);
106
107
        // Act
108
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
109
110
        // Assert
111
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
112
    }
113
114
    public function testAttachSspAssetToQuoteItemWithSessionStorageStrategyRemoveAsset(): void
115
    {
116
        // Arrange
117
        $quoteTransfer = $this->createQuoteTransferWithItem();
118
        $quoteTransfer->getItems()->getIterator()->current()->setSspAsset(
119
            (new SspAssetTransfer())->setReference(static::TEST_SSP_ASSET_REFERENCE),
120
        );
121
122
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
123
        $sspAssetQuoteItemAttachmentRequestTransfer->setSspAssetReference(null);
124
125
        $quoteClientMock = $this->createQuoteClientMock();
126
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
127
        $quoteClientMock->method('setQuote')->willReturnSelf();
128
129
        $quoteItemFinderPluginMock = $this->createQuoteItemFinderPluginMock();
130
        $quoteItemFinderPluginMock->method('findItem')->willReturn($quoteTransfer->getItems()->getIterator()->current());
131
132
        $sessionQuoteStorageStrategy = new SessionQuoteStorageStrategy($quoteClientMock, $quoteItemFinderPluginMock);
133
134
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($sessionQuoteStorageStrategy);
135
136
        // Act
137
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
138
139
        // Assert
140
        $this->assertTrue($quoteResponseTransfer->getIsSuccessful());
141
        $this->assertNull($quoteTransfer->getItems()->getIterator()->current()->getSspAsset());
142
    }
143
144
    public function testAttachSspAssetToQuoteItemWithDatabaseStorageStrategySuccess(): void
145
    {
146
        // Arrange
147
        $quoteTransfer = $this->createQuoteTransferWithItem();
148
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
149
150
        $quoteClientMock = $this->createQuoteClientMock();
151
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
152
        $quoteClientMock->method('setQuote')->willReturnSelf();
153
154
        $expectedQuoteResponseTransfer = new QuoteResponseTransfer();
155
        $expectedQuoteResponseTransfer->setIsSuccessful(true);
156
        $expectedQuoteResponseTransfer->setQuoteTransfer($quoteTransfer);
157
158
        $selfServicePortalStubMock = $this->createSelfServicePortalStubMock();
159
        $selfServicePortalStubMock->method('attachSspAssetToQuoteItem')->willReturn($expectedQuoteResponseTransfer);
0 ignored issues
show
Bug introduced by
The method method() does not exist on SprykerFeature\Client\Se...vicePortalStubInterface. ( Ignorable by Annotation )

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

159
        $selfServicePortalStubMock->/** @scrutinizer ignore-call */ 
160
                                    method('attachSspAssetToQuoteItem')->willReturn($expectedQuoteResponseTransfer);

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...
160
161
        $databaseQuoteStorageStrategy = new DatabaseQuoteStorageStrategy($quoteClientMock, $selfServicePortalStubMock);
162
163
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($databaseQuoteStorageStrategy);
164
165
        // Act
166
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
167
168
        // Assert
169
        $this->assertTrue($quoteResponseTransfer->getIsSuccessful());
170
    }
171
172
    public function testAttachSspAssetToQuoteItemWithDatabaseStorageStrategyNoCustomer(): void
173
    {
174
        // Arrange
175
        $quoteTransfer = $this->createQuoteTransferWithItem();
176
        $quoteTransfer->setCustomer(null);
177
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
178
179
        $quoteClientMock = $this->createQuoteClientMock();
180
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
181
182
        $selfServicePortalStubMock = $this->createSelfServicePortalStubMock();
183
184
        $databaseQuoteStorageStrategy = new DatabaseQuoteStorageStrategy($quoteClientMock, $selfServicePortalStubMock);
185
186
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($databaseQuoteStorageStrategy);
187
188
        // Act
189
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
190
191
        // Assert
192
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
193
    }
194
195
    public function testAttachSspAssetToQuoteItemWithDatabaseStorageStrategyStubFailure(): void
196
    {
197
        // Arrange
198
        $quoteTransfer = $this->createQuoteTransferWithItem();
199
        $sspAssetQuoteItemAttachmentRequestTransfer = $this->createSspAssetQuoteItemAttachmentRequestTransfer();
200
201
        $quoteClientMock = $this->createQuoteClientMock();
202
        $quoteClientMock->method('getQuote')->willReturn($quoteTransfer);
203
204
        $expectedQuoteResponseTransfer = new QuoteResponseTransfer();
205
        $expectedQuoteResponseTransfer->setIsSuccessful(false);
206
207
        $selfServicePortalStubMock = $this->createSelfServicePortalStubMock();
208
        $selfServicePortalStubMock->method('attachSspAssetToQuoteItem')->willReturn($expectedQuoteResponseTransfer);
209
210
        $databaseQuoteStorageStrategy = new DatabaseQuoteStorageStrategy($quoteClientMock, $selfServicePortalStubMock);
211
212
        $selfServicePortalClient = $this->createSelfServicePortalClientWithStorageStrategy($databaseQuoteStorageStrategy);
213
214
        // Act
215
        $quoteResponseTransfer = $selfServicePortalClient->attachSspAssetToQuoteItem($sspAssetQuoteItemAttachmentRequestTransfer);
216
217
        // Assert
218
        $this->assertFalse($quoteResponseTransfer->getIsSuccessful());
219
    }
220
221
    protected function createQuoteTransferWithItem(): QuoteTransfer
222
    {
223
        $itemTransfer = (new ItemTransfer())
224
            ->setSku(static::TEST_SKU)
225
            ->setGroupKey(static::TEST_GROUP_KEY);
226
227
        $customerTransfer = (new CustomerTransfer())
228
            ->setIdCustomer(1);
229
230
        return (new QuoteTransfer())
231
            ->setIdQuote(static::TEST_QUOTE_ID)
232
            ->setCustomer($customerTransfer)
233
            ->setItems(new ArrayObject([$itemTransfer]));
234
    }
235
236
    protected function createSspAssetQuoteItemAttachmentRequestTransfer(): SspAssetQuoteItemAttachmentRequestTransfer
237
    {
238
        $itemTransfer = (new ItemTransfer())
239
            ->setSku(static::TEST_SKU)
240
            ->setGroupKey(static::TEST_GROUP_KEY);
241
242
        return (new SspAssetQuoteItemAttachmentRequestTransfer())
243
            ->setSspAssetReference(static::TEST_SSP_ASSET_REFERENCE)
244
            ->setItem($itemTransfer);
245
    }
246
247
    /**
248
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\Quote\QuoteClientInterface
249
     */
250
    protected function createQuoteClientMock(): QuoteClientInterface
251
    {
252
        return $this->createMock(QuoteClientInterface::class);
253
    }
254
255
    /**
256
     * @return \PHPUnit\Framework\MockObject\MockObject|\SprykerFeature\Client\SelfServicePortal\Asset\Quote\QuoteItemFinderInterface
257
     */
258
    protected function createQuoteItemFinderPluginMock(): QuoteItemFinderInterface
259
    {
260
        return $this->createMock(QuoteItemFinderInterface::class);
261
    }
262
263
    /**
264
     * @return \PHPUnit\Framework\MockObject\MockObject|\SprykerFeature\Client\SelfServicePortal\Zed\SelfServicePortalStubInterface
265
     */
266
    protected function createSelfServicePortalStubMock(): SelfServicePortalStubInterface
267
    {
268
        return $this->createMock(SelfServicePortalStubInterface::class);
269
    }
270
271
    protected function createSelfServicePortalClientWithStorageStrategy(QuoteStorageStrategyInterface $storageStrategy): SelfServicePortalClient
272
    {
273
        $factoryMock = $this->createMock(SelfServicePortalFactory::class);
274
        $factoryMock->method('getQuoteStorageStrategy')->willReturn($storageStrategy);
275
276
        $selfServicePortalClient = new SelfServicePortalClient();
277
        $selfServicePortalClient->setFactory($factoryMock);
278
279
        return $selfServicePortalClient;
280
    }
281
}
282