@@ -57,30 +57,53 @@ 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 );
81
103
82
- foreach ($ configurableProducts as $ configurableProduct ) {
83
- $ this ->assertContains ($ configurableProduct ->getId (), $ result );
104
+ foreach ($ childIds as $ childId ) {
105
+ $ this ->assertArrayHasKey ($ childId , $ result );
106
+ $ this ->assertContains ($ result [$ childId ], $ parentIdsOfChildIds [$ childId ]);
84
107
}
85
108
}
86
109
}
0 commit comments