1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Context\Setup; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Sylius\Behat\Service\SharedStorageInterface; |
17
|
|
|
use Sylius\Component\Core\Factory\PromotionActionFactoryInterface; |
18
|
|
|
use Sylius\Component\Core\Factory\PromotionRuleFactoryInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
20
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
21
|
|
|
use Sylius\Component\Core\Model\PromotionCouponInterface; |
22
|
|
|
use Sylius\Component\Core\Model\PromotionInterface; |
23
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
24
|
|
|
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface; |
25
|
|
|
use Sylius\Component\Promotion\Factory\PromotionCouponFactoryInterface; |
26
|
|
|
use Sylius\Component\Promotion\Model\PromotionActionInterface; |
27
|
|
|
use Sylius\Component\Promotion\Model\PromotionRuleInterface; |
28
|
|
|
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author Mateusz Zalewski <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
final class PromotionContext implements Context |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var SharedStorageInterface |
37
|
|
|
*/ |
38
|
|
|
private $sharedStorage; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var PromotionActionFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $actionFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var PromotionCouponFactoryInterface |
47
|
|
|
*/ |
48
|
|
|
private $couponFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var PromotionRuleFactoryInterface |
52
|
|
|
*/ |
53
|
|
|
private $ruleFactory; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var TestPromotionFactoryInterface |
57
|
|
|
*/ |
58
|
|
|
private $testPromotionFactory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var PromotionRepositoryInterface |
62
|
|
|
*/ |
63
|
|
|
private $promotionRepository; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ObjectManager |
67
|
|
|
*/ |
68
|
|
|
private $objectManager; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param SharedStorageInterface $sharedStorage |
72
|
|
|
* @param PromotionActionFactoryInterface $actionFactory |
73
|
|
|
* @param PromotionCouponFactoryInterface $couponFactory |
74
|
|
|
* @param PromotionRuleFactoryInterface $ruleFactory |
75
|
|
|
* @param TestPromotionFactoryInterface $testPromotionFactory |
76
|
|
|
* @param PromotionRepositoryInterface $promotionRepository |
77
|
|
|
* @param ObjectManager $objectManager |
78
|
|
|
*/ |
79
|
|
|
public function __construct( |
80
|
|
|
SharedStorageInterface $sharedStorage, |
81
|
|
|
PromotionActionFactoryInterface $actionFactory, |
82
|
|
|
PromotionCouponFactoryInterface $couponFactory, |
83
|
|
|
PromotionRuleFactoryInterface $ruleFactory, |
84
|
|
|
TestPromotionFactoryInterface $testPromotionFactory, |
85
|
|
|
PromotionRepositoryInterface $promotionRepository, |
86
|
|
|
ObjectManager $objectManager |
87
|
|
|
) { |
88
|
|
|
$this->sharedStorage = $sharedStorage; |
89
|
|
|
$this->actionFactory = $actionFactory; |
90
|
|
|
$this->couponFactory = $couponFactory; |
91
|
|
|
$this->ruleFactory = $ruleFactory; |
92
|
|
|
$this->testPromotionFactory = $testPromotionFactory; |
93
|
|
|
$this->promotionRepository = $promotionRepository; |
94
|
|
|
$this->objectManager = $objectManager; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @Given there is a promotion :promotionName |
99
|
|
|
* @Given there is a promotion :promotionName identified by :promotionCode code |
100
|
|
|
*/ |
101
|
|
|
public function thereIsPromotion($promotionName, $promotionCode = null) |
102
|
|
|
{ |
103
|
|
|
$promotion = $this->testPromotionFactory |
104
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
if (null !== $promotionCode) { |
108
|
|
|
$promotion->setCode($promotionCode); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->promotionRepository->add($promotion); |
112
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
117
|
|
|
*/ |
118
|
|
|
public function thereIsAPromotionWithPriority($promotionName, $priority) |
119
|
|
|
{ |
120
|
|
|
$promotion = $this->testPromotionFactory |
121
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
122
|
|
|
; |
123
|
|
|
|
124
|
|
|
$promotion->setPriority($priority); |
125
|
|
|
|
126
|
|
|
$this->promotionRepository->add($promotion); |
127
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
132
|
|
|
*/ |
133
|
|
|
public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
134
|
|
|
{ |
135
|
|
|
$promotion = $this->testPromotionFactory |
136
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
137
|
|
|
; |
138
|
|
|
|
139
|
|
|
$promotion->setExclusive(true); |
140
|
|
|
$promotion->setPriority($priority); |
141
|
|
|
|
142
|
|
|
$this->promotionRepository->add($promotion); |
143
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @Given there is a promotion :promotionName limited to :usageLimit usages |
148
|
|
|
*/ |
149
|
|
|
public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
150
|
|
|
{ |
151
|
|
|
$promotion = $this->testPromotionFactory->createForChannel($promotionName, $this->sharedStorage->get('channel')); |
152
|
|
|
|
153
|
|
|
$promotion->setUsageLimit($usageLimit); |
154
|
|
|
|
155
|
|
|
$this->promotionRepository->add($promotion); |
156
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @Given the store has promotion :promotionName with coupon :couponCode |
161
|
|
|
* @Given the store has also promotion :promotionName with coupon :couponCode |
162
|
|
|
* @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
163
|
|
|
*/ |
164
|
|
|
public function thereIsPromotionWithCoupon($promotionName, $couponCode, $usageLimit = null) |
165
|
|
|
{ |
166
|
|
|
/** @var PromotionCouponInterface $coupon */ |
167
|
|
|
$coupon = $this->couponFactory->createNew(); |
168
|
|
|
$coupon->setCode($couponCode); |
169
|
|
|
$coupon->setUsageLimit($usageLimit); |
170
|
|
|
|
171
|
|
|
$promotion = $this->testPromotionFactory |
172
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
173
|
|
|
; |
174
|
|
|
$promotion->addCoupon($coupon); |
175
|
|
|
$promotion->setCouponBased(true); |
176
|
|
|
|
177
|
|
|
$this->promotionRepository->add($promotion); |
178
|
|
|
|
179
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
180
|
|
|
$this->sharedStorage->set('coupon', $coupon); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @Given /^(this promotion) has already expired$/ |
185
|
|
|
*/ |
186
|
|
|
public function thisPromotionHasExpired(PromotionInterface $promotion) |
187
|
|
|
{ |
188
|
|
|
$promotion->setEndsAt(new \DateTime('1 day ago')); |
189
|
|
|
|
190
|
|
|
$this->objectManager->flush(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @Given /^(this coupon) has already expired$/ |
195
|
|
|
*/ |
196
|
|
|
public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
197
|
|
|
{ |
198
|
|
|
$coupon->setExpiresAt(new \DateTime('1 day ago')); |
199
|
|
|
|
200
|
|
|
$this->objectManager->flush(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @Given /^(this coupon) expires tomorrow$/ |
205
|
|
|
*/ |
206
|
|
|
public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
207
|
|
|
{ |
208
|
|
|
$coupon->setExpiresAt(new \DateTime('tomorrow')); |
209
|
|
|
|
210
|
|
|
$this->objectManager->flush(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @Given /^(this coupon) has already reached its usage limit$/ |
215
|
|
|
*/ |
216
|
|
|
public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
217
|
|
|
{ |
218
|
|
|
$coupon->setUsed(42); |
219
|
|
|
$coupon->setUsageLimit(42); |
220
|
|
|
|
221
|
|
|
$this->objectManager->flush(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @Given /^(this coupon) can be used (\d+) times?$/ |
226
|
|
|
*/ |
227
|
|
|
public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
228
|
|
|
{ |
229
|
|
|
$coupon->setUsageLimit($usageLimit); |
230
|
|
|
|
231
|
|
|
$this->objectManager->flush(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @Given /^(this coupon) can be used twice per customer$/ |
236
|
|
|
*/ |
237
|
|
|
public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
238
|
|
|
{ |
239
|
|
|
$coupon->setPerCustomerUsageLimit(2); |
240
|
|
|
|
241
|
|
|
$this->objectManager->flush(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
246
|
|
|
*/ |
247
|
|
|
public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
248
|
|
|
{ |
249
|
|
|
$this->createFixedPromotion($promotion, $discount); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
254
|
|
|
*/ |
255
|
|
|
public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
256
|
|
|
{ |
257
|
|
|
$this->createPercentagePromotion($promotion, $discount); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
262
|
|
|
*/ |
263
|
|
|
public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
264
|
|
|
PromotionInterface $promotion, |
265
|
|
|
$discount, |
266
|
|
|
$quantity |
267
|
|
|
) { |
268
|
|
|
$rule = $this->ruleFactory->createCartQuantity((int) $quantity); |
269
|
|
|
|
270
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
275
|
|
|
*/ |
276
|
|
|
public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
277
|
|
|
PromotionInterface $promotion, |
278
|
|
|
$discount, |
279
|
|
|
$targetAmount |
280
|
|
|
) { |
281
|
|
|
$rule = $this->ruleFactory->createItemTotal($targetAmount); |
282
|
|
|
|
283
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
288
|
|
|
*/ |
289
|
|
|
public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
290
|
|
|
PromotionInterface $promotion, |
291
|
|
|
$discount, |
292
|
|
|
$targetAmount |
293
|
|
|
) { |
294
|
|
|
$rule = $this->ruleFactory->createItemTotal($targetAmount); |
295
|
|
|
|
296
|
|
|
$this->createUnitPercentagePromotion($promotion, $discount, [], $rule); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
301
|
|
|
*/ |
302
|
|
|
public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
303
|
|
|
{ |
304
|
|
|
$action = $this->actionFactory->createShippingPercentageDiscount($discount); |
305
|
|
|
$promotion->addAction($action); |
306
|
|
|
|
307
|
|
|
$this->objectManager->flush(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @Given /^([^"]+) gives free shipping to every order$/ |
312
|
|
|
*/ |
313
|
|
|
public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
314
|
|
|
{ |
315
|
|
|
$this->itGivesPercentageDiscountOnShippingToEveryOrder($promotion, 1); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
320
|
|
|
*/ |
321
|
|
|
public function itGivesPercentageOffEveryProductClassifiedAs( |
322
|
|
|
PromotionInterface $promotion, |
323
|
|
|
$discount, |
324
|
|
|
TaxonInterface $taxon |
325
|
|
|
) { |
326
|
|
|
$this->createUnitPercentagePromotion($promotion, $discount, $this->getTaxonFilterConfiguration([$taxon->getCode()])); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
331
|
|
|
*/ |
332
|
|
|
public function itGivesFixedOffEveryProductClassifiedAs( |
333
|
|
|
PromotionInterface $promotion, |
334
|
|
|
$discount, |
335
|
|
|
TaxonInterface $taxon |
336
|
|
|
) { |
337
|
|
|
$this->createUnitFixedPromotion($promotion, $discount, $this->getTaxonFilterConfiguration([$taxon->getCode()])); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
342
|
|
|
*/ |
343
|
|
|
public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
344
|
|
|
PromotionInterface $promotion, |
345
|
|
|
$discount, |
346
|
|
|
$amount |
347
|
|
|
) { |
348
|
|
|
$this->createUnitFixedPromotion($promotion, $discount, $this->getPriceRangeFilterConfiguration($amount)); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
353
|
|
|
*/ |
354
|
|
|
public function thisPromotionGivesOffOnEveryProductPricedBetween( |
355
|
|
|
PromotionInterface $promotion, |
356
|
|
|
$discount, |
357
|
|
|
$minAmount, |
358
|
|
|
$maxAmount |
359
|
|
|
) { |
360
|
|
|
$this->createUnitFixedPromotion( |
361
|
|
|
$promotion, |
362
|
|
|
$discount, |
363
|
|
|
$this->getPriceRangeFilterConfiguration($minAmount, $maxAmount) |
364
|
|
|
); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
369
|
|
|
*/ |
370
|
|
|
public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
371
|
|
|
PromotionInterface $promotion, |
372
|
|
|
$discount, |
373
|
|
|
$amount |
374
|
|
|
) { |
375
|
|
|
$this->createUnitPercentagePromotion($promotion, $discount, $this->getPriceRangeFilterConfiguration($amount)); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
380
|
|
|
*/ |
381
|
|
|
public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
382
|
|
|
PromotionInterface $promotion, |
383
|
|
|
$discount, |
384
|
|
|
$minAmount, |
385
|
|
|
$maxAmount |
386
|
|
|
) { |
387
|
|
|
$this->createUnitPercentagePromotion( |
388
|
|
|
$promotion, |
389
|
|
|
$discount, |
390
|
|
|
$this->getPriceRangeFilterConfiguration($minAmount, $maxAmount) |
391
|
|
|
); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
396
|
|
|
*/ |
397
|
|
|
public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
398
|
|
|
PromotionInterface $promotion, |
399
|
|
|
$discount, |
400
|
|
|
TaxonInterface $taxon |
401
|
|
|
) { |
402
|
|
|
$rule = $this->ruleFactory->createHasTaxon([$taxon->getCode()]); |
403
|
|
|
|
404
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
409
|
|
|
*/ |
410
|
|
|
public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
411
|
|
|
PromotionInterface $promotion, |
412
|
|
|
$discount, |
413
|
|
|
array $taxons |
414
|
|
|
) { |
415
|
|
|
$rule = $this->ruleFactory->createHasTaxon([$taxons[0]->getCode(), $taxons[1]->getCode()]); |
416
|
|
|
|
417
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
422
|
|
|
*/ |
423
|
|
|
public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
424
|
|
|
PromotionInterface $promotion, |
425
|
|
|
$discount, |
426
|
|
|
TaxonInterface $taxon, |
427
|
|
|
$amount |
428
|
|
|
) { |
429
|
|
|
$rule = $this->ruleFactory->createItemsFromTaxonTotal($taxon->getCode(), $amount); |
430
|
|
|
|
431
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
436
|
|
|
*/ |
437
|
|
|
public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
438
|
|
|
{ |
439
|
|
|
$rule = $this->ruleFactory->createNthOrder((int) $nth); |
440
|
|
|
|
441
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
446
|
|
|
*/ |
447
|
|
|
public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
448
|
|
|
{ |
449
|
|
|
$rule = $this->ruleFactory->createNthOrder((int) $nth); |
450
|
|
|
|
451
|
|
|
$this->createPercentagePromotion($promotion, $discount, [], $rule); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
456
|
|
|
*/ |
457
|
|
|
public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
458
|
|
|
PromotionInterface $promotion, |
459
|
|
|
$productDiscount, |
460
|
|
|
TaxonInterface $discountTaxon, |
461
|
|
|
$orderDiscount |
462
|
|
|
) { |
463
|
|
|
$this->createUnitPercentagePromotion($promotion, $productDiscount, $this->getTaxonFilterConfiguration([$discountTaxon->getCode()])); |
464
|
|
|
$this->createFixedPromotion($promotion, $orderDiscount); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
469
|
|
|
*/ |
470
|
|
|
public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
471
|
|
|
PromotionInterface $promotion, |
472
|
|
|
$discount, |
473
|
|
|
TaxonInterface $taxon, |
|
|
|
|
474
|
|
|
$targetAmount |
475
|
|
|
) { |
476
|
|
|
$freeShippingAction = $this->actionFactory->createShippingPercentageDiscount(1); |
477
|
|
|
$promotion->addAction($freeShippingAction); |
478
|
|
|
|
479
|
|
|
$rule = $this->ruleFactory->createItemTotal($targetAmount); |
480
|
|
|
|
481
|
|
|
$this->createUnitFixedPromotion($promotion, $discount, [], $rule); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
486
|
|
|
*/ |
487
|
|
|
public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
488
|
|
|
PromotionInterface $promotion, |
489
|
|
|
$taxonDiscount, |
490
|
|
|
TaxonInterface $taxon, |
491
|
|
|
$orderDiscount, |
492
|
|
|
$targetAmount |
493
|
|
|
) { |
494
|
|
|
$orderDiscountAction = $this->actionFactory->createFixedDiscount($orderDiscount); |
495
|
|
|
$promotion->addAction($orderDiscountAction); |
496
|
|
|
|
497
|
|
|
$rule = $this->ruleFactory->createItemTotal($targetAmount); |
498
|
|
|
|
499
|
|
|
$this->createUnitPercentagePromotion( |
500
|
|
|
$promotion, |
501
|
|
|
$taxonDiscount, |
502
|
|
|
$this->getTaxonFilterConfiguration([$taxon->getCode()]), |
503
|
|
|
$rule |
504
|
|
|
); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
509
|
|
|
*/ |
510
|
|
|
public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
511
|
|
|
PromotionInterface $promotion, |
512
|
|
|
$discount, |
513
|
|
|
array $discountTaxons, |
514
|
|
|
array $targetTaxons |
515
|
|
|
) { |
516
|
|
|
$discountTaxonsCodes = [$discountTaxons[0]->getCode(), $discountTaxons[1]->getCode()]; |
517
|
|
|
$targetTaxonsCodes = [$targetTaxons[0]->getCode(), $targetTaxons[1]->getCode()]; |
518
|
|
|
|
519
|
|
|
$rule = $this->ruleFactory->createHasTaxon($targetTaxonsCodes); |
520
|
|
|
|
521
|
|
|
$this->createUnitPercentagePromotion( |
522
|
|
|
$promotion, |
523
|
|
|
$discount, |
524
|
|
|
$this->getTaxonFilterConfiguration($discountTaxonsCodes), |
525
|
|
|
$rule |
526
|
|
|
); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
531
|
|
|
*/ |
532
|
|
|
public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
533
|
|
|
PromotionInterface $promotion, |
534
|
|
|
$discount, |
535
|
|
|
$discountTaxon, |
536
|
|
|
$targetTaxon |
537
|
|
|
) { |
538
|
|
|
$rule = $this->ruleFactory->createHasTaxon([$targetTaxon->getCode()]); |
539
|
|
|
|
540
|
|
|
$this->createUnitPercentagePromotion( |
541
|
|
|
$promotion, |
542
|
|
|
$discount, |
543
|
|
|
$this->getTaxonFilterConfiguration([$discountTaxon->getCode()]), |
544
|
|
|
$rule |
545
|
|
|
); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* @Given /^(it) is coupon based promotion$/ |
550
|
|
|
*/ |
551
|
|
|
public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
552
|
|
|
{ |
553
|
|
|
$promotion->setCouponBased(true); |
554
|
|
|
|
555
|
|
|
$this->objectManager->flush(); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
560
|
|
|
*/ |
561
|
|
|
public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
562
|
|
|
{ |
563
|
|
|
$promotion->removeChannel($channel); |
564
|
|
|
|
565
|
|
|
$this->objectManager->flush(); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
570
|
|
|
*/ |
571
|
|
|
public function theCouponWasUsed(PromotionCouponInterface $coupon) |
572
|
|
|
{ |
573
|
|
|
$coupon->setUsed($coupon->getUsageLimit()); |
574
|
|
|
|
575
|
|
|
$this->objectManager->flush(); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
580
|
|
|
*/ |
581
|
|
|
public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
582
|
|
|
{ |
583
|
|
|
$rule = $this->ruleFactory->createContainsProduct($product->getCode()); |
584
|
|
|
|
585
|
|
|
$this->createFixedPromotion($promotion, $discount, [], $rule); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
590
|
|
|
*/ |
591
|
|
|
public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
592
|
|
|
{ |
593
|
|
|
$this->createUnitFixedPromotion($promotion, $discount, $this->getProductsFilterConfiguration([$product->getCode()])); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
598
|
|
|
*/ |
599
|
|
|
public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
600
|
|
|
{ |
601
|
|
|
$this->createUnitPercentagePromotion($promotion, $discount, $this->getProductsFilterConfiguration([$product->getCode()])); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* @param array $taxonCodes |
606
|
|
|
* |
607
|
|
|
* @return array |
|
|
|
|
608
|
|
|
*/ |
609
|
|
|
private function getTaxonFilterConfiguration(array $taxonCodes) |
610
|
|
|
{ |
611
|
|
|
return ['filters' => ['taxons_filter' => ['taxons' => $taxonCodes]]]; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* @param array $productCodes |
616
|
|
|
* |
617
|
|
|
* @return array |
|
|
|
|
618
|
|
|
*/ |
619
|
|
|
private function getProductsFilterConfiguration(array $productCodes) |
620
|
|
|
{ |
621
|
|
|
return ['filters' => ['products_filter' => ['products' => $productCodes]]]; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* @param int $minAmount |
626
|
|
|
* @param int $maxAmount |
|
|
|
|
627
|
|
|
* |
628
|
|
|
* @return array |
|
|
|
|
629
|
|
|
*/ |
630
|
|
|
private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
631
|
|
|
{ |
632
|
|
|
$configuration = ['filters' => ['price_range_filter' => ['min' => $minAmount]]]; |
633
|
|
|
if (null !== $maxAmount) { |
634
|
|
|
$configuration['filters']['price_range_filter']['max'] = $maxAmount; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
return $configuration; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @param PromotionInterface $promotion |
642
|
|
|
* @param int $discount |
643
|
|
|
* @param array $configuration |
644
|
|
|
*/ |
645
|
|
|
private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
646
|
|
|
{ |
647
|
|
|
$this->persistPromotion($promotion, $this->actionFactory->createUnitFixedDiscount($discount), $configuration, $rule); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* @param PromotionInterface $promotion |
652
|
|
|
* @param int $discount |
653
|
|
|
* @param array $configuration |
654
|
|
|
*/ |
655
|
|
|
private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
656
|
|
|
{ |
657
|
|
|
$this->persistPromotion($promotion, $this->actionFactory->createUnitPercentageDiscount($discount), $configuration, $rule); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* @param PromotionInterface $promotion |
662
|
|
|
* @param int $discount |
663
|
|
|
* @param array $configuration |
664
|
|
|
* @param PromotionRuleInterface $rule |
|
|
|
|
665
|
|
|
*/ |
666
|
|
|
private function createFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
667
|
|
|
{ |
668
|
|
|
$this->persistPromotion($promotion, $this->actionFactory->createFixedDiscount($discount), $configuration, $rule); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* @param PromotionInterface $promotion |
673
|
|
|
* @param float $discount |
674
|
|
|
* @param array $configuration |
675
|
|
|
* @param PromotionRuleInterface $rule |
|
|
|
|
676
|
|
|
*/ |
677
|
|
|
private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
678
|
|
|
{ |
679
|
|
|
$this->persistPromotion($promotion, $this->actionFactory->createPercentageDiscount($discount), $configuration, $rule); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* @param PromotionInterface $promotion |
684
|
|
|
* @param PromotionActionInterface $action |
685
|
|
|
* @param array $configuration |
686
|
|
|
* @param PromotionRuleInterface|null $rule |
687
|
|
|
*/ |
688
|
|
|
private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
689
|
|
|
{ |
690
|
|
|
$configuration = array_merge($configuration, $action->getConfiguration()); |
691
|
|
|
$action->setConfiguration($configuration); |
692
|
|
|
|
693
|
|
|
$promotion->addAction($action); |
694
|
|
|
if (null !== $rule) { |
695
|
|
|
$promotion->addRule($rule); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
$this->objectManager->flush(); |
699
|
|
|
} |
700
|
|
|
} |
701
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.