|
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\Model\ProductInterface; |
|
18
|
|
|
use Sylius\Component\Product\Model\ProductAssociationInterface; |
|
19
|
|
|
use Sylius\Component\Product\Model\ProductAssociationTypeInterface; |
|
20
|
|
|
use Sylius\Component\Product\Model\ProductAssociationTypeTranslationInterface; |
|
21
|
|
|
use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface; |
|
22
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
23
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
|
24
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
final class ProductAssociationContext implements Context |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var SharedStorageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sharedStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var FactoryInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $productAssociationTypeFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var FactoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $productAssociationTypeTranslationFactory; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var FactoryInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $productAssociationFactory; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var ProductAssociationTypeRepositoryInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $productAssociationTypeRepository; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var RepositoryInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $productAssociationRepository; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var ObjectManager |
|
63
|
|
|
*/ |
|
64
|
|
|
private $objectManager; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
68
|
|
|
* @param FactoryInterface $productAssociationTypeFactory |
|
69
|
|
|
* @param FactoryInterface $productAssociationTypeTranslationFactory |
|
70
|
|
|
* @param FactoryInterface $productAssociationFactory |
|
71
|
|
|
* @param ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository |
|
72
|
|
|
* @param RepositoryInterface $productAssociationRepository |
|
73
|
|
|
* @param ObjectManager $objectManager |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct( |
|
76
|
|
|
SharedStorageInterface $sharedStorage, |
|
77
|
|
|
FactoryInterface $productAssociationTypeFactory, |
|
78
|
|
|
FactoryInterface $productAssociationTypeTranslationFactory, |
|
79
|
|
|
FactoryInterface $productAssociationFactory, |
|
80
|
|
|
ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository, |
|
81
|
|
|
RepositoryInterface $productAssociationRepository, |
|
82
|
|
|
ObjectManager $objectManager |
|
83
|
|
|
) { |
|
84
|
|
|
$this->sharedStorage = $sharedStorage; |
|
85
|
|
|
$this->productAssociationTypeFactory = $productAssociationTypeFactory; |
|
86
|
|
|
$this->productAssociationTypeTranslationFactory = $productAssociationTypeTranslationFactory; |
|
87
|
|
|
$this->productAssociationFactory = $productAssociationFactory; |
|
88
|
|
|
$this->productAssociationTypeRepository = $productAssociationTypeRepository; |
|
89
|
|
|
$this->productAssociationRepository = $productAssociationRepository; |
|
90
|
|
|
$this->objectManager = $objectManager; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @Given the store has (also) a product association type :name |
|
95
|
|
|
* @Given the store has (also) a product association type :name with a code :code |
|
96
|
|
|
*/ |
|
97
|
|
|
public function theStoreHasAProductAssociationType($name, $code = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->createProductAssociationType($name, $code); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @Given /^the store has(?:| also) a product association type named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
|
104
|
|
|
*/ |
|
105
|
|
|
public function itHasVariantNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
|
106
|
|
|
{ |
|
107
|
|
|
$productAssociationType = $this->createProductAssociationType($firstName); |
|
108
|
|
|
|
|
109
|
|
|
$names = [$firstName => $firstLocale, $secondName => $secondLocale]; |
|
110
|
|
|
foreach ($names as $name => $locale) { |
|
111
|
|
|
$this->addProductAssociationTypeTranslation($productAssociationType, $name, $locale); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->objectManager->flush(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @Given the store has :firstName and :secondName product association types |
|
119
|
|
|
*/ |
|
120
|
|
|
public function theStoreHasProductAssociationTypes(...$names) |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($names as $name) { |
|
123
|
|
|
$this->createProductAssociationType($name); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (product "[^"]+")$/ |
|
129
|
|
|
*/ |
|
130
|
|
|
public function theProductHasAnAssociationWithProduct( |
|
131
|
|
|
ProductInterface $product, |
|
132
|
|
|
ProductAssociationTypeInterface $productAssociationType, |
|
133
|
|
|
ProductInterface $associatedProduct |
|
134
|
|
|
) { |
|
135
|
|
|
$this->createProductAssociation($product, $productAssociationType, [$associatedProduct]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (products "[^"]+" and "[^"]+")$/ |
|
140
|
|
|
*/ |
|
141
|
|
|
public function theProductHasAnAssociationWithProducts( |
|
142
|
|
|
ProductInterface $product, |
|
143
|
|
|
ProductAssociationTypeInterface $productAssociationType, |
|
144
|
|
|
array $associatedProducts |
|
145
|
|
|
) { |
|
146
|
|
|
$this->createProductAssociation($product, $productAssociationType, $associatedProducts); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $name |
|
151
|
|
|
* @param string|null $code |
|
152
|
|
|
* |
|
153
|
|
|
* @return ProductAssociationTypeInterface |
|
154
|
|
|
*/ |
|
155
|
|
|
private function createProductAssociationType($name, $code = null) |
|
156
|
|
|
{ |
|
157
|
|
|
if (null === $code) { |
|
158
|
|
|
$code = $this->generateCodeFromName($name); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** @var ProductAssociationTypeInterface $productAssociationType */ |
|
162
|
|
|
$productAssociationType = $this->productAssociationTypeFactory->createNew(); |
|
163
|
|
|
$productAssociationType->setCode($code); |
|
164
|
|
|
$productAssociationType->setName($name); |
|
165
|
|
|
|
|
166
|
|
|
$this->productAssociationTypeRepository->add($productAssociationType); |
|
167
|
|
|
$this->sharedStorage->set('product_association_type', $productAssociationType); |
|
168
|
|
|
|
|
169
|
|
|
return $productAssociationType; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param ProductInterface $product |
|
174
|
|
|
* @param ProductAssociationTypeInterface $productAssociationType |
|
175
|
|
|
* @param array $associatedProducts |
|
176
|
|
|
*/ |
|
177
|
|
|
private function createProductAssociation( |
|
178
|
|
|
ProductInterface $product, |
|
179
|
|
|
ProductAssociationTypeInterface $productAssociationType, |
|
180
|
|
|
array $associatedProducts |
|
181
|
|
|
) { |
|
182
|
|
|
/** @var ProductAssociationInterface $productAssociation */ |
|
183
|
|
|
$productAssociation = $this->productAssociationFactory->createNew(); |
|
184
|
|
|
$productAssociation->setType($productAssociationType); |
|
185
|
|
|
|
|
186
|
|
|
foreach ($associatedProducts as $associatedProduct) { |
|
187
|
|
|
$productAssociation->addAssociatedProduct($associatedProduct); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$product->addAssociation($productAssociation); |
|
191
|
|
|
|
|
192
|
|
|
$this->productAssociationRepository->add($productAssociation); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param ProductAssociationTypeInterface $productAssociationType |
|
197
|
|
|
* @param string $name |
|
198
|
|
|
* @param string $locale |
|
199
|
|
|
*/ |
|
200
|
|
|
private function addProductAssociationTypeTranslation( |
|
201
|
|
|
ProductAssociationTypeInterface $productAssociationType, |
|
202
|
|
|
$name, |
|
203
|
|
|
$locale |
|
204
|
|
|
) { |
|
205
|
|
|
/** @var ProductAssociationTypeTranslationInterface|TranslationInterface $translation */ |
|
206
|
|
|
$translation = $this->productAssociationTypeTranslationFactory->createNew(); |
|
207
|
|
|
$translation->setLocale($locale); |
|
|
|
|
|
|
208
|
|
|
$translation->setName($name); |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
$productAssociationType->addTranslation($translation); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param string $name |
|
215
|
|
|
* |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
|
|
private function generateCodeFromName($name) |
|
219
|
|
|
{ |
|
220
|
|
|
return str_replace([' ', '-'], '_', strtolower($name)); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: