|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace TddWizard\Fixtures\Catalog; |
|
5
|
|
|
|
|
6
|
|
|
use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
|
7
|
|
|
use Magento\Catalog\Model\Product; |
|
8
|
|
|
use Magento\Eav\Api\AttributeOptionManagementInterface; |
|
9
|
|
|
use Magento\Eav\Api\Data\AttributeOptionLabelInterface; |
|
10
|
|
|
use Magento\Eav\Model\Entity\Attribute\Option as AttributeOption; |
|
11
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create a source-model option for an attribute. |
|
15
|
|
|
* |
|
16
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
17
|
|
|
*/ |
|
18
|
|
|
class OptionBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var AttributeOptionManagementInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $attributeOptionManagement; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var AttributeOption |
|
28
|
|
|
*/ |
|
29
|
|
|
private $option; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AttributeOptionLabelInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $optionLabel; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $attributeCode; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* OptionBuilder constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param AttributeOptionManagementInterface $attributeOptionManagement |
|
45
|
|
|
* @param AttributeOption $option |
|
46
|
|
|
* @param AttributeOptionLabelInterface $optionLabel |
|
47
|
|
|
* @param string $attributeCode |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function __construct( |
|
50
|
|
|
AttributeOptionManagementInterface $attributeOptionManagement, |
|
51
|
|
|
AttributeOption $option, |
|
52
|
|
|
AttributeOptionLabelInterface $optionLabel, |
|
53
|
|
|
string $attributeCode |
|
54
|
|
|
) { |
|
55
|
4 |
|
$this->attributeOptionManagement = $attributeOptionManagement; |
|
56
|
4 |
|
$this->option = $option; |
|
57
|
4 |
|
$this->optionLabel = $optionLabel; |
|
58
|
4 |
|
$this->attributeCode = $attributeCode; |
|
59
|
4 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Clone the builder. |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public function __clone() |
|
65
|
|
|
{ |
|
66
|
4 |
|
$this->option = clone $this->option; |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create an option. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $attributeCode |
|
73
|
|
|
* @return OptionBuilder |
|
74
|
|
|
* @throws \Magento\Framework\Exception\InputException |
|
75
|
|
|
* @throws \Magento\Framework\Exception\StateException |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public static function anOption(string $attributeCode): OptionBuilder |
|
78
|
|
|
{ |
|
79
|
4 |
|
$objectManager = Bootstrap::getObjectManager(); |
|
80
|
|
|
/** @var AttributeOptionManagementInterface $attributeOptionManagement */ |
|
81
|
4 |
|
$attributeOptionManagement = $objectManager->create(AttributeOptionManagementInterface::class); |
|
82
|
4 |
|
$items = $attributeOptionManagement->getItems(Product::ENTITY, $attributeCode); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** @var AttributeOptionLabelInterface $optionLabel */ |
|
85
|
4 |
|
$optionLabel = $objectManager->create(AttributeOptionLabelInterface::class); |
|
86
|
4 |
|
$label = uniqid('Name ', true); |
|
87
|
4 |
|
$optionLabel->setStoreId(1); |
|
88
|
4 |
|
$optionLabel->setLabel($label); |
|
89
|
|
|
|
|
90
|
|
|
/** @var AttributeOption $option */ |
|
91
|
4 |
|
$option = $objectManager->create(AttributeOption::class); |
|
92
|
4 |
|
$option->setLabel($label); |
|
93
|
4 |
|
$option->setStoreLabels([$optionLabel]); |
|
94
|
4 |
|
$option->setSortOrder(count($items) + 1); |
|
95
|
4 |
|
$option->setIsDefault(false); |
|
96
|
|
|
|
|
97
|
4 |
|
return new static( |
|
98
|
4 |
|
$attributeOptionManagement, |
|
99
|
|
|
$option, |
|
100
|
|
|
$optionLabel, |
|
101
|
|
|
$attributeCode |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set label. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $label |
|
109
|
|
|
* @return OptionBuilder |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function withLabel(string $label): OptionBuilder |
|
112
|
|
|
{ |
|
113
|
1 |
|
$builder = clone $this; |
|
114
|
1 |
|
$builder->optionLabel->setLabel($label); |
|
115
|
1 |
|
$builder->option->setStoreLabels([$builder->optionLabel]); |
|
116
|
1 |
|
$builder->option->setLabel($label); |
|
117
|
|
|
|
|
118
|
1 |
|
return $builder; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set sort order. |
|
123
|
|
|
* |
|
124
|
|
|
* @param int $sortOrder |
|
125
|
|
|
* @return OptionBuilder |
|
126
|
|
|
*/ |
|
127
|
|
|
public function withSortOrder(int $sortOrder): OptionBuilder |
|
128
|
|
|
{ |
|
129
|
|
|
$builder = clone $this; |
|
130
|
|
|
$builder->option->setSortOrder($sortOrder); |
|
131
|
|
|
|
|
132
|
|
|
return $builder; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set default. |
|
137
|
|
|
* |
|
138
|
|
|
* @param bool $isDefault |
|
139
|
|
|
* @return OptionBuilder |
|
140
|
|
|
*/ |
|
141
|
|
|
public function withIsDefault(bool $isDefault): OptionBuilder |
|
142
|
|
|
{ |
|
143
|
|
|
$builder = clone $this; |
|
144
|
|
|
$builder->option->setIsDefault($isDefault); |
|
145
|
|
|
|
|
146
|
|
|
return $builder; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set store ID. |
|
151
|
|
|
* |
|
152
|
|
|
* @param int $storeId |
|
153
|
|
|
* @return OptionBuilder |
|
154
|
|
|
*/ |
|
155
|
|
|
public function withStoreId(int $storeId): OptionBuilder |
|
156
|
|
|
{ |
|
157
|
|
|
$builder = clone $this; |
|
158
|
|
|
$builder->optionLabel->setStoreId($storeId); |
|
159
|
|
|
|
|
160
|
|
|
return $builder; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Build the option and apply it to the attribute. |
|
165
|
|
|
* |
|
166
|
|
|
* @return AttributeOption |
|
167
|
|
|
* @throws \Magento\Framework\Exception\InputException |
|
168
|
|
|
* @throws \Magento\Framework\Exception\StateException |
|
169
|
|
|
*/ |
|
170
|
4 |
|
public function build(): AttributeOption |
|
171
|
|
|
{ |
|
172
|
4 |
|
$builder = clone $this; |
|
173
|
|
|
|
|
174
|
|
|
// add the option |
|
175
|
4 |
|
$this->attributeOptionManagement->add( |
|
176
|
4 |
|
\Magento\Catalog\Model\Product::ENTITY, |
|
|
|
|
|
|
177
|
4 |
|
$builder->attributeCode, |
|
178
|
4 |
|
$builder->option |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
4 |
|
$optionId = $this->getOptionId(); |
|
182
|
4 |
|
$builder->option->setId($optionId); |
|
183
|
|
|
|
|
184
|
4 |
|
return $builder->option; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get the option ID. |
|
189
|
|
|
* |
|
190
|
|
|
* @return int |
|
191
|
|
|
*/ |
|
192
|
4 |
|
private function getOptionId(): int |
|
193
|
|
|
{ |
|
194
|
4 |
|
$objectManager = Bootstrap::getObjectManager(); |
|
195
|
|
|
// the add option above does not return the option, so we need to retrieve it |
|
196
|
4 |
|
$attributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class); |
|
197
|
4 |
|
$attribute = $attributeRepository->get($this->attributeCode); |
|
198
|
4 |
|
$attributeValues[$attribute->getAttributeId()] = []; |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
// We have to generate a new sourceModel instance each time through to prevent it from |
|
201
|
|
|
// referencing its _options cache. No other way to get it to pick up newly-added values. |
|
202
|
4 |
|
$tableFactory = $objectManager->get(\Magento\Eav\Model\Entity\Attribute\Source\TableFactory::class); |
|
|
|
|
|
|
203
|
4 |
|
$sourceModel = $tableFactory->create(); |
|
204
|
4 |
|
$sourceModel->setAttribute($attribute); |
|
205
|
4 |
|
foreach ($sourceModel->getAllOptions() as $option) { |
|
206
|
4 |
|
$attributeValues[$attribute->getAttributeId()][$option['label']] = $option['value']; |
|
207
|
|
|
} |
|
208
|
4 |
|
if (isset($attributeValues[$attribute->getAttributeId()][$this->optionLabel->getLabel()])) { |
|
209
|
4 |
|
return (int)$attributeValues[$attribute->getAttributeId()][$this->optionLabel->getLabel()]; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
throw new \RuntimeException('Error building option'); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths