@@ -57,30 +57,69 @@ protected function setUp(): void
5757 */
5858 public function testGetRelationsByChildren (): void
5959 {
60- // Find configurable products options
61- $ productOptionSkus = ['simple_10 ' , 'simple_20 ' , 'simple_30 ' , 'simple_40 ' ];
62- $ searchCriteria = $ this ->searchCriteriaBuilder ->addFilter ('sku ' , $ productOptionSkus , 'in ' )
60+ $ childSkusOfParentSkus = [
61+ 'configurable ' => ['simple_10 ' , 'simple_20 ' ],
62+ 'configurable_12345 ' => ['simple_30 ' , 'simple_40 ' ],
63+ ];
64+ $ configurableSkus = [
65+ 'configurable ' ,
66+ 'configurable_12345 ' ,
67+ 'simple_10 ' ,
68+ 'simple_20 ' ,
69+ 'simple_30 ' ,
70+ 'simple_40 ' ,
71+ ];
72+ $ configurableIdsOfSkus = [];
73+
74+ $ searchCriteria = $ this ->searchCriteriaBuilder ->addFilter ('sku ' , $ configurableSkus , 'in ' )
6375 ->create ();
64- $ productOptions = $ this ->productRepository ->getList ($ searchCriteria )
76+ $ configurableProducts = $ this ->productRepository ->getList ($ searchCriteria )
6577 ->getItems ();
6678
67- $ productOptionsIds = [];
79+ $ childIds = [];
6880
69- foreach ($ productOptions as $ productOption ) {
70- $ productOptionsIds [] = $ productOption ->getId ();
81+ foreach ($ configurableProducts as $ product ) {
82+ $ configurableIdsOfSkus [$ product ->getSku ()] = $ product ->getId ();
83+
84+ if ($ product ->getTypeId () != 'configurable ' ) {
85+ $ childIds [] = $ product ->getId ();
86+ }
7187 }
7288
73- // Find configurable products
74- $ searchCriteria = $ this ->searchCriteriaBuilder ->addFilter ('sku ' , ['configurable ' , 'configurable_12345 ' ], 'in ' )
75- ->create ();
76- $ configurableProducts = $ this ->productRepository ->getList ($ searchCriteria )
77- ->getItems ();
89+ $ parentIdsOfChildIds = [];
90+
91+ foreach ($ childSkusOfParentSkus as $ parentSku => $ childSkus ) {
92+ foreach ($ childSkus as $ childSku ) {
93+ $ childId = $ configurableIdsOfSkus [$ childSku ];
94+ $ parentIdsOfChildIds [$ childId ][] = $ configurableIdsOfSkus [$ parentSku ];
95+ }
96+ }
7897
79- // Assert there are configurable products ids in result of getRelationsByChildren method.
80- $ result = $ this ->model ->getRelationsByChildren ($ productOptionsIds );
98+ /**
99+ * Assert there are parent configurable products ids in result of getRelationsByChildren method
100+ * and they are related to child ids.
101+ */
102+ $ result = $ this ->model ->getRelationsByChildren ($ childIds );
103+ $ sortedResult = $ this ->sortParentIdsOfChildIds ($ result );
104+ $ sortedExpected = $ this ->sortParentIdsOfChildIds ($ parentIdsOfChildIds );
81105
82- foreach ($ configurableProducts as $ configurableProduct ) {
83- $ this ->assertContains ($ configurableProduct ->getId (), $ result );
106+ $ this ->assertEquals ($ sortedExpected , $ sortedResult );
107+ }
108+
109+ /**
110+ * Sorts the "Parent Ids Of Child Ids" type of the array
111+ *
112+ * @param array $array
113+ * @return array
114+ */
115+ private function sortParentIdsOfChildIds (array $ array ): array
116+ {
117+ foreach ($ array as $ childId => &$ parentIds ) {
118+ sort ($ parentIds , SORT_NUMERIC );
84119 }
120+
121+ ksort ($ array , SORT_NUMERIC );
122+
123+ return $ array ;
85124 }
86125}
0 commit comments