14
14
class CollectTotalsObserverTest extends \PHPUnit_Framework_TestCase
15
15
{
16
16
/**
17
- * @var \Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver;
17
+ * @var \Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver
18
18
*/
19
19
protected $ model ;
20
20
@@ -23,11 +23,21 @@ class CollectTotalsObserverTest extends \PHPUnit_Framework_TestCase
23
23
*/
24
24
protected $ customerAddressMock ;
25
25
26
+ /**
27
+ * @var \PHPUnit_Framework_MockObject_MockObject
28
+ */
29
+ protected $ customerSession ;
30
+
26
31
/**
27
32
* @var \PHPUnit_Framework_MockObject_MockObject
28
33
*/
29
34
protected $ customerVatMock ;
30
35
36
+ /**
37
+ * @var \PHPUnit_Framework_MockObject_MockObject
38
+ */
39
+ protected $ addressRepository ;
40
+
31
41
/**
32
42
* @var \PHPUnit_Framework_MockObject_MockObject
33
43
*/
@@ -172,14 +182,21 @@ protected function setUp()
172
182
->method ('getCustomer ' )
173
183
->will ($ this ->returnValue ($ this ->customerMock ));
174
184
185
+ $ this ->addressRepository = $ this ->getMock (\Magento \Customer \Api \AddressRepositoryInterface::class);
186
+ $ this ->customerSession = $ this ->getMockBuilder (\Magento \Customer \Model \Session::class)
187
+ ->disableOriginalConstructor ()
188
+ ->getMock ();
189
+
175
190
$ this ->customerMock ->expects ($ this ->any ())->method ('getStoreId ' )->will ($ this ->returnValue ($ this ->storeId ));
176
191
177
192
$ this ->model = new \Magento \Quote \Observer \Frontend \Quote \Address \CollectTotalsObserver (
178
193
$ this ->customerAddressMock ,
179
194
$ this ->customerVatMock ,
180
195
$ this ->vatValidatorMock ,
181
196
$ this ->customerDataFactoryMock ,
182
- $ this ->groupManagementMock
197
+ $ this ->groupManagementMock ,
198
+ $ this ->addressRepository ,
199
+ $ this ->customerSession
183
200
);
184
201
}
185
202
@@ -319,4 +336,75 @@ public function testDispatchWithCustomerCountryInEU()
319
336
->willReturn ($ this ->customerMock );
320
337
$ this ->model ->execute ($ this ->observerMock );
321
338
}
339
+
340
+ public function testDispatchWithEmptyShippingAddress ()
341
+ {
342
+ $ customerCountryCode = "DE " ;
343
+ $ customerVat = "123123123 " ;
344
+ $ defaultShipping = 1 ;
345
+
346
+ $ customerAddress = $ this ->getMock (\Magento \Customer \Api \Data \AddressInterface::class);
347
+ $ customerAddress ->expects ($ this ->once ())
348
+ ->method ("getCountryId " )
349
+ ->willReturn ($ customerCountryCode );
350
+
351
+ $ customerAddress ->expects ($ this ->once ())
352
+ ->method ("getVatId " )
353
+ ->willReturn ($ customerVat );
354
+ $ this ->addressRepository ->expects ($ this ->once ())
355
+ ->method ("getById " )
356
+ ->with ($ defaultShipping )
357
+ ->willReturn ($ customerAddress );
358
+
359
+ $ this ->customerMock ->expects ($ this ->atLeastOnce ())
360
+ ->method ("getDefaultShipping " )
361
+ ->willReturn ($ defaultShipping );
362
+
363
+ $ this ->vatValidatorMock ->expects ($ this ->once ())
364
+ ->method ('isEnabled ' )
365
+ ->with ($ this ->quoteAddressMock , $ this ->storeId )
366
+ ->will ($ this ->returnValue (true ));
367
+
368
+ $ this ->quoteAddressMock ->expects ($ this ->once ())
369
+ ->method ('getCountryId ' )
370
+ ->will ($ this ->returnValue (null ));
371
+ $ this ->quoteAddressMock ->expects ($ this ->once ())
372
+ ->method ('getVatId ' )
373
+ ->will ($ this ->returnValue (null ));
374
+
375
+ $ this ->customerVatMock ->expects ($ this ->once ())
376
+ ->method ('isCountryInEU ' )
377
+ ->with ($ customerCountryCode )
378
+ ->willReturn (true );
379
+
380
+ $ this ->quoteMock ->expects ($ this ->once ())
381
+ ->method ('getCustomerGroupId ' )
382
+ ->will ($ this ->returnValue ('customerGroupId ' ));
383
+ $ validationResult = ['some ' => 'result ' ];
384
+ $ this ->customerVatMock ->expects ($ this ->once ())
385
+ ->method ('getCustomerGroupIdBasedOnVatNumber ' )
386
+ ->with ($ customerCountryCode , $ validationResult , $ this ->storeId )
387
+ ->will ($ this ->returnValue ('customerGroupId ' ));
388
+ $ this ->customerSession ->expects ($ this ->once ())
389
+ ->method ("setCustomerGroupId " )
390
+ ->with ('customerGroupId ' );
391
+
392
+ $ this ->vatValidatorMock ->expects ($ this ->once ())
393
+ ->method ('validate ' )
394
+ ->with ($ this ->quoteAddressMock , $ this ->storeId )
395
+ ->will ($ this ->returnValue ($ validationResult ));
396
+
397
+ /** Assertions */
398
+ $ this ->quoteAddressMock ->expects ($ this ->once ())
399
+ ->method ('setPrevQuoteCustomerGroupId ' )
400
+ ->with ('customerGroupId ' );
401
+
402
+ $ this ->quoteMock ->expects ($ this ->once ())->method ('setCustomerGroupId ' )->with ('customerGroupId ' );
403
+ $ this ->quoteMock ->expects ($ this ->once ())->method ('setCustomer ' )->with ($ this ->customerMock );
404
+ $ this ->customerDataFactoryMock ->expects ($ this ->any ())
405
+ ->method ('create ' )
406
+ ->willReturn ($ this ->customerMock );
407
+ $ this ->model ->execute ($ this ->observerMock );
408
+
409
+ }
322
410
}
0 commit comments