1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TddWizard\Fixtures\Catalog; |
5
|
|
|
|
6
|
|
|
use Magento\Catalog\Api\CategoryLinkRepositoryInterface; |
7
|
|
|
use Magento\Catalog\Api\CategoryRepositoryInterface; |
8
|
|
|
use Magento\Catalog\Api\Data\CategoryInterface; |
9
|
|
|
use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory; |
|
|
|
|
10
|
|
|
use Magento\Catalog\Model\Category; |
11
|
|
|
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; |
12
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
13
|
|
|
|
14
|
|
|
class CategoryBuilder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CategoryRepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $categoryRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CategoryResource |
23
|
|
|
*/ |
24
|
|
|
private $categoryResource; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CategoryLinkRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $categoryLinkRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CategoryProductLinkInterfaceFactory |
33
|
|
|
*/ |
34
|
|
|
private $productLinkFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Category |
38
|
|
|
*/ |
39
|
|
|
private $category; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
private $skus; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param CategoryRepositoryInterface $categoryRepository |
48
|
|
|
* @param CategoryResource $categoryResource |
49
|
|
|
* @param CategoryLinkRepositoryInterface $categoryLinkRepository |
50
|
|
|
* @param CategoryProductLinkInterfaceFactory $productLinkFactory |
51
|
|
|
* @param Category $category |
52
|
|
|
* @param string[] $skus |
53
|
|
|
*/ |
54
|
8 |
|
public function __construct( |
55
|
|
|
CategoryRepositoryInterface $categoryRepository, |
56
|
|
|
CategoryResource $categoryResource, |
57
|
|
|
CategoryLinkRepositoryInterface $categoryLinkRepository, |
58
|
|
|
CategoryProductLinkInterfaceFactory $productLinkFactory, |
59
|
|
|
Category $category, |
60
|
|
|
array $skus |
61
|
|
|
) { |
62
|
8 |
|
$this->categoryRepository = $categoryRepository; |
63
|
8 |
|
$this->categoryResource = $categoryResource; |
64
|
8 |
|
$this->categoryLinkRepository = $categoryLinkRepository; |
65
|
8 |
|
$this->productLinkFactory = $productLinkFactory; |
66
|
8 |
|
$this->category = $category; |
67
|
8 |
|
$this->skus = $skus; |
68
|
8 |
|
} |
69
|
|
|
|
70
|
8 |
|
public static function topLevelCategory(): CategoryBuilder |
71
|
|
|
{ |
72
|
8 |
|
$objectManager = Bootstrap::getObjectManager(); |
73
|
|
|
|
74
|
|
|
// use interface to reflect DI configuration but assume instance of the real model because we need its methods |
75
|
|
|
/** @var Category $category */ |
76
|
8 |
|
$category = $objectManager->create(CategoryInterface::class); |
77
|
|
|
|
78
|
8 |
|
$category->setName('Top Level Category'); |
79
|
8 |
|
$category->setIsActive(true); |
80
|
8 |
|
$category->setPath('1/2'); |
81
|
|
|
|
82
|
8 |
|
return new self( |
83
|
8 |
|
$objectManager->create(CategoryRepositoryInterface::class), |
84
|
8 |
|
$objectManager->create(CategoryResource::class), |
85
|
8 |
|
$objectManager->create(CategoryLinkRepositoryInterface::class), |
86
|
8 |
|
$objectManager->create(CategoryProductLinkInterfaceFactory::class), |
87
|
|
|
$category, |
88
|
8 |
|
[] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public static function childCategoryOf( |
93
|
|
|
CategoryFixture $parent |
94
|
|
|
): CategoryBuilder { |
95
|
1 |
|
$objectManager = Bootstrap::getObjectManager(); |
96
|
|
|
// use interface to reflect DI configuration but assume instance of the real model because we need its methods |
97
|
|
|
/** @var Category $category */ |
98
|
1 |
|
$category = $objectManager->create(CategoryInterface::class); |
99
|
|
|
|
100
|
1 |
|
$category->setName('Child Category'); |
101
|
1 |
|
$category->setIsActive(true); |
102
|
1 |
|
$category->setPath((string)$parent->getCategory()->getPath()); |
103
|
|
|
|
104
|
1 |
|
return new self( |
105
|
1 |
|
$objectManager->create(CategoryRepositoryInterface::class), |
106
|
1 |
|
$objectManager->create(CategoryResource::class), |
107
|
1 |
|
$objectManager->create(CategoryLinkRepositoryInterface::class), |
108
|
1 |
|
$objectManager->create(CategoryProductLinkInterfaceFactory::class), |
109
|
|
|
$category, |
110
|
1 |
|
[] |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Assigns products by sku. The keys of the array will be used for the sort position |
116
|
|
|
* |
117
|
|
|
* @param string[] $skus |
118
|
|
|
* @return CategoryBuilder |
119
|
|
|
*/ |
120
|
1 |
|
public function withProducts(array $skus): CategoryBuilder |
121
|
|
|
{ |
122
|
1 |
|
$builder = clone $this; |
123
|
1 |
|
$builder->skus = $skus; |
124
|
1 |
|
return $builder; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
public function withDescription(string $description): CategoryBuilder |
128
|
|
|
{ |
129
|
1 |
|
$builder = clone $this; |
130
|
1 |
|
$builder->category->setCustomAttribute('description', $description); |
131
|
1 |
|
return $builder; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function withName(string $name): CategoryBuilder |
135
|
|
|
{ |
136
|
1 |
|
$builder = clone $this; |
137
|
1 |
|
$builder->category->setName($name); |
138
|
1 |
|
return $builder; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function withUrlKey(string $urlKey): CategoryBuilder |
142
|
|
|
{ |
143
|
1 |
|
$builder = clone $this; |
144
|
1 |
|
$builder->category->setData('url_key', $urlKey); |
145
|
1 |
|
return $builder; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
public function withIsActive(bool $isActive): CategoryBuilder |
149
|
|
|
{ |
150
|
1 |
|
$builder = clone $this; |
151
|
1 |
|
$builder->category->setIsActive($isActive); |
152
|
1 |
|
return $builder; |
153
|
|
|
} |
154
|
|
|
|
155
|
8 |
|
public function __clone() |
156
|
|
|
{ |
157
|
8 |
|
$this->category = clone $this->category; |
158
|
8 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Category |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
8 |
|
public function build(): Category |
165
|
|
|
{ |
166
|
8 |
|
$builder = clone $this; |
167
|
|
|
|
168
|
8 |
|
if (!$builder->category->getData('url_key')) { |
169
|
7 |
|
$builder->category->setData('url_key', sha1(uniqid('', true))); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Save with global scope if not specified otherwise |
173
|
8 |
|
if (!$builder->category->hasData('store_id')) { |
174
|
8 |
|
$builder->category->setStoreId(0); |
175
|
|
|
} |
176
|
8 |
|
$builder->categoryResource->save($builder->category); |
177
|
|
|
|
178
|
8 |
|
foreach ($builder->skus as $position => $sku) { |
179
|
1 |
|
$productLink = $builder->productLinkFactory->create(); |
180
|
1 |
|
$productLink->setSku($sku); |
181
|
1 |
|
$productLink->setPosition($position); |
182
|
1 |
|
$productLink->setCategoryId($builder->category->getId()); |
183
|
1 |
|
$builder->categoryLinkRepository->save($productLink); |
184
|
|
|
} |
185
|
8 |
|
return $builder->category; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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