77
88namespace Magento \Catalog \Test \Fixture ;
99
10+ use Magento \Catalog \Api \Data \ProductInterface ;
1011use Magento \Catalog \Api \ProductRepositoryInterface ;
1112use Magento \Catalog \Model \Product \Attribute \Source \Status ;
1213use Magento \Catalog \Model \Product \Type ;
@@ -50,6 +51,12 @@ class Product implements RevertibleDataFixtureInterface
5051 'updated_at ' => null ,
5152 ];
5253
54+ private const DEFAULT_PRODUCT_LINK_DATA = [
55+ 'sku ' => null ,
56+ 'type ' => 'related ' ,
57+ 'position ' => 1 ,
58+ ];
59+
5360 /**
5461 * @var ServiceFactory
5562 */
@@ -65,18 +72,25 @@ class Product implements RevertibleDataFixtureInterface
6572 */
6673 private $ dataMerger ;
6774
75+ /**
76+ * @var ProductRepositoryInterface
77+ */
78+ private $ productRepository ;
79+
6880 /**
6981 * @param ServiceFactory $serviceFactory
7082 * @param ProcessorInterface $dataProcessor
7183 */
7284 public function __construct (
7385 ServiceFactory $ serviceFactory ,
7486 ProcessorInterface $ dataProcessor ,
75- DataMerger $ dataMerger
87+ DataMerger $ dataMerger ,
88+ ProductRepositoryInterface $ productRepository
7689 ) {
7790 $ this ->serviceFactory = $ serviceFactory ;
7891 $ this ->dataProcessor = $ dataProcessor ;
7992 $ this ->dataMerger = $ dataMerger ;
93+ $ this ->productRepository = $ productRepository ;
8094 }
8195
8296 /**
@@ -122,6 +136,49 @@ private function prepareData(array $data): array
122136 unset($ data ['extension_attributes ' ]['category_links ' ]);
123137 }
124138
139+ $ data ['product_links ' ] = $ this ->prepareLinksData ($ data );
140+
125141 return $ this ->dataProcessor ->process ($ this , $ data );
126142 }
143+
144+ /**
145+ * Prepare links data
146+ *
147+ * @param array $data
148+ * @return array
149+ */
150+ private function prepareLinksData (array $ data ): array
151+ {
152+ $ links = [];
153+
154+ $ position = 1 ;
155+ foreach ($ data ['product_links ' ] as $ link ) {
156+ $ defaultLinkData = self ::DEFAULT_PRODUCT_LINK_DATA ;
157+ $ defaultLinkData ['position ' ] = $ position ;
158+ $ linkData = [];
159+ if (is_numeric ($ link )) {
160+ $ product = $ this ->productRepository ->getById ($ link );
161+ } elseif (is_string ($ link )) {
162+ $ product = $ this ->productRepository ->get ($ link );
163+ } elseif ($ link instanceof ProductInterface) {
164+ $ product = $ this ->productRepository ->get ($ link ->getSku ());
165+ } else {
166+ $ linkData = $ link instanceof DataObject ? $ link ->toArray () : $ link ;
167+ $ product = $ this ->productRepository ->get ($ linkData ['sku ' ]);
168+ }
169+
170+ $ linkData += $ defaultLinkData ;
171+ $ links [] = [
172+ 'sku ' => $ data ['sku ' ],
173+ 'link_type ' => $ linkData ['type ' ],
174+ 'linked_product_sku ' => $ product ->getSku (),
175+ 'linked_product_type ' => $ product ->getTypeId (),
176+ 'position ' => $ linkData ['position ' ],
177+ 'extension_attributes ' => array_diff_key ($ linkData , $ defaultLinkData ),
178+ ];
179+ $ position ++;
180+ }
181+
182+ return $ links ;
183+ }
127184}
0 commit comments