|
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 Behat\Mink\Element\NodeElement; |
|
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
17
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
|
18
|
|
|
use Sylius\Component\Core\Model\ImageInterface; |
|
19
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
20
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
|
21
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
22
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
|
23
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
24
|
|
|
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface; |
|
25
|
|
|
use Sylius\Component\Taxonomy\Model\TaxonTranslationInterface; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class TaxonomyContext implements Context |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var RepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $taxonRepository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var FactoryInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $taxonFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var FactoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $taxonTranslationFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var FactoryInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $taxonImageFactory; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ObjectManager |
|
55
|
|
|
*/ |
|
56
|
|
|
private $objectManager; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var ImageUploaderInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
private $imageUploader; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var TaxonSlugGeneratorInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
private $taxonSlugGenerator; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
private $minkParameters; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param RepositoryInterface $taxonRepository |
|
75
|
|
|
* @param FactoryInterface $taxonFactory |
|
76
|
|
|
* @param FactoryInterface $taxonTranslationFactory |
|
77
|
|
|
* @param FactoryInterface $taxonImageFactory |
|
78
|
|
|
* @param ObjectManager $objectManager |
|
79
|
|
|
* @param ImageUploaderInterface $imageUploader |
|
80
|
|
|
* @param TaxonSlugGeneratorInterface $taxonSlugGenerator |
|
81
|
|
|
* @param array $minkParameters |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct( |
|
84
|
|
|
RepositoryInterface $taxonRepository, |
|
85
|
|
|
FactoryInterface $taxonFactory, |
|
86
|
|
|
FactoryInterface $taxonTranslationFactory, |
|
87
|
|
|
FactoryInterface $taxonImageFactory, |
|
88
|
|
|
ObjectManager $objectManager, |
|
89
|
|
|
ImageUploaderInterface $imageUploader, |
|
90
|
|
|
TaxonSlugGeneratorInterface $taxonSlugGenerator, |
|
91
|
|
|
array $minkParameters |
|
92
|
|
|
) { |
|
93
|
|
|
$this->taxonRepository = $taxonRepository; |
|
94
|
|
|
$this->taxonFactory = $taxonFactory; |
|
95
|
|
|
$this->taxonTranslationFactory = $taxonTranslationFactory; |
|
96
|
|
|
$this->taxonImageFactory = $taxonImageFactory; |
|
97
|
|
|
$this->objectManager = $objectManager; |
|
98
|
|
|
$this->imageUploader = $imageUploader; |
|
99
|
|
|
$this->taxonSlugGenerator = $taxonSlugGenerator; |
|
100
|
|
|
$this->minkParameters = $minkParameters; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @Given the store has :firstTaxonName taxonomy |
|
105
|
|
|
* @Given the store classifies its products as :firstTaxonName |
|
106
|
|
|
* @Given the store classifies its products as :firstTaxonName and :secondTaxonName |
|
107
|
|
|
* @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName |
|
108
|
|
|
* @Given the store classifies its products as :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName |
|
109
|
|
|
*/ |
|
110
|
|
|
public function storeClassifiesItsProductsAs(...$taxonsNames) |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($taxonsNames as $taxonName) { |
|
113
|
|
|
$this->taxonRepository->add($this->createTaxon($taxonName)); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @Given /^the store has taxonomy named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
|
119
|
|
|
*/ |
|
120
|
|
|
public function theStoreHasTaxonomyNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
|
121
|
|
|
{ |
|
122
|
|
|
$translationMap = [ |
|
123
|
|
|
$firstLocale => $firstName, |
|
124
|
|
|
$secondLocale => $secondName, |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
$this->taxonRepository->add($this->createTaxonInManyLanguages($translationMap)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @Given /^the ("[^"]+" taxon)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
|
132
|
|
|
*/ |
|
133
|
|
|
public function theTaxonHasAnImageWithType(TaxonInterface $taxon, $imagePath, $imageType) |
|
134
|
|
|
{ |
|
135
|
|
|
$filesPath = $this->getParameter('files_path'); |
|
136
|
|
|
|
|
137
|
|
|
/** @var ImageInterface $taxonImage */ |
|
138
|
|
|
$taxonImage = $this->taxonImageFactory->createNew(); |
|
139
|
|
|
$taxonImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath))); |
|
140
|
|
|
$taxonImage->setType($imageType); |
|
141
|
|
|
$this->imageUploader->upload($taxonImage); |
|
142
|
|
|
|
|
143
|
|
|
$taxon->addImage($taxonImage); |
|
144
|
|
|
|
|
145
|
|
|
$this->objectManager->flush($taxon); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $name |
|
150
|
|
|
* |
|
151
|
|
|
* @return TaxonInterface |
|
152
|
|
|
*/ |
|
153
|
|
|
private function createTaxon($name) |
|
154
|
|
|
{ |
|
155
|
|
|
/** @var TaxonInterface $taxon */ |
|
156
|
|
|
$taxon = $this->taxonFactory->createNew(); |
|
157
|
|
|
$taxon->setName($name); |
|
158
|
|
|
$taxon->setCode(StringInflector::nameToCode($name)); |
|
159
|
|
|
$taxon->setSlug($this->taxonSlugGenerator->generate($name)); |
|
160
|
|
|
|
|
161
|
|
|
return $taxon; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array $names |
|
166
|
|
|
* |
|
167
|
|
|
* @return TaxonInterface |
|
168
|
|
|
*/ |
|
169
|
|
|
private function createTaxonInManyLanguages(array $names) |
|
170
|
|
|
{ |
|
171
|
|
|
/** @var TaxonInterface $taxon */ |
|
172
|
|
|
$taxon = $this->taxonFactory->createNew(); |
|
173
|
|
|
$taxon->setCode(StringInflector::nameToCode($names['en_US'])); |
|
174
|
|
|
foreach ($names as $locale => $name) { |
|
175
|
|
|
/** @var TranslationInterface|TaxonTranslationInterface $taxonTranslation */ |
|
176
|
|
|
$taxonTranslation = $this->taxonTranslationFactory->createNew(); |
|
177
|
|
|
$taxonTranslation->setLocale($locale); |
|
|
|
|
|
|
178
|
|
|
$taxonTranslation->setName($name); |
|
|
|
|
|
|
179
|
|
|
$taxonTranslation->setSlug($this->taxonSlugGenerator->generate($name)); |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
$taxon->addTranslation($taxonTranslation); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $taxon; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $name |
|
189
|
|
|
* |
|
190
|
|
|
* @return NodeElement |
|
191
|
|
|
*/ |
|
192
|
|
|
private function getParameter($name) |
|
193
|
|
|
{ |
|
194
|
|
|
return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
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: