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 FactoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $shippingMethodFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ObjectManager |
41
|
|
|
*/ |
42
|
|
|
private $shippingMethodManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var SharedStorageInterface |
46
|
|
|
*/ |
47
|
|
|
private $sharedStorage; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RepositoryInterface $shippingMethodRepository |
51
|
|
|
* @param FactoryInterface $shippingMethodFactory |
52
|
|
|
* @param ObjectManager $shippingMethodManager |
53
|
|
|
* @param SharedStorageInterface $sharedStorage |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
RepositoryInterface $shippingMethodRepository, |
57
|
|
|
FactoryInterface $shippingMethodFactory, |
58
|
|
|
ObjectManager $shippingMethodManager, |
59
|
|
|
SharedStorageInterface $sharedStorage |
60
|
|
|
) { |
61
|
|
|
$this->shippingMethodRepository = $shippingMethodRepository; |
62
|
|
|
$this->shippingMethodFactory = $shippingMethodFactory; |
63
|
|
|
$this->shippingMethodManager = $shippingMethodManager; |
64
|
|
|
$this->sharedStorage = $sharedStorage; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @Transform :shippingMethodName shipping method |
69
|
|
|
* @Transform shipping method :shippingMethodName |
70
|
|
|
*/ |
71
|
|
|
public function getShippingMethodByName($shippingMethodName) |
72
|
|
|
{ |
73
|
|
|
$shippingMethod = $this->shippingMethodRepository->findOneBy(['name' => $shippingMethodName]); |
74
|
|
|
if (null === $shippingMethod) { |
75
|
|
|
throw new \Exception('Shipping method with name "'.$shippingMethodName.'" does not exist'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $shippingMethod; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Given store ships everything for free |
83
|
|
|
* @Given store ships everything for free within :zone zone |
84
|
|
|
*/ |
85
|
|
|
public function storeShipsEverythingForFree(ZoneInterface $zone = null) |
86
|
|
|
{ |
87
|
|
|
$this->createShippingMethod('Free', $zone); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @Given /^store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee$/ |
92
|
|
|
* @Given /^store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee within ("([^"]*)" zone)$/ |
93
|
|
|
* @Given /^store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee for (the rest of the world)$/ |
94
|
|
|
*/ |
95
|
|
|
public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null) |
96
|
|
|
{ |
97
|
|
|
$this->createShippingMethod($shippingMethodName, $zone, 'en', ['amount' => $this->getFeeFromString($fee)]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/ |
102
|
|
|
*/ |
103
|
|
|
public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory) |
104
|
|
|
{ |
105
|
|
|
$shippingMethod->setTaxCategory($taxCategory); |
106
|
|
|
$this->shippingMethodManager->flush(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param string $locale |
112
|
|
|
* @param array $configuration |
113
|
|
|
* @param string $calculator |
114
|
|
|
* @param ZoneInterface|null $zone |
115
|
|
|
*/ |
116
|
|
|
private function createShippingMethod( |
117
|
|
|
$name, |
118
|
|
|
$zone = null, |
119
|
|
|
$locale = 'en', |
120
|
|
|
$configuration = ['amount' => 0], |
121
|
|
|
$calculator = DefaultCalculators::FLAT_RATE |
122
|
|
|
) { |
123
|
|
|
if (null === $zone) { |
124
|
|
|
$zone = $this->sharedStorage->getCurrentResource('zone'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$shippingMethod = $this->shippingMethodFactory->createNew(); |
128
|
|
|
$shippingMethod->setCode($this->getCodeFromName($name)); |
129
|
|
|
$shippingMethod->setName($name); |
130
|
|
|
$shippingMethod->setCurrentLocale($locale); |
131
|
|
|
$shippingMethod->setConfiguration($configuration); |
132
|
|
|
$shippingMethod->setCalculator($calculator); |
133
|
|
|
$shippingMethod->setZone($zone); |
134
|
|
|
|
135
|
|
|
$this->shippingMethodRepository->add($shippingMethod); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $shippingMethodName |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function getCodeFromName($shippingMethodName) |
144
|
|
|
{ |
145
|
|
|
return str_replace([' ', '-'], '_', strtolower($shippingMethodName)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $shippingMethodFee |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
private function getFeeFromString($shippingMethodFee) |
154
|
|
|
{ |
155
|
|
|
return ((int) $shippingMethodFee) * 100; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|