12
12
use Magento \Customer \Api \Data \AddressSearchResultsInterfaceFactory ;
13
13
use Magento \Customer \Model \AddressFactory ;
14
14
use Magento \Customer \Model \AddressRegistry ;
15
+ use Magento \Customer \Model \Config \Share ;
15
16
use Magento \Customer \Model \Customer ;
16
17
use Magento \Customer \Model \CustomerRegistry ;
17
18
use Magento \Customer \Model \ResourceModel \Address ;
@@ -91,6 +92,11 @@ class AddressRepositoryTest extends TestCase
91
92
*/
92
93
private $ collectionProcessor ;
93
94
95
+ /**
96
+ * @var Share|MockObject
97
+ */
98
+ private $ configShare ;
99
+
94
100
protected function setUp (): void
95
101
{
96
102
$ this ->addressFactory = $ this ->createPartialMock (AddressFactory::class, ['create ' ]);
@@ -112,9 +118,20 @@ protected function setUp(): void
112
118
'' ,
113
119
false
114
120
);
115
- $ this ->customer = $ this ->createMock (Customer::class);
121
+ $ this ->customer = $ this ->createPartialMock (
122
+ Customer::class,
123
+ ['getSharingConfig ' ,'getAddressesCollection ' ]
124
+ );
116
125
$ this ->address = $ this ->getMockBuilder (\Magento \Customer \Model \Address::class)->addMethods (
117
- ['getCountryId ' , 'getFirstname ' , 'getLastname ' , 'getCity ' , 'getTelephone ' , 'getShouldIgnoreValidation ' ]
126
+ [
127
+ 'getCountryId ' ,
128
+ 'getFirstname ' ,
129
+ 'getLastname ' ,
130
+ 'getCity ' ,
131
+ 'getTelephone ' ,
132
+ 'getShouldIgnoreValidation ' ,
133
+ 'setStoreId '
134
+ ]
118
135
)
119
136
->onlyMethods (
120
137
[
@@ -148,6 +165,11 @@ protected function setUp(): void
148
165
$ this ->extensionAttributesJoinProcessor ,
149
166
$ this ->collectionProcessor
150
167
);
168
+ $ this ->configShare = $ this ->getMockBuilder (Share::class)->onlyMethods (
169
+ ['isWebsiteScope ' ]
170
+ )
171
+ ->disableOriginalConstructor ()
172
+ ->getMock ();
151
173
}
152
174
153
175
public function testSave ()
@@ -212,6 +234,146 @@ public function testSave()
212
234
$ this ->repository ->save ($ customerAddress );
213
235
}
214
236
237
+ public function testSaveWithConfigCustomerAccountShareScopeWebsite ()
238
+ {
239
+ $ customerId = 34 ;
240
+ $ addressId = 53 ;
241
+ $ customerAddress = $ this ->getMockForAbstractClass (
242
+ AddressInterface::class,
243
+ [],
244
+ '' ,
245
+ false
246
+ );
247
+ $ addressCollection =
248
+ $ this ->createMock (Collection::class);
249
+ $ customerAddress ->expects ($ this ->atLeastOnce ())
250
+ ->method ('getCustomerId ' )
251
+ ->willReturn ($ customerId );
252
+ $ customerAddress ->expects ($ this ->atLeastOnce ())
253
+ ->method ('getId ' )
254
+ ->willReturn ($ addressId );
255
+ $ this ->customerRegistry ->expects ($ this ->once ())
256
+ ->method ('retrieve ' )
257
+ ->with ($ customerId )
258
+ ->willReturn ($ this ->customer );
259
+ $ this ->address ->expects ($ this ->atLeastOnce ())
260
+ ->method ("getId " )
261
+ ->willReturn ($ addressId );
262
+ $ this ->addressRegistry ->expects ($ this ->once ())
263
+ ->method ('retrieve ' )
264
+ ->with ($ addressId )
265
+ ->willReturn (null );
266
+ $ this ->addressFactory ->expects ($ this ->once ())
267
+ ->method ('create ' )
268
+ ->willReturn ($ this ->address );
269
+ $ this ->address ->expects ($ this ->once ())
270
+ ->method ('updateData ' )
271
+ ->with ($ customerAddress );
272
+ $ this ->address ->expects ($ this ->once ())
273
+ ->method ('setCustomer ' )
274
+ ->with ($ this ->customer );
275
+ $ this ->customer ->expects ($ this ->exactly (2 ))
276
+ ->method ('getSharingConfig ' )
277
+ ->willReturn ($ this ->configShare );
278
+ $ this ->configShare ->expects ($ this ->once ())
279
+ ->method ('isWebsiteScope ' )
280
+ ->willReturn (true );
281
+ $ this ->address ->expects ($ this ->once ())
282
+ ->method ('setStoreId ' );
283
+ $ this ->address ->expects ($ this ->once ())
284
+ ->method ('validate ' )
285
+ ->willReturn (true );
286
+ $ this ->address ->expects ($ this ->once ())
287
+ ->method ('save ' );
288
+ $ this ->addressRegistry ->expects ($ this ->once ())
289
+ ->method ('push ' )
290
+ ->with ($ this ->address );
291
+ $ this ->customer ->expects ($ this ->exactly (2 ))
292
+ ->method ('getAddressesCollection ' )
293
+ ->willReturn ($ addressCollection );
294
+ $ addressCollection ->expects ($ this ->once ())
295
+ ->method ("removeItemByKey " )
296
+ ->with ($ addressId );
297
+ $ addressCollection ->expects ($ this ->once ())
298
+ ->method ("addItem " )
299
+ ->with ($ this ->address );
300
+ $ this ->address ->expects ($ this ->once ())
301
+ ->method ('getDataModel ' )
302
+ ->willReturn ($ customerAddress );
303
+
304
+ $ this ->repository ->save ($ customerAddress );
305
+ }
306
+
307
+ public function testSaveWithConfigCustomerAccountShareScopeGlobal ()
308
+ {
309
+ $ customerId = 34 ;
310
+ $ addressId = 53 ;
311
+ $ customerAddress = $ this ->getMockForAbstractClass (
312
+ AddressInterface::class,
313
+ [],
314
+ '' ,
315
+ false
316
+ );
317
+ $ addressCollection =
318
+ $ this ->createMock (Collection::class);
319
+ $ customerAddress ->expects ($ this ->atLeastOnce ())
320
+ ->method ('getCustomerId ' )
321
+ ->willReturn ($ customerId );
322
+ $ customerAddress ->expects ($ this ->atLeastOnce ())
323
+ ->method ('getId ' )
324
+ ->willReturn ($ addressId );
325
+ $ this ->customerRegistry ->expects ($ this ->once ())
326
+ ->method ('retrieve ' )
327
+ ->with ($ customerId )
328
+ ->willReturn ($ this ->customer );
329
+ $ this ->address ->expects ($ this ->atLeastOnce ())
330
+ ->method ("getId " )
331
+ ->willReturn ($ addressId );
332
+ $ this ->addressRegistry ->expects ($ this ->once ())
333
+ ->method ('retrieve ' )
334
+ ->with ($ addressId )
335
+ ->willReturn (null );
336
+ $ this ->addressFactory ->expects ($ this ->once ())
337
+ ->method ('create ' )
338
+ ->willReturn ($ this ->address );
339
+ $ this ->address ->expects ($ this ->once ())
340
+ ->method ('updateData ' )
341
+ ->with ($ customerAddress );
342
+ $ this ->address ->expects ($ this ->once ())
343
+ ->method ('setCustomer ' )
344
+ ->with ($ this ->customer );
345
+ $ this ->customer ->expects ($ this ->exactly (2 ))
346
+ ->method ('getSharingConfig ' )
347
+ ->willReturn ($ this ->configShare );
348
+ $ this ->configShare ->expects ($ this ->once ())
349
+ ->method ('isWebsiteScope ' )
350
+ ->willReturn (false );
351
+ $ this ->address ->expects ($ this ->never ())
352
+ ->method ('setStoreId ' );
353
+ $ this ->address ->expects ($ this ->once ())
354
+ ->method ('validate ' )
355
+ ->willReturn (true );
356
+ $ this ->address ->expects ($ this ->once ())
357
+ ->method ('save ' );
358
+ $ this ->addressRegistry ->expects ($ this ->once ())
359
+ ->method ('push ' )
360
+ ->with ($ this ->address );
361
+ $ this ->customer ->expects ($ this ->exactly (2 ))
362
+ ->method ('getAddressesCollection ' )
363
+ ->willReturn ($ addressCollection );
364
+ $ addressCollection ->expects ($ this ->once ())
365
+ ->method ("removeItemByKey " )
366
+ ->with ($ addressId );
367
+ $ addressCollection ->expects ($ this ->once ())
368
+ ->method ("addItem " )
369
+ ->with ($ this ->address );
370
+ $ this ->address ->expects ($ this ->once ())
371
+ ->method ('getDataModel ' )
372
+ ->willReturn ($ customerAddress );
373
+
374
+ $ this ->repository ->save ($ customerAddress );
375
+ }
376
+
215
377
public function testSaveWithException ()
216
378
{
217
379
$ this ->expectException (InputException::class);
0 commit comments