Skip to content

Commit 32fb867

Browse files
authored
Merge pull request #43 from tddwizard/category-builder-improvements
Save category with global scope via resource model
2 parents e1936c1 + 093dfe9 commit 32fb867

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Catalog/CategoryBuilder.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
66
use Magento\Catalog\Api\CategoryRepositoryInterface;
7+
use Magento\Catalog\Model\Category;
8+
use \Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
79
use Magento\Catalog\Api\Data\CategoryInterface;
810
use Magento\Catalog\Api\Data\CategoryProductLinkInterface;
911
use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory;
@@ -21,6 +23,11 @@ class CategoryBuilder
2123
*/
2224
private $categoryRepository;
2325

26+
/**
27+
* @var CategoryResource
28+
*/
29+
private $categoryResource;
30+
2431
/**
2532
* @var CategoryLinkRepositoryInterface
2633
*/
@@ -38,12 +45,14 @@ class CategoryBuilder
3845

3946
public function __construct(
4047
CategoryRepositoryInterface $categoryRepository,
48+
CategoryResource $categoryResource,
4149
CategoryLinkRepositoryInterface $categoryLinkRepository,
4250
CategoryProductLinkInterfaceFactory $productLinkFactory,
4351
CategoryInterface $category,
4452
array $skus
4553
) {
4654
$this->categoryRepository = $categoryRepository;
55+
$this->categoryResource = $categoryResource;
4756
$this->categoryLinkRepository = $categoryLinkRepository;
4857
$this->category = $category;
4958
$this->skus = $skus;
@@ -60,9 +69,11 @@ public static function topLevelCategory(ObjectManagerInterface $objectManager =
6069

6170
$category->setName('Top Level Category');
6271
$category->setIsActive(true);
72+
$category->setPath('1/2');
6373

6474
return new self(
6575
$objectManager->create(CategoryRepositoryInterface::class),
76+
$objectManager->create(CategoryResource::class),
6677
$objectManager->create(CategoryLinkRepositoryInterface::class),
6778
$objectManager->create(CategoryProductLinkInterfaceFactory::class),
6879
$category,
@@ -83,10 +94,11 @@ public static function childCategoryOf(
8394

8495
$category->setName('Child Category');
8596
$category->setIsActive(true);
86-
$category->setParentId($parent->getId());
97+
$category->setPath($parent->getCategory()->getPath());
8798

8899
return new self(
89100
$objectManager->create(CategoryRepositoryInterface::class),
101+
$objectManager->create(CategoryResource::class),
90102
$objectManager->create(CategoryLinkRepositoryInterface::class),
91103
$objectManager->create(CategoryProductLinkInterfaceFactory::class),
92104
$category,
@@ -147,16 +159,20 @@ public function build() : CategoryInterface
147159
$builder->category->setData('url_key', sha1(uniqid('', true)));
148160
}
149161

150-
$category = $builder->categoryRepository->save($builder->category);
162+
// Save with global scope if not specified otherwise
163+
if ($builder->category instanceof Category && !$builder->category->hasData('store_id')) {
164+
$builder->category->setStoreId(0);
165+
}
166+
$builder->categoryResource->save($builder->category);
151167

152168
foreach ($builder->skus as $position => $sku) {
153169
/** @var CategoryProductLinkInterface $productLink */
154170
$productLink = $builder->productLinkFactory->create();
155171
$productLink->setSku($sku);
156172
$productLink->setPosition($position);
157-
$productLink->setCategoryId($category->getId());
173+
$productLink->setCategoryId($builder->category->getId());
158174
$builder->categoryLinkRepository->save($productLink);
159175
}
160-
return $category;
176+
return $builder->category;
161177
}
162178
}

src/Catalog/CategoryFixture.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class CategoryFixture
1212
*/
1313
private $category;
1414

15+
/**
16+
* @return CategoryInterface
17+
*/
18+
public function getCategory(): CategoryInterface
19+
{
20+
return $this->category;
21+
}
22+
1523
public function __construct(CategoryInterface $category)
1624
{
1725
$this->category = $category;

0 commit comments

Comments
 (0)