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