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\Zed\Discount\Business\Facade; |
9
|
|
|
|
10
|
|
|
use Codeception\Test\Unit; |
11
|
|
|
use DateTime; |
12
|
|
|
use Generated\Shared\Transfer\MoneyValueTransfer; |
13
|
|
|
use Generated\Shared\Transfer\StoreTransfer; |
14
|
|
|
use Orm\Zed\Discount\Persistence\SpyDiscountQuery; |
15
|
|
|
use Spryker\Shared\Discount\DiscountConstants; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Auto-generated group annotations |
19
|
|
|
* |
20
|
|
|
* @group SprykerTest |
21
|
|
|
* @group Zed |
22
|
|
|
* @group Discount |
23
|
|
|
* @group Business |
24
|
|
|
* @group Facade |
25
|
|
|
* @group CreateDiscountTest |
26
|
|
|
* Add your own group annotations below this line |
27
|
|
|
*/ |
28
|
|
|
class CreateDiscountTest extends Unit |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @uses \Spryker\Zed\Discount\Persistence\Propel\Mapper\DiscountMapper::DATE_TIME_FORMAT |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected const DATE_TIME_FORMAT = 'Y-m-d H:i:s'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @uses \Spryker\Zed\Discount\DiscountDependencyProvider::PLUGIN_CALCULATOR_PERCENTAGE |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected const PLUGIN_CALCULATOR_PERCENTAGE = 'PLUGIN_CALCULATOR_PERCENTAGE'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected const STORE_NAME = 'DE'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \SprykerTest\Zed\Discount\DiscountBusinessTester |
51
|
|
|
*/ |
52
|
|
|
protected $tester; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function testWillPersistAllConfiguredData(): void |
58
|
|
|
{ |
59
|
|
|
// Arrange |
60
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
61
|
|
|
|
62
|
|
|
// Act |
63
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade() |
64
|
|
|
->createDiscount($discountConfiguratorTransfer); |
65
|
|
|
|
66
|
|
|
// Assert |
67
|
|
|
$this->assertTrue($discountConfiguratorResponseTransfer->getIsSuccessful()); |
68
|
|
|
$idDiscount = $discountConfiguratorResponseTransfer->getDiscountConfiguratorOrFail() |
69
|
|
|
->getDiscountGeneralOrFail() |
70
|
|
|
->getIdDiscountOrFail(); |
71
|
|
|
|
72
|
|
|
$discountEntity = $this->tester->findDiscountEntityById($idDiscount); |
73
|
|
|
$this->assertNotNull($discountEntity); |
74
|
|
|
|
75
|
|
|
$discountGeneralTransfer = $discountConfiguratorTransfer->getDiscountGeneral(); |
76
|
|
|
$this->assertSame($discountGeneralTransfer->getDisplayName(), $discountEntity->getDisplayName()); |
77
|
|
|
$this->assertSame($discountGeneralTransfer->getIsActive(), $discountEntity->getIsActive()); |
78
|
|
|
$this->assertSame($discountGeneralTransfer->getIsExclusive(), $discountEntity->getIsExclusive()); |
79
|
|
|
$this->assertSame($discountGeneralTransfer->getDescription(), $discountEntity->getDescription()); |
80
|
|
|
$this->assertSame($discountGeneralTransfer->getValidFrom(), $discountEntity->getValidFrom()->format(static::DATE_TIME_FORMAT)); |
81
|
|
|
$this->assertSame($discountGeneralTransfer->getValidTo(), $discountEntity->getValidTo()->format(static::DATE_TIME_FORMAT)); |
82
|
|
|
|
83
|
|
|
$discountCalculatorTransfer = $discountConfiguratorTransfer->getDiscountCalculator(); |
84
|
|
|
$this->assertSame($discountCalculatorTransfer->getAmount(), $discountEntity->getAmount()); |
85
|
|
|
$this->assertSame($discountCalculatorTransfer->getCalculatorPlugin(), $discountEntity->getCalculatorPlugin()); |
86
|
|
|
$this->assertSame($discountCalculatorTransfer->getCollectorQueryString(), $discountEntity->getCollectorQueryString()); |
87
|
|
|
|
88
|
|
|
$discountConditionTransfer = $discountConfiguratorTransfer->getDiscountCondition(); |
89
|
|
|
$this->assertSame($discountConditionTransfer->getDecisionRuleQueryString(), $discountEntity->getDecisionRuleQueryString()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function testWillCreateDiscountWithEmptyVoucherPool(): void |
96
|
|
|
{ |
97
|
|
|
// Arrange |
98
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
99
|
|
|
$discountConfiguratorTransfer->getDiscountGeneral() |
100
|
|
|
->setDiscountType(DiscountConstants::TYPE_VOUCHER); |
101
|
|
|
|
102
|
|
|
// Act |
103
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade() |
104
|
|
|
->createDiscount($discountConfiguratorTransfer); |
105
|
|
|
|
106
|
|
|
// Assert |
107
|
|
|
$this->assertTrue($discountConfiguratorResponseTransfer->getIsSuccessful()); |
108
|
|
|
$idDiscount = $discountConfiguratorResponseTransfer->getDiscountConfiguratorOrFail() |
109
|
|
|
->getDiscountGeneralOrFail() |
110
|
|
|
->getIdDiscountOrFail(); |
111
|
|
|
|
112
|
|
|
$discountEntity = $this->tester->findDiscountEntityById($idDiscount); |
113
|
|
|
$this->assertNotNull($discountEntity); |
114
|
|
|
|
115
|
|
|
$this->assertNotEmpty($discountEntity->getFkDiscountVoucherPool()); |
116
|
|
|
$discountVoucherPoolEntity = $discountEntity->getVoucherPool(); |
117
|
|
|
$this->assertNotEmpty($discountVoucherPoolEntity); |
118
|
|
|
$this->assertSame($discountConfiguratorTransfer->getDiscountGeneral()->getDisplayName(), $discountVoucherPoolEntity->getName()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function testWillPersisStoreRelation(): void |
125
|
|
|
{ |
126
|
|
|
// Arrange |
127
|
|
|
$storeTransfer = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME]); |
128
|
|
|
$storeIds = [$storeTransfer->getIdStore()]; |
129
|
|
|
|
130
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer($storeIds); |
131
|
|
|
|
132
|
|
|
// Act |
133
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
134
|
|
|
|
135
|
|
|
// Assert |
136
|
|
|
$this->assertTrue($discountConfiguratorResponseTransfer->getIsSuccessful()); |
137
|
|
|
|
138
|
|
|
$idDiscount = $discountConfiguratorResponseTransfer->getDiscountConfiguratorOrFail() |
139
|
|
|
->getDiscountGeneralOrFail() |
140
|
|
|
->getIdDiscountOrFail(); |
141
|
|
|
|
142
|
|
|
$discountStoreEntityCollection = $this->tester->getDiscountStoreEntityCollectionByIdDiscount($idDiscount); |
143
|
|
|
$this->assertCount(1, $discountStoreEntityCollection); |
144
|
|
|
$this->assertSame($storeTransfer->getIdStore(), $discountStoreEntityCollection[0]->getFkStore()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function testWillValidateDatesFormat(): void |
151
|
|
|
{ |
152
|
|
|
// Arrange |
153
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
154
|
|
|
$discountConfiguratorTransfer->getDiscountGeneral() |
155
|
|
|
->setValidFrom('NOT_A_DATE') |
156
|
|
|
->setValidTo('1234567890'); |
157
|
|
|
|
158
|
|
|
// Act |
159
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
160
|
|
|
|
161
|
|
|
// Assert |
162
|
|
|
$this->assertFalse($discountConfiguratorResponseTransfer->getIsSuccessful()); |
163
|
|
|
$this->assertCount(4, $discountConfiguratorResponseTransfer->getMessages()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function testWillValidateDatesMaxValue(): void |
170
|
|
|
{ |
171
|
|
|
// Arrange |
172
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
173
|
|
|
$discountConfiguratorTransfer->getDiscountGeneral() |
174
|
|
|
->setValidFrom((new DateTime('+1 day'))->format(static::DATE_TIME_FORMAT)) |
175
|
|
|
->setValidTo((new DateTime('2038-01-19 03:14:08'))->format(static::DATE_TIME_FORMAT)); |
176
|
|
|
|
177
|
|
|
// Act |
178
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
179
|
|
|
|
180
|
|
|
// Assert |
181
|
|
|
$this->assertFalse($discountConfiguratorResponseTransfer->getIsSuccessful()); |
182
|
|
|
$this->assertCount(1, $discountConfiguratorResponseTransfer->getMessages()); |
183
|
|
|
|
184
|
|
|
/** @var \Generated\Shared\Transfer\MessageTransfer $messageTransfer */ |
185
|
|
|
$messageTransfer = $discountConfiguratorResponseTransfer->getMessages()->offsetGet(0); |
186
|
|
|
$this->assertSame('Date cannot be later than {{ compared_value }}', $messageTransfer->getValue()); |
187
|
|
|
$this->assertArrayHasKey('{{ compared_value }}', $messageTransfer->getParameters()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public function testWillValidateDatePeriod(): void |
194
|
|
|
{ |
195
|
|
|
// Arrange |
196
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
197
|
|
|
$discountConfiguratorTransfer->getDiscountGeneral() |
198
|
|
|
->setValidFrom((new DateTime('+1 day'))->format(static::DATE_TIME_FORMAT)) |
199
|
|
|
->setValidTo((new DateTime('-1 day'))->format(static::DATE_TIME_FORMAT)); |
200
|
|
|
|
201
|
|
|
// Act |
202
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
203
|
|
|
|
204
|
|
|
// Assert |
205
|
|
|
$this->assertFalse($discountConfiguratorResponseTransfer->getIsSuccessful()); |
206
|
|
|
$this->assertCount(1, $discountConfiguratorResponseTransfer->getMessages()); |
207
|
|
|
|
208
|
|
|
/** @var \Generated\Shared\Transfer\MessageTransfer $messageTransfer */ |
209
|
|
|
$messageTransfer = $discountConfiguratorResponseTransfer->getMessages()->offsetGet(0); |
210
|
|
|
$this->assertSame('The discount cannot end before the starting date.', $messageTransfer->getValue()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
public function testCreateDiscountShouldNotAddDiscountAmountForPercentageType(): void |
217
|
|
|
{ |
218
|
|
|
// Arrange |
219
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
220
|
|
|
$discountConfiguratorTransfer->getDiscountCalculator() |
221
|
|
|
->setAmount(10) |
222
|
|
|
->setCalculatorPlugin(static::PLUGIN_CALCULATOR_PERCENTAGE); |
223
|
|
|
|
224
|
|
|
// Act |
225
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
226
|
|
|
$discountEntity = SpyDiscountQuery::create() |
227
|
|
|
->leftJoinWithDiscountAmount() |
228
|
|
|
->findByIdDiscount($discountConfiguratorResponseTransfer->getDiscountConfigurator()->getDiscountGeneral()->getIdDiscount()) |
229
|
|
|
->getFirst(); |
230
|
|
|
|
231
|
|
|
// Assert |
232
|
|
|
$this->assertTrue($discountConfiguratorResponseTransfer->getIsSuccessful()); |
233
|
|
|
$this->assertEmpty($discountEntity->getDiscountAmounts()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
public function testCreateDiscountShouldAddDiscountAmountForFixedType(): void |
240
|
|
|
{ |
241
|
|
|
// Arrange |
242
|
|
|
$currencyTransfer = $this->tester->haveCurrencyTransfer(); |
243
|
|
|
$discountConfiguratorTransfer = $this->tester->createDiscountConfiguratorTransfer(); |
244
|
|
|
|
245
|
|
|
$discountConfiguratorTransfer->getDiscountCalculator() |
246
|
|
|
->addMoneyValue( |
247
|
|
|
(new MoneyValueTransfer()) |
248
|
|
|
->setGrossAmount(50) |
249
|
|
|
->setFkCurrency($currencyTransfer->getIdCurrency()), |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
// Act |
253
|
|
|
$discountConfiguratorResponseTransfer = $this->tester->getFacade()->createDiscount($discountConfiguratorTransfer); |
254
|
|
|
$discountEntity = SpyDiscountQuery::create() |
255
|
|
|
->leftJoinWithDiscountAmount() |
256
|
|
|
->findByIdDiscount($discountConfiguratorResponseTransfer->getDiscountConfigurator()->getDiscountGeneral()->getIdDiscount()) |
257
|
|
|
->getFirst(); |
258
|
|
|
|
259
|
|
|
// Assert |
260
|
|
|
$this->assertTrue($discountConfiguratorResponseTransfer->getIsSuccessful()); |
261
|
|
|
$this->assertCount(1, $discountEntity->getDiscountAmounts()); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|