3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Catalog \Test \Unit \Model \ResourceModel \Product ;
7
8
8
9
use Magento \Catalog \Model \ResourceModel \Product \CategoryLink ;
@@ -129,9 +130,65 @@ public function testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affe
129
130
);
130
131
}
131
132
133
+ $ expectedResult = [];
134
+
135
+ foreach ($ affectedIds as $ type => $ ids ) {
136
+ $ expectedResult = array_merge ($ expectedResult , $ ids );
137
+
138
+ // Verify if the correct insert, update and/or delete actions are performed:
139
+ switch ($ type ) {
140
+ case 'insert ' :
141
+ $ this ->connectionMock
142
+ ->expects ($ this ->exactly (empty ($ ids ) ? 0 : 1 ))
143
+ ->method ('insertArray ' )
144
+ ->with (
145
+ $ this ->anything (),
146
+ $ this ->anything (),
147
+ $ this ->callback (function ($ data ) use ($ ids ) {
148
+ $ foundIds = [];
149
+ foreach ($ data as $ row ) {
150
+ $ foundIds [] = $ row ['category_id ' ];
151
+ }
152
+ return $ ids === $ foundIds ;
153
+ })
154
+ );
155
+ break ;
156
+ case 'update ' :
157
+ $ this ->connectionMock
158
+ ->expects ($ this ->exactly (empty ($ ids ) ? 0 : 1 ))
159
+ ->method ('insertOnDuplicate ' )
160
+ ->with (
161
+ $ this ->anything (),
162
+ $ this ->callback (function ($ data ) use ($ ids ) {
163
+ $ foundIds = [];
164
+ foreach ($ data as $ row ) {
165
+ $ foundIds [] = $ row ['category_id ' ];
166
+ }
167
+ return $ ids === $ foundIds ;
168
+ })
169
+ );
170
+ break ;
171
+ case 'delete ' :
172
+ $ this ->connectionMock
173
+ ->expects ($ this ->exactly (empty ($ ids ) ? 0 : 1 ))
174
+ ->method ('delete ' )
175
+ // Verify that the correct category ID's are touched:
176
+ ->with (
177
+ $ this ->anything (),
178
+ $ this ->callback (function ($ data ) use ($ ids ) {
179
+ return array_values ($ data )[1 ] === $ ids ;
180
+ })
181
+ );
182
+ break ;
183
+ }
184
+ }
185
+
132
186
$ actualResult = $ this ->model ->saveCategoryLinks ($ product , $ newCategoryLinks );
187
+
133
188
sort ($ actualResult );
134
- $ this ->assertEquals ($ affectedIds , $ actualResult );
189
+ sort ($ expectedResult );
190
+
191
+ $ this ->assertEquals ($ expectedResult , $ actualResult );
135
192
}
136
193
137
194
/**
@@ -151,7 +208,11 @@ public function getCategoryLinksDataProvider()
151
208
['category_id ' => 3 , 'position ' => 10 ],
152
209
['category_id ' => 4 , 'position ' => 20 ],
153
210
],
154
- [], // Nothing to update - data not changed
211
+ [
212
+ 'update ' => [],
213
+ 'insert ' => [],
214
+ 'delete ' => [],
215
+ ],
155
216
],
156
217
[
157
218
[
@@ -162,7 +223,11 @@ public function getCategoryLinksDataProvider()
162
223
['category_id ' => 3 , 'position ' => 10 ],
163
224
['category_id ' => 4 , 'position ' => 20 ],
164
225
],
165
- [3 , 4 , 5 ], // 4 - updated position, 5 - added, 3 - deleted
226
+ [
227
+ 'update ' => [4 ],
228
+ 'insert ' => [5 ],
229
+ 'delete ' => [3 ],
230
+ ],
166
231
],
167
232
[
168
233
[
@@ -173,16 +238,39 @@ public function getCategoryLinksDataProvider()
173
238
['category_id ' => 3 , 'position ' => 10 ],
174
239
['category_id ' => 4 , 'position ' => 20 ],
175
240
],
176
- [3 , 4 ], // 3 - updated position, 4 - deleted
241
+ [
242
+ 'update ' => [3 ],
243
+ 'insert ' => [],
244
+ 'delete ' => [4 ],
245
+ ],
177
246
],
178
247
[
179
248
[],
180
249
[
181
250
['category_id ' => 3 , 'position ' => 10 ],
182
251
['category_id ' => 4 , 'position ' => 20 ],
183
252
],
184
- [3 , 4 ], // 3, 4 - deleted
253
+ [
254
+ 'update ' => [],
255
+ 'insert ' => [],
256
+ 'delete ' => [3 , 4 ],
257
+ ],
185
258
],
259
+ [
260
+ [
261
+ ['category_id ' => 3 , 'position ' => 10 ],
262
+ ['category_id ' => 4 , 'position ' => 20 ],
263
+ ],
264
+ [
265
+ ['category_id ' => 3 , 'position ' => 20 ], // swapped positions
266
+ ['category_id ' => 4 , 'position ' => 10 ], // swapped positions
267
+ ],
268
+ [
269
+ 'update ' => [3 , 4 ],
270
+ 'insert ' => [],
271
+ 'delete ' => [],
272
+ ],
273
+ ]
186
274
];
187
275
}
188
276
}
0 commit comments