77
88namespace 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 ;
1214use Magento \TestFramework \Fixture \Data \ProcessorInterface ;
1315use Magento \TestFramework \Fixture \RevertibleDataFixtureInterface ;
1416
15- /**
16- * Creates category fixture
17- */
1817class 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