@@ -57,30 +57,69 @@ protected function setUp(): void
57
57
*/
58
58
public function testGetRelationsByChildren (): void
59
59
{
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 ' )
63
75
->create ();
64
- $ productOptions = $ this ->productRepository ->getList ($ searchCriteria )
76
+ $ configurableProducts = $ this ->productRepository ->getList ($ searchCriteria )
65
77
->getItems ();
66
78
67
- $ productOptionsIds = [];
79
+ $ childIds = [];
68
80
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
+ }
71
87
}
72
88
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
+ }
78
97
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 );
81
105
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 );
84
119
}
120
+
121
+ ksort ($ array , SORT_NUMERIC );
122
+
123
+ return $ array ;
85
124
}
86
125
}
0 commit comments