11
11
use Magento \Customer \Api \Data \GroupInterface ;
12
12
use Magento \Customer \Api \GroupRepositoryInterface ;
13
13
use Magento \Customer \Model \Vat ;
14
+ use Magento \Customer \Observer \AfterAddressSaveObserver ;
14
15
use Magento \Framework \Api \SearchCriteriaBuilder ;
15
16
use Magento \Framework \App \Config \MutableScopeConfigInterface ;
17
+ use Magento \Framework \DataObject ;
16
18
use Magento \Framework \Exception \NoSuchEntityException ;
17
19
use Magento \Framework \ObjectManagerInterface ;
18
20
use Magento \Quote \Api \CartRepositoryInterface ;
19
21
use Magento \Quote \Api \Data \AddressInterfaceFactory ;
20
22
use Magento \Quote \Api \Data \EstimateAddressInterface ;
21
23
use Magento \Quote \Api \GuestShippingMethodManagementInterface ;
22
24
use Magento \Quote \Api \ShippingMethodManagementInterface ;
25
+ use Magento \Quote \Observer \Frontend \Quote \Address \CollectTotalsObserver ;
26
+ use Magento \Quote \Observer \Frontend \Quote \Address \VatValidator ;
23
27
use Magento \Store \Model \ScopeInterface ;
24
28
use Magento \Tax \Api \Data \TaxClassInterface ;
25
29
use Magento \Tax \Api \TaxClassRepositoryInterface ;
@@ -299,6 +303,8 @@ private function executeTestFlow($flatRateAmount, $tableRateAmount)
299
303
*/
300
304
public function testEstimateByAddressWithInclExclTaxAndVATGroup ()
301
305
{
306
+ $ this ->mockCustomerVat ();
307
+
302
308
/** @var CustomerRepositoryInterface $customerRepository */
303
309
$ customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
304
310
$ customer =
$ customerRepository->
get (
'[email protected] ' );
@@ -324,11 +330,45 @@ public function testEstimateByAddressWithInclExclTaxAndVATGroup()
324
330
$ this ->assertEquals (5.0 , $ result [0 ]->getPriceExclTax ());
325
331
}
326
332
333
+ /**
334
+ * Create a test double fot customer vat class
335
+ */
336
+ private function mockCustomerVat (): void
337
+ {
338
+ $ gatewayResponse = new DataObject ([
339
+ 'is_valid ' => false ,
340
+ 'request_date ' => '' ,
341
+ 'request_identifier ' => '123123123 ' ,
342
+ 'request_success ' => false ,
343
+ 'request_message ' => __ ('Error during VAT Number verification. ' ),
344
+ ]);
345
+ $ customerVat = $ this ->createPartialMock (Vat::class,
346
+ [
347
+ 'checkVatNumber ' ,
348
+ 'isCountryInEU ' ,
349
+ 'getCustomerGroupIdBasedOnVatNumber ' ,
350
+ 'getMerchantCountryCode ' ,
351
+ 'getMerchantVatNumber '
352
+ ]
353
+ );
354
+ $ customerVat ->method ('checkVatNumber ' )->willReturn ($ gatewayResponse );
355
+ $ customerVat ->method ('isCountryInEU ' )->willReturn (true );
356
+ $ customerVat ->method ('getMerchantCountryCode ' )->willReturn ('GB ' );
357
+ $ customerVat ->method ('getMerchantVatNumber ' )->willReturn ('11111 ' );
358
+ $ customerVat ->method ('getCustomerGroupIdBasedOnVatNumber ' )->willReturn ('4 ' );
359
+ $ this ->objectManager ->removeSharedInstance (Vat::class);
360
+ $ this ->objectManager ->addSharedInstance ($ customerVat , Vat::class);
361
+
362
+ // Remove instances where the customer vat object is cached
363
+ $ this ->objectManager ->removeSharedInstance (CollectTotalsObserver::class);
364
+ $ this ->objectManager ->removeSharedInstance (AfterAddressSaveObserver::class);
365
+ $ this ->objectManager ->removeSharedInstance (VatValidator::class);
366
+ }
367
+
327
368
/**
328
369
* Find the group with a given code.
329
370
*
330
371
* @param string $code
331
- *
332
372
* @return GroupInterface
333
373
*/
334
374
protected function findCustomerGroupByCode (string $ code ): ?GroupInterface
@@ -397,11 +437,6 @@ private function setConfig(int $customerGroupId, int $productTaxClassId): void
397
437
'value ' => $ customerGroupId ,
398
438
'scope ' => ScopeInterface::SCOPE_STORE ,
399
439
],
400
- [
401
- 'path ' => Vat::XML_PATH_CUSTOMER_VIV_ERROR_GROUP ,
402
- 'value ' => $ customerGroupId ,
403
- 'scope ' => ScopeInterface::SCOPE_STORE ,
404
- ],
405
440
[
406
441
'path ' => TaxConfig::CONFIG_XML_PATH_SHIPPING_TAX_CLASS ,
407
442
'value ' => $ productTaxClassId ,
0 commit comments