@@ -44,7 +44,7 @@ protected function setUp(): void
44
44
{
45
45
$ this ->configurableMock = $ this ->createPartialMock (
46
46
Configurable::class,
47
- ['getChildrenIds ' ]
47
+ ['getChildrenIds ' , ' getParentIdsByChild ' ]
48
48
);
49
49
$ this ->configurableProductsProviderMock = $ this ->createPartialMock (
50
50
ConfigurableProductsProvider::class,
@@ -61,29 +61,38 @@ protected function setUp(): void
61
61
/**
62
62
* @return void
63
63
*/
64
- public function testAfterGetMatchingProductIdsWithSimpleProduct ()
64
+ public function testAroundGetMatchingProductIdsWithSimpleProduct ()
65
65
{
66
66
$ this ->configurableProductsProviderMock ->expects ($ this ->once ())->method ('getIds ' )->willReturn ([]);
67
67
$ this ->configurableMock ->expects ($ this ->never ())->method ('getChildrenIds ' );
68
+ $ this ->ruleMock ->expects ($ this ->never ())
69
+ ->method ('setProductsFilter ' );
68
70
69
71
$ productIds = ['product ' => 'valid results ' ];
70
72
$ this ->assertEquals (
71
73
$ productIds ,
72
- $ this ->configurableProductHandler ->afterGetMatchingProductIds ($ this ->ruleMock , $ productIds )
74
+ $ this ->configurableProductHandler ->aroundGetMatchingProductIds (
75
+ $ this ->ruleMock ,
76
+ function () {
77
+ return ['product ' => 'valid results ' ];
78
+ }
79
+ )
73
80
);
74
81
}
75
82
76
83
/**
77
84
* @return void
78
85
*/
79
- public function testAfterGetMatchingProductIdsWithConfigurableProduct ()
86
+ public function testAroundGetMatchingProductIdsWithConfigurableProduct ()
80
87
{
81
88
$ this ->configurableProductsProviderMock ->expects ($ this ->once ())->method ('getIds ' )
82
89
->willReturn (['conf1 ' , 'conf2 ' ]);
83
90
$ this ->configurableMock ->expects ($ this ->any ())->method ('getChildrenIds ' )->willReturnMap ([
84
91
['conf1 ' , true , [ 0 => ['simple1 ' ]]],
85
92
['conf2 ' , true , [ 0 => ['simple1 ' , 'simple2 ' ]]],
86
93
]);
94
+ $ this ->ruleMock ->expects ($ this ->never ())
95
+ ->method ('setProductsFilter ' );
87
96
88
97
$ this ->assertEquals (
89
98
[
@@ -96,21 +105,118 @@ public function testAfterGetMatchingProductIdsWithConfigurableProduct()
96
105
3 => true ,
97
106
]
98
107
],
99
- $ this ->configurableProductHandler ->afterGetMatchingProductIds (
108
+ $ this ->configurableProductHandler ->aroundGetMatchingProductIds (
100
109
$ this ->ruleMock ,
101
- [
102
- 'conf1 ' => [
103
- 0 => true ,
104
- 1 => true ,
105
- ],
106
- 'conf2 ' => [
107
- 0 => false ,
108
- 1 => false ,
109
- 3 => true ,
110
- 4 => false ,
111
- ],
112
- ]
110
+ function () {
111
+ return [
112
+ 'conf1 ' => [
113
+ 0 => true ,
114
+ 1 => true ,
115
+ ],
116
+ 'conf2 ' => [
117
+ 0 => false ,
118
+ 1 => false ,
119
+ 3 => true ,
120
+ 4 => false ,
121
+ ],
122
+ ];
123
+ }
113
124
)
114
125
);
115
126
}
127
+
128
+ /**
129
+ * @param array $productsFilter
130
+ * @param array $expectedProductsFilter
131
+ * @param array $matchingProductIds
132
+ * @param array $expectedMatchingProductIds
133
+ * @return void
134
+ * @dataProvider aroundGetMatchingProductIdsDataProvider
135
+ */
136
+ public function testAroundGetMatchingProductIdsWithProductsFilter (
137
+ array $ productsFilter ,
138
+ array $ expectedProductsFilter ,
139
+ array $ matchingProductIds ,
140
+ array $ expectedMatchingProductIds
141
+ ): void {
142
+ $ configurableProducts = [
143
+ 'conf1 ' => ['simple11 ' , 'simple12 ' ],
144
+ 'conf2 ' => ['simple21 ' , 'simple22 ' ],
145
+ ];
146
+ $ this ->configurableProductsProviderMock ->method ('getIds ' )
147
+ ->willReturnCallback (
148
+ function ($ ids ) use ($ configurableProducts ) {
149
+ return array_intersect ($ ids , array_keys ($ configurableProducts ));
150
+ }
151
+ );
152
+ $ this ->configurableMock ->method ('getChildrenIds ' )
153
+ ->willReturnCallback (
154
+ function ($ id ) use ($ configurableProducts ) {
155
+ return [0 => $ configurableProducts [$ id ] ?? []];
156
+ }
157
+ );
158
+
159
+ $ this ->configurableMock ->method ('getParentIdsByChild ' )
160
+ ->willReturnCallback (
161
+ function ($ ids ) use ($ configurableProducts ) {
162
+ $ result = [];
163
+ foreach ($ configurableProducts as $ configurableProduct => $ childProducts ) {
164
+ if (array_intersect ($ ids , $ childProducts )) {
165
+ $ result [] = $ configurableProduct ;
166
+ }
167
+ }
168
+ return $ result ;
169
+ }
170
+ );
171
+
172
+ $ this ->ruleMock ->method ('getProductsFilter ' )
173
+ ->willReturn ($ productsFilter );
174
+
175
+ $ this ->ruleMock ->expects ($ this ->once ())
176
+ ->method ('setProductsFilter ' )
177
+ ->willReturn ($ expectedProductsFilter );
178
+
179
+ $ this ->assertEquals (
180
+ $ expectedMatchingProductIds ,
181
+ $ this ->configurableProductHandler ->aroundGetMatchingProductIds (
182
+ $ this ->ruleMock ,
183
+ function () use ($ matchingProductIds ) {
184
+ return $ matchingProductIds ;
185
+ }
186
+ )
187
+ );
188
+ }
189
+
190
+ /**
191
+ * @return array[]
192
+ */
193
+ public function aroundGetMatchingProductIdsDataProvider (): array
194
+ {
195
+ return [
196
+ [
197
+ ['simple1 ' ,],
198
+ ['simple1 ' ,],
199
+ ['simple1 ' => [1 => false ]],
200
+ ['simple1 ' => [1 => false ],],
201
+ ],
202
+ [
203
+ ['simple11 ' ,],
204
+ ['simple11 ' , 'conf1 ' ,],
205
+ ['simple11 ' => [1 => false ], 'conf1 ' => [1 => true ],],
206
+ ['simple11 ' => [1 => true ],],
207
+ ],
208
+ [
209
+ ['simple11 ' , 'simple12 ' ,],
210
+ ['simple11 ' , 'conf1 ' ,],
211
+ ['simple11 ' => [1 => false ], 'conf1 ' => [1 => true ],],
212
+ ['simple11 ' => [1 => true ], 'simple12 ' => [1 => true ],],
213
+ ],
214
+ [
215
+ ['conf1 ' , 'simple11 ' , 'simple12 ' ],
216
+ ['conf1 ' , 'simple11 ' , 'simple12 ' ],
217
+ ['conf1 ' => [1 => true ], 'simple11 ' => [1 => false ], 'simple12 ' => [1 => false ]],
218
+ ['simple11 ' => [1 => true ], 'simple12 ' => [1 => true ]],
219
+ ],
220
+ ];
221
+ }
116
222
}
0 commit comments