Skip to content

Commit f3b15ad

Browse files
authored
Merge pull request #407 from magento-l3/MC-40746
MC-40746: Add support for variables in fixture parameters
2 parents 7042c53 + 1a3e06d commit f3b15ad

File tree

40 files changed

+1476
-679
lines changed

40 files changed

+1476
-679
lines changed

app/code/Magento/Catalog/Test/Fixture/AddAttributeToAttributeSet.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
use Magento\Catalog\Api\ProductAttributeManagementInterface;
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Eav\Setup\EavSetup;
13+
use Magento\Framework\DataObject;
1314
use Magento\TestFramework\Fixture\Api\ServiceFactory;
1415
use Magento\TestFramework\Fixture\DataFixtureInterface;
1516

16-
/**
17-
* Add product attribute to attribute set
18-
*/
1917
class AddAttributeToAttributeSet implements DataFixtureInterface
2018
{
2119
/**
@@ -43,7 +41,7 @@ public function __construct(
4341
/**
4442
* @inheritdoc
4543
*/
46-
public function apply(array $data = []): ?array
44+
public function apply(array $data = []): ?DataObject
4745
{
4846
$attributeSetId = $this->eavSetup->getAttributeSetId(Product::ENTITY, 'Default');
4947
$attributeGroupId = $this->eavSetup->getDefaultAttributeGroupId(Product::ENTITY, $attributeSetId);
@@ -60,6 +58,6 @@ public function apply(array $data = []): ?array
6058
$service = $this->serviceFactory->create(ProductAttributeManagementInterface::class, 'assign');
6159
$service->execute($data);
6260

63-
return $data;
61+
return null;
6462
}
6563
}

app/code/Magento/Catalog/Test/Fixture/Attribute.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
namespace Magento\Catalog\Test\Fixture;
99

1010
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11+
use Magento\Framework\DataObject;
1112
use Magento\TestFramework\Fixture\Api\ServiceFactory;
1213
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
1314
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
1415

15-
/**
16-
* Creates product attribute fixture
17-
*/
1816
class Attribute implements RevertibleDataFixtureInterface
1917
{
2018
private const DEFAULT_DATA = [
@@ -74,31 +72,40 @@ public function __construct(
7472
/**
7573
* @inheritdoc
7674
*/
77-
public function apply(array $data = []): ?array
75+
public function apply(array $data = []): ?DataObject
7876
{
7977
$service = $this->serviceFactory->create(ProductAttributeRepositoryInterface::class, 'save');
80-
$fixtureData = array_merge(self::DEFAULT_DATA, $data);
81-
$result = $service->execute(
78+
79+
return $service->execute(
8280
[
83-
'attribute' => $this->dataProcessor->process($this, $fixtureData)
81+
'attribute' => $this->prepareData($data)
8482
]
8583
);
86-
87-
return [
88-
'attribute' => $result
89-
];
9084
}
9185

9286
/**
9387
* @inheritdoc
9488
*/
95-
public function revert(array $data = []): void
89+
public function revert(DataObject $data): void
9690
{
9791
$service = $this->serviceFactory->create(ProductAttributeRepositoryInterface::class, 'deleteById');
9892
$service->execute(
9993
[
100-
'attributeCode' => $data['attribute']->getAttributeCode()
94+
'attributeCode' => $data->getAttributeCode()
10195
]
10296
);
10397
}
98+
99+
/**
100+
* Prepare attribute data
101+
*
102+
* @param array $data
103+
* @return array
104+
*/
105+
private function prepareData(array $data): array
106+
{
107+
$data = array_merge(self::DEFAULT_DATA, $data);
108+
109+
return $this->dataProcessor->process($this, $data);
110+
}
104111
}

app/code/Magento/Catalog/Test/Fixture/Category.php

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,86 @@
77

88
namespace Magento\Catalog\Test\Fixture;
99

10-
use Magento\Catalog\Model\CategoryFactory;
11-
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Framework\DataObject;
12+
use Magento\TestFramework\Fixture\Api\DataMerger;
13+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
1214
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
1315
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
1416

15-
/**
16-
* Creates category fixture
17-
*/
1817
class Category implements RevertibleDataFixtureInterface
1918
{
20-
private const DEFAULT_PARENT_ID = 2;
21-
22-
private const DEFAULT_PARENT_PATH = '1/2';
23-
2419
private const DEFAULT_DATA = [
20+
'id' => null,
2521
'name' => 'Category%uniqid%',
26-
'parent_id' => self::DEFAULT_PARENT_ID,
22+
'parent_id' => 2,
2723
'is_active' => true,
2824
'position' => 1,
29-
'available_sort_by' => ['position', 'name'],
30-
'default_sort_by' => 'name'
25+
'level' => 1,
26+
'path' => null,
27+
'include_in_menu' => true,
28+
'available_sort_by' => [],
29+
'custom_attributes' => [
30+
'default_sort_by' => ['name']
31+
],
32+
'extension_attributes' => [],
33+
'created_at' => null,
34+
'updated_at' => null,
3135
];
3236

3337
/**
34-
* @var CategoryFactory
35-
*/
36-
private $categoryFactory;
37-
38-
/**
39-
* @var CategoryResource
38+
* @var ServiceFactory
4039
*/
41-
private $categoryResource;
40+
private $serviceFactory;
4241

4342
/**
4443
* @var ProcessorInterface
4544
*/
4645
private $dataProcessor;
46+
/**
47+
* @var DataMerger
48+
*/
49+
private $dataMerger;
4750

4851
/**
49-
* @param CategoryFactory $categoryFactory
50-
* @param CategoryResource $categoryResource
52+
* @param ServiceFactory $serviceFactory
5153
* @param ProcessorInterface $dataProcessor
5254
*/
5355
public function __construct(
54-
CategoryFactory $categoryFactory,
55-
CategoryResource $categoryResource,
56-
ProcessorInterface $dataProcessor
56+
ServiceFactory $serviceFactory,
57+
ProcessorInterface $dataProcessor,
58+
DataMerger $dataMerger
5759
) {
58-
$this->categoryFactory = $categoryFactory;
59-
$this->categoryResource = $categoryResource;
60+
$this->serviceFactory = $serviceFactory;
6061
$this->dataProcessor = $dataProcessor;
62+
$this->dataMerger = $dataMerger;
6163
}
6264

6365
/**
6466
* @inheritdoc
6567
*/
66-
public function apply(array $data = []): ?array
68+
public function apply(array $data = []): ?DataObject
6769
{
68-
$data = $this->prepareData($data);
69-
/** @var \Magento\Catalog\Model\Category $category */
70-
$category = $this->categoryFactory->create();
71-
$category->isObjectNew(true);
72-
$category->setData($data);
73-
if (isset($data['id'])) {
74-
$category->setId($data['id']);
75-
}
76-
$this->categoryResource->save($category);
70+
$service = $this->serviceFactory->create(CategoryRepositoryInterface::class, 'save');
71+
72+
return $service->execute(
73+
[
74+
'category' => $this->prepareData($data)
75+
]
76+
);
77+
}
7778

78-
return [
79-
'category' => $category
80-
];
79+
/**
80+
* @inheritdoc
81+
*/
82+
public function revert(DataObject $data): void
83+
{
84+
$service = $this->serviceFactory->create(CategoryRepositoryInterface::class, 'deleteByIdentifier');
85+
$service->execute(
86+
[
87+
'categoryId' => $data->getId()
88+
]
89+
);
8190
}
8291

8392
/**
@@ -88,32 +97,8 @@ public function apply(array $data = []): ?array
8897
*/
8998
private function prepareData(array $data): array
9099
{
91-
$data = $this->dataProcessor->process($this, array_merge(self::DEFAULT_DATA, $data));
92-
if (!isset($data['path'])) {
93-
$data['path'] = self::DEFAULT_PARENT_PATH;
94-
if ((int) $data['parent_id'] !== self::DEFAULT_PARENT_ID) {
95-
/** @var \Magento\Catalog\Model\Category $parentCategory */
96-
$parentCategory = $this->categoryFactory->create();
97-
$this->categoryResource->load($parentCategory, $data['parent_id']);
98-
$data['path'] = $parentCategory->getPath();
99-
}
100-
if (isset($data['id'])) {
101-
$data['path'] .= '/' . $data['id'];
102-
}
103-
}
104-
return $data;
105-
}
100+
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data);
106101

107-
/**
108-
* @inheritdoc
109-
*/
110-
public function revert(array $data = []): void
111-
{
112-
/** @var \Magento\Catalog\Model\Category $category */
113-
$category = $this->categoryFactory->create();
114-
$this->categoryResource->load($category, $data['category']->getId());
115-
if ($category->getId()) {
116-
$this->categoryResource->delete($category);
117-
}
102+
return $this->dataProcessor->process($this, $data);
118103
}
119104
}

0 commit comments

Comments
 (0)