|
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\Component\Core\Factory\ActionFactoryInterface; |
|
17
|
|
|
use Sylius\Component\Core\Factory\RuleFactoryInterface; |
|
18
|
|
|
use Sylius\Component\Core\Model\PromotionInterface; |
|
19
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
20
|
|
|
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface; |
|
21
|
|
|
use Sylius\Component\Core\Test\Services\SharedStorageInterface; |
|
22
|
|
|
use Sylius\Component\Promotion\Factory\CouponFactoryInterface; |
|
23
|
|
|
use Sylius\Component\Promotion\Model\ActionInterface; |
|
24
|
|
|
use Sylius\Component\Promotion\Model\CouponInterface; |
|
25
|
|
|
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class PromotionContext implements Context |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var SharedStorageInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sharedStorage; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ActionFactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $actionFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var CouponFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $couponFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var RuleFactoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $ruleFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var TestPromotionFactoryInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
private $testPromotionFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var PromotionRepositoryInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
private $promotionRepository; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var ObjectManager |
|
64
|
|
|
*/ |
|
65
|
|
|
private $objectManager; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
69
|
|
|
* @param ActionFactoryInterface $actionFactory |
|
70
|
|
|
* @param CouponFactoryInterface $couponFactory |
|
71
|
|
|
* @param RuleFactoryInterface $ruleFactory |
|
72
|
|
|
* @param TestPromotionFactoryInterface $testPromotionFactory |
|
73
|
|
|
* @param PromotionRepositoryInterface $promotionRepository |
|
74
|
|
|
* @param ObjectManager $objectManager |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct( |
|
77
|
|
|
SharedStorageInterface $sharedStorage, |
|
78
|
|
|
ActionFactoryInterface $actionFactory, |
|
79
|
|
|
CouponFactoryInterface $couponFactory, |
|
80
|
|
|
RuleFactoryInterface $ruleFactory, |
|
81
|
|
|
TestPromotionFactoryInterface $testPromotionFactory, |
|
82
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
83
|
|
|
ObjectManager $objectManager |
|
84
|
|
|
) { |
|
85
|
|
|
$this->sharedStorage = $sharedStorage; |
|
86
|
|
|
$this->actionFactory = $actionFactory; |
|
87
|
|
|
$this->couponFactory = $couponFactory; |
|
88
|
|
|
$this->ruleFactory = $ruleFactory; |
|
89
|
|
|
$this->testPromotionFactory = $testPromotionFactory; |
|
90
|
|
|
$this->promotionRepository = $promotionRepository; |
|
91
|
|
|
$this->objectManager = $objectManager; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @Given there is a promotion :promotionName |
|
96
|
|
|
*/ |
|
97
|
|
|
public function thereIsPromotion($promotionName) |
|
98
|
|
|
{ |
|
99
|
|
|
$promotion = $this->testPromotionFactory |
|
100
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
|
101
|
|
|
; |
|
102
|
|
|
|
|
103
|
|
|
$this->promotionRepository->add($promotion); |
|
104
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @Given the store has promotion :promotionName with coupon :couponCode |
|
109
|
|
|
*/ |
|
110
|
|
|
public function thereIsPromotionWithCoupon($promotionName, $couponCode) |
|
111
|
|
|
{ |
|
112
|
|
|
/** @var CouponInterface $coupon */ |
|
113
|
|
|
$coupon = $this->couponFactory->createNew(); |
|
114
|
|
|
$coupon->setCode($couponCode); |
|
115
|
|
|
|
|
116
|
|
|
$promotion = $this->testPromotionFactory |
|
117
|
|
|
->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
|
118
|
|
|
; |
|
119
|
|
|
$promotion->addCoupon($coupon); |
|
120
|
|
|
$promotion->setCouponBased(true); |
|
121
|
|
|
|
|
122
|
|
|
$this->promotionRepository->add($promotion); |
|
123
|
|
|
$this->sharedStorage->set('promotion', $promotion); |
|
124
|
|
|
$this->sharedStorage->set('coupon', $coupon); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @Given /^([^"]+) gives ("[^"]+") fixed discount to every order$/ |
|
129
|
|
|
*/ |
|
130
|
|
|
public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $amount) |
|
131
|
|
|
{ |
|
132
|
|
|
$action = $this->actionFactory->createFixedDiscount($amount); |
|
133
|
|
|
$promotion->addAction($action); |
|
134
|
|
|
|
|
135
|
|
|
$this->objectManager->flush(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @Given /^([^"]+) gives ("[^"]+") percentage discount to every order$/ |
|
140
|
|
|
*/ |
|
141
|
|
|
public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
|
142
|
|
|
{ |
|
143
|
|
|
$action = $this->actionFactory->createPercentageDiscount($discount); |
|
144
|
|
|
$promotion->addAction($action); |
|
145
|
|
|
|
|
146
|
|
|
$this->objectManager->flush(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with quantity at least ([^"]+)$/ |
|
151
|
|
|
*/ |
|
152
|
|
|
public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast(PromotionInterface $promotion, $amount, $quantity) |
|
153
|
|
|
{ |
|
154
|
|
|
$action = $this->actionFactory->createFixedDiscount($amount); |
|
155
|
|
|
$promotion->addAction($action); |
|
156
|
|
|
|
|
157
|
|
|
$rule = $this->ruleFactory->createCartQuantity((int) $quantity); |
|
158
|
|
|
$promotion->addRule($rule); |
|
159
|
|
|
|
|
160
|
|
|
$this->objectManager->flush(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with items total at least ("[^"]+")$/ |
|
165
|
|
|
*/ |
|
166
|
|
|
public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
|
167
|
|
|
PromotionInterface $promotion, |
|
168
|
|
|
$amount, |
|
169
|
|
|
$targetAmount |
|
170
|
|
|
) { |
|
171
|
|
|
$action = $this->actionFactory->createFixedDiscount($amount); |
|
172
|
|
|
$promotion->addAction($action); |
|
173
|
|
|
|
|
174
|
|
|
$rule = $this->ruleFactory->createItemTotal($targetAmount); |
|
175
|
|
|
$promotion->addRule($rule); |
|
176
|
|
|
|
|
177
|
|
|
$this->objectManager->flush(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @Given /^([^"]+) gives ("[^"]+") percentage discount on shipping to every order$/ |
|
182
|
|
|
*/ |
|
183
|
|
|
public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
|
184
|
|
|
{ |
|
185
|
|
|
$action = $this->actionFactory->createPercentageShippingDiscount($discount); |
|
186
|
|
|
$promotion->addAction($action); |
|
187
|
|
|
|
|
188
|
|
|
$this->objectManager->flush(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
|
193
|
|
|
*/ |
|
194
|
|
|
public function itGivesPercentageOffEveryProductClassifiedAs( |
|
195
|
|
|
PromotionInterface $promotion, |
|
196
|
|
|
$discount, |
|
197
|
|
|
TaxonInterface $taxon |
|
198
|
|
|
) { |
|
199
|
|
|
$action = $this->actionFactory->createItemPercentageDiscount($discount); |
|
200
|
|
|
$promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()])); |
|
201
|
|
|
|
|
202
|
|
|
$this->objectManager->flush(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
|
207
|
|
|
*/ |
|
208
|
|
|
public function itGivesFixedOffEveryProductClassifiedAs( |
|
209
|
|
|
PromotionInterface $promotion, |
|
210
|
|
|
$discount, |
|
211
|
|
|
TaxonInterface $taxon |
|
212
|
|
|
) { |
|
213
|
|
|
$action = $this->actionFactory->createItemFixedDiscount($discount); |
|
214
|
|
|
$promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()])); |
|
215
|
|
|
|
|
216
|
|
|
$this->objectManager->flush(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
|
221
|
|
|
*/ |
|
222
|
|
|
public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
|
223
|
|
|
PromotionInterface $promotion, |
|
224
|
|
|
$discount, |
|
225
|
|
|
$amount |
|
226
|
|
|
) { |
|
227
|
|
|
$filterConfiguration = ['filters' => ['price_range' => ['min' => $amount]]]; |
|
228
|
|
|
|
|
229
|
|
|
$this->createFixedPromotionWithPriceRangeFilter($promotion, $discount, $filterConfiguration); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
|
234
|
|
|
*/ |
|
235
|
|
|
public function thisPromotionGivesOffOnEveryProductPricedBetween( |
|
236
|
|
|
PromotionInterface $promotion, |
|
237
|
|
|
$discount, |
|
238
|
|
|
$minAmount, |
|
239
|
|
|
$maxAmount |
|
240
|
|
|
) { |
|
241
|
|
|
$filterConfiguration = ['filters' => ['price_range' => ['min' => $minAmount, 'max' => $maxAmount]]]; |
|
242
|
|
|
|
|
243
|
|
|
$this->createFixedPromotionWithPriceRangeFilter($promotion, $discount, $filterConfiguration); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
|
248
|
|
|
*/ |
|
249
|
|
|
public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
|
250
|
|
|
PromotionInterface $promotion, |
|
251
|
|
|
$discount, |
|
252
|
|
|
$amount |
|
253
|
|
|
) { |
|
254
|
|
|
$filterConfiguration = ['filters' => ['price_range' => ['min' => $amount]]]; |
|
255
|
|
|
|
|
256
|
|
|
$this->createPercentagePromotionWithPriceRangeFilter($promotion, $discount, $filterConfiguration); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
|
261
|
|
|
*/ |
|
262
|
|
|
public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
|
263
|
|
|
PromotionInterface $promotion, |
|
264
|
|
|
$discount, |
|
265
|
|
|
$minAmount, |
|
266
|
|
|
$maxAmount |
|
267
|
|
|
) { |
|
268
|
|
|
$filterConfiguration = ['filters' => ['price_range' => ['min' => $minAmount, 'max' => $maxAmount]]]; |
|
269
|
|
|
|
|
270
|
|
|
$this->createPercentagePromotionWithPriceRangeFilter($promotion, $discount, $filterConfiguration); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param ActionInterface $action |
|
275
|
|
|
* @param array $taxonCodes |
|
276
|
|
|
* |
|
277
|
|
|
* @return ActionInterface |
|
278
|
|
|
*/ |
|
279
|
|
|
private function configureActionTaxonFilter(ActionInterface $action, array $taxonCodes) |
|
280
|
|
|
{ |
|
281
|
|
|
$configuration = array_merge(['filters' => ['taxons' => $taxonCodes]], $action->getConfiguration()); |
|
282
|
|
|
$action->setConfiguration($configuration); |
|
283
|
|
|
|
|
284
|
|
|
return $action; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param PromotionInterface $promotion |
|
289
|
|
|
* @param int $discount |
|
290
|
|
|
* @param array $configuration |
|
291
|
|
|
*/ |
|
292
|
|
|
private function createFixedPromotionWithPriceRangeFilter(PromotionInterface $promotion, $discount, array $configuration) |
|
293
|
|
|
{ |
|
294
|
|
|
$this->persistPromotionWithAction($promotion, $this->actionFactory->createItemFixedDiscount($discount), $configuration); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param PromotionInterface $promotion |
|
299
|
|
|
* @param int $discount |
|
300
|
|
|
* @param array $configuration |
|
301
|
|
|
*/ |
|
302
|
|
|
private function createPercentagePromotionWithPriceRangeFilter(PromotionInterface $promotion, $discount, array $configuration) |
|
303
|
|
|
{ |
|
304
|
|
|
$this->persistPromotionWithAction($promotion, $this->actionFactory->createItemPercentageDiscount($discount), $configuration); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param PromotionInterface $promotion |
|
309
|
|
|
* @param ActionInterface $action |
|
310
|
|
|
* @param array $configuration |
|
311
|
|
|
*/ |
|
312
|
|
|
private function persistPromotionWithAction(PromotionInterface $promotion, ActionInterface $action, array $configuration) |
|
313
|
|
|
{ |
|
314
|
|
|
$configuration = array_merge($configuration, $action->getConfiguration()); |
|
315
|
|
|
$action->setConfiguration($configuration); |
|
316
|
|
|
|
|
317
|
|
|
$promotion->addAction($action); |
|
318
|
|
|
|
|
319
|
|
|
$this->objectManager->flush(); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|