|
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\Addressing\Model\ZoneInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
|
18
|
|
|
use Sylius\Component\Core\Test\Services\SharedStorageInterface; |
|
19
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
21
|
|
|
use Sylius\Component\Shipping\Calculator\DefaultCalculators; |
|
22
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class ShippingContext implements Context |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var RepositoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $shippingMethodRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var RepositoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $zoneRepository; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var FactoryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $shippingMethodFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ObjectManager |
|
46
|
|
|
*/ |
|
47
|
|
|
private $shippingMethodManager; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var SharedStorageInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $sharedStorage; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param RepositoryInterface $shippingMethodRepository |
|
56
|
|
|
* @param RepositoryInterface $zoneRepository |
|
57
|
|
|
* @param FactoryInterface $shippingMethodFactory |
|
58
|
|
|
* @param ObjectManager $shippingMethodManager |
|
59
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct( |
|
62
|
|
|
RepositoryInterface $shippingMethodRepository, |
|
63
|
|
|
RepositoryInterface $zoneRepository, |
|
64
|
|
|
FactoryInterface $shippingMethodFactory, |
|
65
|
|
|
ObjectManager $shippingMethodManager, |
|
66
|
|
|
SharedStorageInterface $sharedStorage |
|
67
|
|
|
) { |
|
68
|
|
|
$this->shippingMethodRepository = $shippingMethodRepository; |
|
69
|
|
|
$this->zoneRepository = $zoneRepository; |
|
70
|
|
|
$this->shippingMethodFactory = $shippingMethodFactory; |
|
71
|
|
|
$this->shippingMethodManager = $shippingMethodManager; |
|
72
|
|
|
$this->sharedStorage = $sharedStorage; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @Transform :shippingMethodName shipping method |
|
77
|
|
|
* @Transform shipping method :shippingMethodName |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getShippingMethodByName($shippingMethodName) |
|
80
|
|
|
{ |
|
81
|
|
|
$shippingMethod = $this->shippingMethodRepository->findOneBy(['name' => $shippingMethodName]); |
|
82
|
|
|
if (null === $shippingMethod) { |
|
83
|
|
|
throw new \Exception('Shipping method with name "'.$shippingMethodName.'" does not exist'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $shippingMethod; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Given the store ships everything for free |
|
91
|
|
|
* @Given the store ships everything for free within :zone zone |
|
92
|
|
|
* @Given /^the store ships everything for free for (the rest of the world)$/ |
|
93
|
|
|
*/ |
|
94
|
|
|
public function storeShipsEverythingForFree(ZoneInterface $zone = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->createShippingMethod('Free', $zone); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Given /^the store ships everything for free to all available locations$/ |
|
101
|
|
|
*/ |
|
102
|
|
|
public function theStoreShipsEverythingForFreeToAllAvailableLocations() |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($this->zoneRepository->findAll() as $zone) { |
|
105
|
|
|
$this->createShippingMethod('Free', $zone); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @Given /^the store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee$/ |
|
111
|
|
|
* @Given /^the store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee within ("([^"]*)" zone)$/ |
|
112
|
|
|
* @Given /^the store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee for (the rest of the world)$/ |
|
113
|
|
|
*/ |
|
114
|
|
|
public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->createShippingMethod($shippingMethodName, $zone, 'en', ['amount' => $this->getFeeFromString($fee)]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/ |
|
121
|
|
|
*/ |
|
122
|
|
|
public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory) |
|
123
|
|
|
{ |
|
124
|
|
|
$shippingMethod->setTaxCategory($taxCategory); |
|
125
|
|
|
$this->shippingMethodManager->flush(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* @param string $locale |
|
131
|
|
|
* @param array $configuration |
|
132
|
|
|
* @param string $calculator |
|
133
|
|
|
* @param ZoneInterface|null $zone |
|
134
|
|
|
*/ |
|
135
|
|
|
private function createShippingMethod( |
|
136
|
|
|
$name, |
|
137
|
|
|
$zone = null, |
|
138
|
|
|
$locale = 'en', |
|
139
|
|
|
$configuration = ['amount' => 0], |
|
140
|
|
|
$calculator = DefaultCalculators::FLAT_RATE |
|
141
|
|
|
) { |
|
142
|
|
|
if (null === $zone) { |
|
143
|
|
|
$zone = $this->sharedStorage->getCurrentResource('zone'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$shippingMethod = $this->shippingMethodFactory->createNew(); |
|
147
|
|
|
$shippingMethod->setCode($this->generateCodeFromNameAndZone($name, $zone->getCode())); |
|
148
|
|
|
$shippingMethod->setName($name); |
|
149
|
|
|
$shippingMethod->setCurrentLocale($locale); |
|
150
|
|
|
$shippingMethod->setConfiguration($configuration); |
|
151
|
|
|
$shippingMethod->setCalculator($calculator); |
|
152
|
|
|
$shippingMethod->setZone($zone); |
|
153
|
|
|
|
|
154
|
|
|
$this->shippingMethodRepository->add($shippingMethod); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $shippingMethodName |
|
159
|
|
|
* @param string|null $zoneCode |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
private function generateCodeFromNameAndZone($shippingMethodName, $zoneCode = null) |
|
164
|
|
|
{ |
|
165
|
|
|
return str_replace([' ', '-'], '_', strtolower($shippingMethodName)).'_'.strtolower($zoneCode); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $shippingMethodFee |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
private function getFeeFromString($shippingMethodFee) |
|
174
|
|
|
{ |
|
175
|
|
|
return ((int) $shippingMethodFee) * 100; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|