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\Addressing\Model\ZoneInterface; |
18
|
|
|
use Sylius\Component\Addressing\Repository\ZoneRepositoryInterface; |
19
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
20
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
21
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
22
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
23
|
|
|
use Sylius\Component\Shipping\Calculator\DefaultCalculators; |
24
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
25
|
|
|
use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface; |
26
|
|
|
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface; |
27
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class ShippingContext implements Context |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var SharedStorageInterface |
36
|
|
|
*/ |
37
|
|
|
private $sharedStorage; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ShippingMethodRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $shippingMethodRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ZoneRepositoryInterface |
46
|
|
|
*/ |
47
|
|
|
private $zoneRepository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FactoryInterface |
51
|
|
|
*/ |
52
|
|
|
private $shippingMethodFactory; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var FactoryInterface |
56
|
|
|
*/ |
57
|
|
|
private $shippingMethodTranslationFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ObjectManager |
61
|
|
|
*/ |
62
|
|
|
private $shippingMethodManager; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param SharedStorageInterface $sharedStorage |
66
|
|
|
* @param ShippingMethodRepositoryInterface $shippingMethodRepository |
67
|
|
|
* @param ZoneRepositoryInterface $zoneRepository |
68
|
|
|
* @param FactoryInterface $shippingMethodFactory |
69
|
|
|
* @param FactoryInterface $shippingMethodTranslationFactory |
70
|
|
|
* @param ObjectManager $shippingMethodManager |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
SharedStorageInterface $sharedStorage, |
74
|
|
|
ShippingMethodRepositoryInterface $shippingMethodRepository, |
|
|
|
|
75
|
|
|
ZoneRepositoryInterface $zoneRepository, |
76
|
|
|
FactoryInterface $shippingMethodFactory, |
|
|
|
|
77
|
|
|
FactoryInterface $shippingMethodTranslationFactory, |
|
|
|
|
78
|
|
|
ObjectManager $shippingMethodManager |
|
|
|
|
79
|
|
|
) { |
80
|
|
|
$this->sharedStorage = $sharedStorage; |
81
|
|
|
$this->shippingMethodRepository = $shippingMethodRepository; |
82
|
|
|
$this->zoneRepository = $zoneRepository; |
83
|
|
|
$this->shippingMethodFactory = $shippingMethodFactory; |
84
|
|
|
$this->shippingMethodTranslationFactory = $shippingMethodTranslationFactory; |
85
|
|
|
$this->shippingMethodManager = $shippingMethodManager; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @Given the store ships everything for free within the :zone zone |
90
|
|
|
* @Given /^the store ships everything for free for the (rest of the world)$/ |
91
|
|
|
*/ |
92
|
|
|
public function storeShipsEverythingForFree(ZoneInterface $zone = null) |
93
|
|
|
{ |
94
|
|
|
$this->createShippingMethod('Free', null, null, $zone); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @Given the store ships everywhere for free |
99
|
|
|
*/ |
100
|
|
|
public function theStoreShipsEverywhereForFree() |
101
|
|
|
{ |
102
|
|
|
foreach ($this->zoneRepository->findAll() as $zone) { |
103
|
|
|
$this->createShippingMethod('Free', null, null, $zone); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Given the store (also) allows shipping with :name |
109
|
|
|
* @Given the store (also) allows shipping with :name identified by :code |
110
|
|
|
* @Given the store (also) allows shipping with :name at position :position |
111
|
|
|
*/ |
112
|
|
|
public function theStoreAllowsShippingMethod($name, $code = null, $position = null) |
113
|
|
|
{ |
114
|
|
|
$this->createShippingMethod($name, $code, $position); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @Given /^(this shipping method) is named "([^"]+)" in the "([^"]+)" locale$/ |
119
|
|
|
*/ |
120
|
|
|
public function thisShippingMethodIsNamedInLocale(ShippingMethodInterface $shippingMethod, $name, $locale) |
121
|
|
|
{ |
122
|
|
|
/** @var ShippingMethodTranslationInterface $translation */ |
123
|
|
|
$translation = $this->shippingMethodTranslationFactory->createNew(); |
124
|
|
|
$translation->setLocale($locale); |
125
|
|
|
$translation->setName($name); |
126
|
|
|
|
127
|
|
|
$shippingMethod->addTranslation($translation); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$this->shippingMethodManager->flush(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @Given the store allows shipping with :firstName and :secondName |
134
|
|
|
*/ |
135
|
|
|
public function theStoreAllowsShippingWithAnd($firstName, $secondName) |
136
|
|
|
{ |
137
|
|
|
$this->createShippingMethod($firstName); |
138
|
|
|
$this->createShippingMethod($secondName); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee$/ |
143
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee within the ("[^"]+" zone)$/ |
144
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee for the (rest of the world)$/ |
145
|
|
|
*/ |
146
|
|
|
public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null) |
147
|
|
|
{ |
148
|
|
|
$this->createShippingMethod($shippingMethodName, null, null, $zone, 'en', ['amount' => $fee]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @Given /^the store has disabled "([^"]+)" shipping method with ("[^"]+") fee$/ |
153
|
|
|
*/ |
154
|
|
|
public function storeHasDisabledShippingMethodWithFee($shippingMethodName, $fee) |
155
|
|
|
{ |
156
|
|
|
$this->createShippingMethod($shippingMethodName, null, null, null, 'en', ['amount' => $fee], DefaultCalculators::FLAT_RATE, false); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit$/ |
161
|
|
|
*/ |
162
|
|
|
public function theStoreHasShippingMethodWithFeePerUnit($shippingMethodName, $fee) |
163
|
|
|
{ |
164
|
|
|
$this->createShippingMethod($shippingMethodName, null, null, null, 'en', ['amount' => $fee], DefaultCalculators::PER_UNIT_RATE); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee on fist unit and ("[^"]+") on next (\d+)$/ |
169
|
|
|
*/ |
170
|
|
|
public function theStoreHasShippingMethodWithFeeOnFistUnitAndOnNext($shippingMethodName, $fee, $perUnitFee, $limit) |
171
|
|
|
{ |
172
|
|
|
$this->createShippingMethod( |
173
|
|
|
$shippingMethodName, |
174
|
|
|
null, |
175
|
|
|
null, |
176
|
|
|
null, |
177
|
|
|
'en', |
178
|
|
|
['first_unit_cost' => $fee, 'additional_unit_cost' => $perUnitFee, 'additional_unit_limit' => $limit], |
179
|
|
|
DefaultCalculators::FLEXIBLE_RATE |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee not assigned to any channel$/ |
185
|
|
|
*/ |
186
|
|
|
public function storeHasShippingMethodWithFeeNotAssignedToAnyChannel($shippingMethodName, $fee) |
187
|
|
|
{ |
188
|
|
|
$this->createShippingMethod($shippingMethodName, null, null, null, 'en', ['amount' => $fee], DefaultCalculators::FLAT_RATE, false, false); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/ |
193
|
|
|
*/ |
194
|
|
|
public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory) |
195
|
|
|
{ |
196
|
|
|
$shippingMethod->setTaxCategory($taxCategory); |
197
|
|
|
$this->shippingMethodManager->flush(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @Given the shipping method :shippingMethod is enabled |
202
|
|
|
*/ |
203
|
|
|
public function theShippingMethodIsEnabled(ShippingMethodInterface $shippingMethod) |
204
|
|
|
{ |
205
|
|
|
$shippingMethod->enable(); |
206
|
|
|
$this->shippingMethodManager->flush(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @Given the shipping method :shippingMethod is disabled |
211
|
|
|
*/ |
212
|
|
|
public function theShippingMethodIsDisabled(ShippingMethodInterface $shippingMethod) |
213
|
|
|
{ |
214
|
|
|
$shippingMethod->disable(); |
215
|
|
|
$this->shippingMethodManager->flush(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @Given /^(this shipping method) requires at least one unit matches to ("([^"]+)" shipping category)$/ |
220
|
|
|
*/ |
221
|
|
|
public function thisShippingMethodRequiresAtLeastOneUnitMatchToShippingCategory( |
222
|
|
|
ShippingMethodInterface $shippingMethod, |
223
|
|
|
ShippingCategoryInterface $shippingCategory |
224
|
|
|
) { |
225
|
|
|
$shippingMethod->setCategory($shippingCategory); |
226
|
|
|
$shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY); |
227
|
|
|
$this->shippingMethodManager->flush(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @Given /^(this shipping method) requires that all units match to ("([^"]+)" shipping category)$/ |
232
|
|
|
*/ |
233
|
|
|
public function thisShippingMethodRequiresThatAllUnitsMatchToShippingCategory( |
234
|
|
|
ShippingMethodInterface $shippingMethod, |
235
|
|
|
ShippingCategoryInterface $shippingCategory |
236
|
|
|
) { |
237
|
|
|
$shippingMethod->setCategory($shippingCategory); |
238
|
|
|
$shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ALL); |
239
|
|
|
$this->shippingMethodManager->flush(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @Given /^(this shipping method) requires that no units match to ("([^"]+)" shipping category)$/ |
244
|
|
|
*/ |
245
|
|
|
public function thisShippingMethodRequiresThatNoUnitsMatchToShippingCategory( |
246
|
|
|
ShippingMethodInterface $shippingMethod, |
247
|
|
|
ShippingCategoryInterface $shippingCategory |
248
|
|
|
) { |
249
|
|
|
$shippingMethod->setCategory($shippingCategory); |
250
|
|
|
$shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_NONE); |
251
|
|
|
$this->shippingMethodManager->flush(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $name |
256
|
|
|
* @param string|null $code |
257
|
|
|
* @param int|null $position |
258
|
|
|
* @param ZoneInterface|null $zone |
259
|
|
|
* @param string $locale |
260
|
|
|
* @param array $configuration |
261
|
|
|
* @param string $calculator |
262
|
|
|
* @param bool $enabled |
263
|
|
|
* @param bool $addForCurrentChannel |
264
|
|
|
*/ |
265
|
|
|
private function createShippingMethod( |
266
|
|
|
$name, |
267
|
|
|
$code = null, |
268
|
|
|
$position = null, |
269
|
|
|
ZoneInterface $zone = null, |
270
|
|
|
$locale = 'en', |
271
|
|
|
$configuration = ['amount' => 0], |
272
|
|
|
$calculator = DefaultCalculators::FLAT_RATE, |
273
|
|
|
$enabled = true, |
274
|
|
|
$addForCurrentChannel = true |
275
|
|
|
) { |
276
|
|
|
if (null === $zone) { |
277
|
|
|
$zone = $this->sharedStorage->get('zone'); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if (null === $code) { |
281
|
|
|
$code = $this->generateCodeFromNameAndZone($name, $zone->getCode()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** @var ShippingMethodInterface $shippingMethod */ |
285
|
|
|
$shippingMethod = $this->shippingMethodFactory->createNew(); |
286
|
|
|
$shippingMethod->setCode($code); |
287
|
|
|
$shippingMethod->setName($name); |
288
|
|
|
$shippingMethod->setPosition($position); |
289
|
|
|
$shippingMethod->setCurrentLocale($locale); |
290
|
|
|
$shippingMethod->setConfiguration($configuration); |
291
|
|
|
$shippingMethod->setCalculator($calculator); |
292
|
|
|
$shippingMethod->setZone($zone); |
293
|
|
|
$shippingMethod->setEnabled($enabled); |
294
|
|
|
|
295
|
|
|
if ($addForCurrentChannel && $this->sharedStorage->has('channel')) { |
296
|
|
|
$shippingMethod->addChannel($this->sharedStorage->get('channel')); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$this->shippingMethodRepository->add($shippingMethod); |
300
|
|
|
$this->sharedStorage->set('shipping_method', $shippingMethod); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param string $shippingMethodName |
305
|
|
|
* @param string|null $zoneCode |
306
|
|
|
* |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
private function generateCodeFromNameAndZone($shippingMethodName, $zoneCode = null) |
310
|
|
|
{ |
311
|
|
|
return StringInflector::nameToLowercaseCode($shippingMethodName).'_'.StringInflector::nameToLowercaseCode($zoneCode); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.