3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Quote \Model ;
8
7
9
8
use Magento \Customer \Api \AddressRepositoryInterface ;
12
11
use Magento \Customer \Api \Data \GroupInterface ;
13
12
use Magento \Customer \Api \GroupRepositoryInterface ;
14
13
use Magento \Customer \Model \Vat ;
14
+ use Magento \Customer \Observer \AfterAddressSaveObserver ;
15
15
use Magento \Framework \Api \SearchCriteriaBuilder ;
16
16
use Magento \Framework \App \Config \MutableScopeConfigInterface ;
17
+ use Magento \Framework \DataObject ;
18
+ use Magento \Framework \Exception \NoSuchEntityException ;
17
19
use Magento \Framework \ObjectManagerInterface ;
18
20
use Magento \Quote \Api \CartRepositoryInterface ;
21
+ use Magento \Quote \Api \Data \AddressInterfaceFactory ;
22
+ use Magento \Quote \Api \Data \EstimateAddressInterface ;
23
+ use Magento \Quote \Api \GuestShippingMethodManagementInterface ;
19
24
use Magento \Quote \Api \ShippingMethodManagementInterface ;
25
+ use Magento \Quote \Observer \Frontend \Quote \Address \CollectTotalsObserver ;
26
+ use Magento \Quote \Observer \Frontend \Quote \Address \VatValidator ;
20
27
use Magento \Store \Model \ScopeInterface ;
21
28
use Magento \Tax \Api \Data \TaxClassInterface ;
22
29
use Magento \Tax \Api \TaxClassRepositoryInterface ;
23
30
use Magento \Tax \Model \ClassModel ;
24
31
use Magento \Tax \Model \Config as TaxConfig ;
25
32
use Magento \TestFramework \Helper \Bootstrap ;
26
33
use Magento \TestFramework \Quote \Model \GetQuoteByReservedOrderId ;
34
+ use PHPUnit \Framework \TestCase ;
27
35
28
36
/**
29
37
* Test for shipping methods management
30
38
*
31
39
* @magentoDbIsolation enabled
32
40
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33
41
*/
34
- class ShippingMethodManagementTest extends \ PHPUnit \ Framework \ TestCase
42
+ class ShippingMethodManagementTest extends TestCase
35
43
{
36
44
/** @var ObjectManagerInterface $objectManager */
37
45
private $ objectManager ;
@@ -56,14 +64,14 @@ protected function setUp()
56
64
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_100_percent_off.php
57
65
* @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
58
66
* @return void
59
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
67
+ * @throws NoSuchEntityException
60
68
*/
61
69
public function testRateAppliedToShipping (): void
62
70
{
63
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
71
+ $ objectManager = Bootstrap::getObjectManager ();
64
72
65
- /** @var \Magento\Quote\Api\ CartRepositoryInterface $quoteRepository */
66
- $ quoteRepository = $ objectManager ->create (\ Magento \ Quote \ Api \ CartRepositoryInterface::class);
73
+ /** @var CartRepositoryInterface $quoteRepository */
74
+ $ quoteRepository = $ objectManager ->create (CartRepositoryInterface::class);
67
75
$ customerQuote = $ quoteRepository ->getForCustomer (1 );
68
76
$ this ->assertEquals (0 , $ customerQuote ->getBaseGrandTotal ());
69
77
}
@@ -80,17 +88,17 @@ public function testRateAppliedToShipping(): void
80
88
*/
81
89
public function testTableRateFreeShipping ()
82
90
{
83
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
84
- /** @var \Magento\Quote\Model\ Quote $quote */
85
- $ quote = $ objectManager ->get (\ Magento \ Quote \ Model \ Quote::class);
91
+ $ objectManager = Bootstrap::getObjectManager ();
92
+ /** @var Quote $quote */
93
+ $ quote = $ objectManager ->get (Quote::class);
86
94
$ quote ->load ('test01 ' , 'reserved_order_id ' );
87
95
$ cartId = $ quote ->getId ();
88
96
if (!$ cartId ) {
89
97
$ this ->fail ('quote fixture failed ' );
90
98
}
91
- /** @var \Magento\Quote\Model\ QuoteIdMask $quoteIdMask */
92
- $ quoteIdMask = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
93
- ->create (\ Magento \ Quote \ Model \ QuoteIdMaskFactory::class)
99
+ /** @var QuoteIdMask $quoteIdMask */
100
+ $ quoteIdMask = Bootstrap::getObjectManager ()
101
+ ->create (QuoteIdMaskFactory::class)
94
102
->create ();
95
103
$ quoteIdMask ->load ($ cartId , 'quote_id ' );
96
104
//Use masked cart Id
@@ -103,10 +111,10 @@ public function testTableRateFreeShipping()
103
111
'region_id ' => null
104
112
]
105
113
];
106
- /** @var \Magento\Quote\Api\Data\ EstimateAddressInterface $address */
107
- $ address = $ objectManager ->create (\ Magento \ Quote \ Api \ Data \ EstimateAddressInterface::class, $ data );
108
- /** @var \Magento\Quote\Api\ GuestShippingMethodManagementInterface $shippingEstimation */
109
- $ shippingEstimation = $ objectManager ->get (\ Magento \ Quote \ Api \ GuestShippingMethodManagementInterface::class);
114
+ /** @var EstimateAddressInterface $address */
115
+ $ address = $ objectManager ->create (EstimateAddressInterface::class, $ data );
116
+ /** @var GuestShippingMethodManagementInterface $shippingEstimation */
117
+ $ shippingEstimation = $ objectManager ->get (GuestShippingMethodManagementInterface::class);
110
118
$ result = $ shippingEstimation ->estimateByAddress ($ cartId , $ address );
111
119
$ this ->assertNotEmpty ($ result );
112
120
$ expectedResult = [
@@ -134,25 +142,25 @@ public function testTableRateFreeShipping()
134
142
*/
135
143
public function testTableRateWithCartRuleForFreeShipping ()
136
144
{
137
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
145
+ $ objectManager = Bootstrap::getObjectManager ();
138
146
$ quote = $ this ->getQuote ('tableRate ' );
139
147
$ cartId = $ quote ->getId ();
140
148
if (!$ cartId ) {
141
149
$ this ->fail ('quote fixture failed ' );
142
150
}
143
- /** @var \Magento\Quote\Model\ QuoteIdMask $quoteIdMask */
144
- $ quoteIdMask = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
145
- ->create (\ Magento \ Quote \ Model \ QuoteIdMaskFactory::class)
151
+ /** @var QuoteIdMask $quoteIdMask */
152
+ $ quoteIdMask = Bootstrap::getObjectManager ()
153
+ ->create (QuoteIdMaskFactory::class)
146
154
->create ();
147
155
$ quoteIdMask ->load ($ cartId , 'quote_id ' );
148
156
//Use masked cart Id
149
157
$ cartId = $ quoteIdMask ->getMaskedId ();
150
- $ addressFactory = $ this ->objectManager ->get (\ Magento \ Quote \ Api \ Data \ AddressInterfaceFactory::class);
158
+ $ addressFactory = $ this ->objectManager ->get (AddressInterfaceFactory::class);
151
159
/** @var \Magento\Quote\Api\Data\AddressInterface $address */
152
160
$ address = $ addressFactory ->create ();
153
161
$ address ->setCountryId ('US ' );
154
- /** @var \Magento\Quote\Api\ GuestShippingMethodManagementInterface $shippingEstimation */
155
- $ shippingEstimation = $ objectManager ->get (\ Magento \ Quote \ Api \ GuestShippingMethodManagementInterface::class);
162
+ /** @var GuestShippingMethodManagementInterface $shippingEstimation */
163
+ $ shippingEstimation = $ objectManager ->get (GuestShippingMethodManagementInterface::class);
156
164
$ result = $ shippingEstimation ->estimateByExtendedAddress ($ cartId , $ address );
157
165
$ this ->assertCount (1 , $ result );
158
166
$ rate = reset ($ result );
@@ -234,17 +242,17 @@ public function testEstimateByAddress()
234
242
*/
235
243
private function executeTestFlow ($ flatRateAmount , $ tableRateAmount )
236
244
{
237
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
238
- /** @var \Magento\Quote\Model\ Quote $quote */
239
- $ quote = $ objectManager ->get (\ Magento \ Quote \ Model \ Quote::class);
245
+ $ objectManager = Bootstrap::getObjectManager ();
246
+ /** @var Quote $quote */
247
+ $ quote = $ objectManager ->get (Quote::class);
240
248
$ quote ->load ('test01 ' , 'reserved_order_id ' );
241
249
$ cartId = $ quote ->getId ();
242
250
if (!$ cartId ) {
243
251
$ this ->fail ('quote fixture failed ' );
244
252
}
245
- /** @var \Magento\Quote\Model\ QuoteIdMask $quoteIdMask */
246
- $ quoteIdMask = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
247
- ->create (\ Magento \ Quote \ Model \ QuoteIdMaskFactory::class)
253
+ /** @var QuoteIdMask $quoteIdMask */
254
+ $ quoteIdMask = Bootstrap::getObjectManager ()
255
+ ->create (QuoteIdMaskFactory::class)
248
256
->create ();
249
257
$ quoteIdMask ->load ($ cartId , 'quote_id ' );
250
258
//Use masked cart Id
@@ -257,17 +265,17 @@ private function executeTestFlow($flatRateAmount, $tableRateAmount)
257
265
'region_id ' => null
258
266
]
259
267
];
260
- /** @var \Magento\Quote\Api\Data\ EstimateAddressInterface $address */
261
- $ address = $ objectManager ->create (\ Magento \ Quote \ Api \ Data \ EstimateAddressInterface::class, $ data );
262
- /** @var \Magento\Quote\Api\ GuestShippingMethodManagementInterface $shippingEstimation */
263
- $ shippingEstimation = $ objectManager ->get (\ Magento \ Quote \ Api \ GuestShippingMethodManagementInterface::class);
268
+ /** @var EstimateAddressInterface $address */
269
+ $ address = $ objectManager ->create (EstimateAddressInterface::class, $ data );
270
+ /** @var GuestShippingMethodManagementInterface $shippingEstimation */
271
+ $ shippingEstimation = $ objectManager ->get (GuestShippingMethodManagementInterface::class);
264
272
$ result = $ shippingEstimation ->estimateByAddress ($ cartId , $ address );
265
273
$ this ->assertNotEmpty ($ result );
266
274
$ expectedResult = [
267
275
'tablerate ' => [
268
- 'method_code ' => 'bestway ' ,
269
- 'amount ' => $ tableRateAmount
270
- ],
276
+ 'method_code ' => 'bestway ' ,
277
+ 'amount ' => $ tableRateAmount
278
+ ],
271
279
'flatrate ' => [
272
280
'method_code ' => 'flatrate ' ,
273
281
'amount ' => $ flatRateAmount
@@ -295,17 +303,15 @@ private function executeTestFlow($flatRateAmount, $tableRateAmount)
295
303
*/
296
304
public function testEstimateByAddressWithInclExclTaxAndVATGroup ()
297
305
{
298
- $ this ->markTestSkipped ('MC-33463 ' );
299
-
300
- /** @var CustomerRepositoryInterface $customerRepository */
301
- $ customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
302
- $ customer = $ customerRepository ->get ('customer@example.com ' );
303
-
304
306
/** @var GroupInterface $customerGroup */
305
307
$ customerGroup = $ this ->findCustomerGroupByCode ('custom_group ' );
308
+ $ this ->mockCustomerVat ((int )$ customerGroup ->getId ());
309
+
306
310
$ customerGroup ->setTaxClassId ($ this ->getTaxClass ('CustomerTaxClass ' )->getClassId ());
307
311
$ this ->groupRepository ->save ($ customerGroup );
308
-
312
+ /** @var CustomerRepositoryInterface $customerRepository */
313
+ $ customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
314
+ $ customer = $ customerRepository ->get ('customer@example.com ' );
309
315
$ customer ->setGroupId ($ customerGroup ->getId ());
310
316
$ customer ->setTaxvat ('12 ' );
311
317
$ customerRepository ->save ($ customer );
@@ -322,11 +328,46 @@ public function testEstimateByAddressWithInclExclTaxAndVATGroup()
322
328
$ this ->assertEquals (5.0 , $ result [0 ]->getPriceExclTax ());
323
329
}
324
330
331
+ /**
332
+ * Create a test double fot customer vat class
333
+ *
334
+ * @param int $customerGroupId
335
+ */
336
+ private function mockCustomerVat (int $ customerGroupId ): 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 (
346
+ Vat::class,
347
+ [
348
+ 'checkVatNumber ' ,
349
+ 'isCountryInEU ' ,
350
+ 'getCustomerGroupIdBasedOnVatNumber ' ,
351
+ 'getMerchantCountryCode ' ,
352
+ 'getMerchantVatNumber '
353
+ ]
354
+ );
355
+ $ customerVat ->method ('checkVatNumber ' )->willReturn ($ gatewayResponse );
356
+ $ customerVat ->method ('isCountryInEU ' )->willReturn (true );
357
+ $ customerVat ->method ('getMerchantCountryCode ' )->willReturn ('GB ' );
358
+ $ customerVat ->method ('getMerchantVatNumber ' )->willReturn ('11111 ' );
359
+ $ customerVat ->method ('getCustomerGroupIdBasedOnVatNumber ' )->willReturn ($ customerGroupId );
360
+ $ this ->objectManager ->removeSharedInstance (Vat::class);
361
+ $ this ->objectManager ->addSharedInstance ($ customerVat , Vat::class);
362
+
363
+ // Remove instances where the customer vat object is cached
364
+ $ this ->objectManager ->removeSharedInstance (CollectTotalsObserver::class);
365
+ }
366
+
325
367
/**
326
368
* Find the group with a given code.
327
369
*
328
370
* @param string $code
329
- *
330
371
* @return GroupInterface
331
372
*/
332
373
protected function findCustomerGroupByCode (string $ code ): ?GroupInterface
0 commit comments