1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Catalog; |
4
|
|
|
|
5
|
|
|
use Magento\Catalog\Api\Data\ProductInterface; |
6
|
|
|
use Magento\Catalog\Api\Data\ProductWebsiteLinkInterface; |
7
|
|
|
use Magento\Catalog\Api\Data\ProductWebsiteLinkInterfaceFactory; |
|
|
|
|
8
|
|
|
use Magento\Catalog\Api\ProductRepositoryInterface; |
9
|
|
|
use Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface; |
10
|
|
|
use Magento\Catalog\Model\Product\Attribute\Source\Status; |
11
|
|
|
use Magento\Catalog\Model\Product\Visibility; |
12
|
|
|
use Magento\CatalogInventory\Api\Data\StockItemInterface; |
13
|
|
|
use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
14
|
|
|
use Magento\Framework\ObjectManagerInterface; |
15
|
|
|
|
16
|
|
|
class ProductBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ProductInterface |
20
|
|
|
*/ |
21
|
|
|
private $product; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ProductRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $productRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int[] |
30
|
|
|
*/ |
31
|
|
|
private $websiteIds = []; |
32
|
|
|
/** |
33
|
|
|
* @var ProductWebsiteLinkRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $websiteLinkRepository; |
36
|
|
|
/** |
37
|
|
|
* @var StockItemRepositoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $stockItemRepository; |
40
|
|
|
/** |
41
|
|
|
* @var ProductWebsiteLinkInterfaceFactory |
42
|
|
|
*/ |
43
|
|
|
private $websiteLinkFactory; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
ProductRepositoryInterface $productRepository, |
47
|
|
|
StockItemRepositoryInterface $stockItemRepository, |
48
|
|
|
ProductWebsiteLinkRepositoryInterface $websiteLinkRepository, |
49
|
|
|
ProductWebsiteLinkInterfaceFactory $websiteLinkFactory, |
50
|
|
|
ProductInterface $product, |
51
|
|
|
array $websiteIds |
52
|
|
|
) { |
53
|
|
|
$this->productRepository = $productRepository; |
54
|
|
|
$this->websiteLinkRepository = $websiteLinkRepository; |
55
|
|
|
$this->stockItemRepository = $stockItemRepository; |
56
|
|
|
$this->websiteLinkFactory = $websiteLinkFactory; |
57
|
|
|
$this->product = $product; |
58
|
|
|
$this->websiteIds = $websiteIds; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function __clone() |
62
|
|
|
{ |
63
|
|
|
$this->product = clone $this->product; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function aSimpleProduct(ObjectManagerInterface $objectManager = null) : ProductBuilder |
67
|
|
|
{ |
68
|
|
|
if ($objectManager === null) { |
69
|
|
|
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
/** @var ProductInterface $product */ |
72
|
|
|
$product = $objectManager->create(ProductInterface::class); |
73
|
|
|
|
74
|
|
|
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) |
75
|
|
|
->setAttributeSetId(4) |
76
|
|
|
->setName('Simple Product') |
77
|
|
|
->setPrice(10) |
78
|
|
|
->setVisibility(Visibility::VISIBILITY_BOTH) |
79
|
|
|
->setStatus(Status::STATUS_ENABLED); |
80
|
|
|
$product->addData([ |
81
|
|
|
'tax_class_id' => 1, |
82
|
|
|
'description' => 'Description', |
83
|
|
|
]); |
84
|
|
|
/** @var StockItemInterface $stockItem */ |
85
|
|
|
$stockItem = $objectManager->create(StockItemInterface::class); |
86
|
|
|
$stockItem->setManageStock(true) |
87
|
|
|
->setQty(100) |
88
|
|
|
->setIsQtyDecimal(false) |
89
|
|
|
->setIsInStock(true); |
90
|
|
|
$product->setExtensionAttributes( |
91
|
|
|
$product->getExtensionAttributes()->setStockItem($stockItem) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return new self( |
95
|
|
|
$objectManager->create(ProductRepositoryInterface::class), |
96
|
|
|
$objectManager->create(StockItemRepositoryInterface::class), |
97
|
|
|
$objectManager->create(ProductWebsiteLinkRepositoryInterface::class), |
98
|
|
|
$objectManager->create(ProductWebsiteLinkInterfaceFactory::class), |
99
|
|
|
$product, |
100
|
|
|
[1] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function withSku(string $sku) : ProductBuilder |
105
|
|
|
{ |
106
|
|
|
$builder = clone $this; |
107
|
|
|
$builder->product->setSku($sku); |
108
|
|
|
return $builder; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function withName(string $name) : ProductBuilder |
112
|
|
|
{ |
113
|
|
|
$builder = clone $this; |
114
|
|
|
$builder->product->setName($name); |
115
|
|
|
return $builder; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function withStatus(int $status) : ProductBuilder |
119
|
|
|
{ |
120
|
|
|
$builder = clone $this; |
121
|
|
|
$builder->product->setStatus($status); |
122
|
|
|
return $builder; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function withVisibility(int $visibility) : ProductBuilder |
126
|
|
|
{ |
127
|
|
|
$builder = clone $this; |
128
|
|
|
$builder->product->setVisibility($visibility); |
129
|
|
|
return $builder; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function withWebsiteIds(array $websiteIds) : ProductBuilder |
133
|
|
|
{ |
134
|
|
|
$builder = clone $this; |
135
|
|
|
$builder->websiteIds = $websiteIds; |
136
|
|
|
return $builder; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function withPrice(float $price) : ProductBuilder |
140
|
|
|
{ |
141
|
|
|
$builder = clone $this; |
142
|
|
|
$builder->product->setPrice($price); |
143
|
|
|
return $builder; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function withTaxClassId($taxClassId) : ProductBuilder |
147
|
|
|
{ |
148
|
|
|
$builder = clone $this; |
149
|
|
|
$builder->product->setData('tax_class_id', $taxClassId); |
150
|
|
|
return $builder; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function withIsInStock(bool $inStock) : ProductBuilder |
154
|
|
|
{ |
155
|
|
|
$builder = clone $this; |
156
|
|
|
$builder->product->getExtensionAttributes()->getStockItem()->setIsInStock($inStock); |
157
|
|
|
return $builder; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function withStockQty($qty) : ProductBuilder |
161
|
|
|
{ |
162
|
|
|
$builder = clone $this; |
163
|
|
|
$builder->product->getExtensionAttributes()->getStockItem()->setQty($qty); |
164
|
|
|
return $builder; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function withCustomAttributes(array $values) : ProductBuilder |
168
|
|
|
{ |
169
|
|
|
$builder = clone $this; |
170
|
|
|
foreach ($values as $code => $value) { |
171
|
|
|
$builder->product->setCustomAttribute($code, $value); |
172
|
|
|
} |
173
|
|
|
return $builder; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function build() : ProductInterface |
177
|
|
|
{ |
178
|
|
|
FulltextIndex::ensureTablesAreCreated(); |
179
|
|
|
$builder = clone $this; |
180
|
|
|
if (!$builder->product->getSku()) { |
181
|
|
|
$builder->product->setSku(sha1(uniqid('', true))); |
182
|
|
|
} |
183
|
|
|
$builder->product->addData([ |
184
|
|
|
'url_key' => $builder->product->getSku() |
185
|
|
|
]); |
186
|
|
|
$product = $builder->productRepository->save($builder->product); |
187
|
|
|
foreach ($builder->websiteIds as $websiteId) { |
188
|
|
|
/** @var ProductWebsiteLinkInterface $websiteLink */ |
189
|
|
|
$websiteLink = $builder->websiteLinkFactory->create(); |
190
|
|
|
$websiteLink->setWebsiteId($websiteId)->setSku($product->getSku()); |
191
|
|
|
$builder->websiteLinkRepository->save($websiteLink); |
192
|
|
|
} |
193
|
|
|
return $product; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
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