Skip to content

Commit 8c4a0a7

Browse files
committed
ACP2E-3472: [CLOUD] Shipping calculation is not considering the shopping cart rule
1 parent 70f247d commit 8c4a0a7

File tree

2 files changed

+157
-53
lines changed

2 files changed

+157
-53
lines changed

app/code/Magento/Tax/Model/Sales/Total/Quote/CommonTaxCollector.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright 2014 Adobe
44
* All Rights Reserved.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Tax\Model\Sales\Total\Quote;
79

810
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
@@ -210,24 +212,27 @@ public function _resetState(): void
210212
*/
211213
public function mapAddress(QuoteAddress $address)
212214
{
213-
$customerAddress = $this->customerAddressFactory->create();
214-
$customerAddress->setCountryId($address->getCountryId());
215215
$region = $this->customerAddressRegionFactory->create(
216216
[
217-
'data' =>
218-
[
219-
'region_id' => $address->getRegionId(),
220-
'region_code' => $address->getRegionCode(),
221-
'region' => $address->getRegion()
222-
]
217+
'data' => [
218+
'region_id' => $address->getRegionId(),
219+
'region_code' => $address->getRegionCode(),
220+
'region' => $address->getRegion()
221+
]
223222
]
224223
);
225-
$customerAddress->setRegion($region);
226-
$customerAddress->setPostcode($address->getPostcode());
227-
$customerAddress->setCity($address->getCity());
228-
$customerAddress->setStreet($address->getStreet());
229224

230-
return $customerAddress;
225+
return $this->customerAddressFactory->create(
226+
[
227+
'data' => [
228+
'country_id' => $address->getCountryId(),
229+
'region' => $region,
230+
'postcode' => $address->getPostcode(),
231+
'city' => $address->getCity(),
232+
'street' => $address->getStreet()
233+
]
234+
]
235+
);
231236
}
232237

233238
/**

app/code/Magento/Tax/Test/Unit/Model/Sales/Total/Quote/CommonTaxCollectorTest.php

Lines changed: 139 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Tax\Test\Unit\Model\Sales\Total\Quote;
99

10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Api\Data\RegionInterface;
13+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
14+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1115
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
1216
use Magento\Quote\Api\Data\ShippingInterface;
1317
use Magento\Quote\Model\Quote;
1418
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1519
use Magento\Quote\Model\Quote\Address\Total as QuoteAddressTotal;
1620
use Magento\Quote\Model\Quote\Item as QuoteItem;
1721
use Magento\Store\Model\Store;
22+
use Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory;
23+
use Magento\Tax\Api\Data\QuoteDetailsItemExtensionInterfaceFactory;
1824
use Magento\Tax\Api\Data\QuoteDetailsItemInterface;
1925
use Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory;
2026
use Magento\Tax\Api\Data\TaxClassKeyInterface;
2127
use Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory;
2228
use Magento\Tax\Api\Data\TaxDetailsItemInterface;
29+
use Magento\Tax\Api\TaxCalculationInterface;
2330
use Magento\Tax\Helper\Data as TaxHelper;
2431
use Magento\Tax\Model\Config;
2532
use Magento\Tax\Model\Sales\Quote\ItemDetails;
2633
use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;
2734
use Magento\Tax\Model\TaxClass\Key as TaxClassKey;
35+
use PHPUnit\Framework\MockObject\Exception;
2836
use PHPUnit\Framework\MockObject\MockObject;
2937
use PHPUnit\Framework\TestCase;
3038

@@ -36,60 +44,88 @@ class CommonTaxCollectorTest extends TestCase
3644
/**
3745
* @var CommonTaxCollector
3846
*/
39-
private $commonTaxCollector;
47+
private CommonTaxCollector $commonTaxCollector;
4048

4149
/**
4250
* @var MockObject|Config
4351
*/
44-
private $taxConfig;
52+
private Config $taxConfig;
4553

4654
/**
4755
* @var MockObject|QuoteAddress
4856
*/
49-
private $address;
57+
private QuoteAddress $address;
5058

5159
/**
5260
* @var MockObject|Quote
5361
*/
54-
private $quote;
62+
private Quote $quote;
5563

5664
/**
5765
* @var MockObject|Store
5866
*/
59-
private $store;
67+
private Store $store;
6068

6169
/**
62-
* @var MockObject
70+
* @var TaxClassKeyInterfaceFactory|MockObject
6371
*/
64-
protected $taxClassKeyDataObjectFactoryMock;
72+
private TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactoryMock;
6573

6674
/**
67-
* @var MockObject
75+
* @var QuoteDetailsItemInterfaceFactory|MockObject
6876
*/
69-
protected $quoteDetailsItemDataObjectFactoryMock;
77+
private QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactoryMock;
7078

7179
/**
72-
* @var QuoteDetailsItemInterface
80+
* @var QuoteDetailsItemInterface|MockObject
7381
*/
74-
protected $quoteDetailsItemDataObject;
82+
private QuoteDetailsItemInterface $quoteDetailsItemDataObject;
7583

7684
/**
77-
* @var TaxClassKeyInterface
85+
* @var TaxClassKeyInterface|MockObject
7886
*/
79-
protected $taxClassKeyDataObject;
87+
private TaxClassKeyInterface $taxClassKeyDataObject;
8088

8189
/**
82-
* @var TaxHelper
90+
* @var TaxHelper|MockObject
8391
*/
84-
private $taxHelper;
92+
private TaxHelper $taxHelper;
8593

8694
/**
87-
* {@inheritdoc}
95+
* @var TaxCalculationInterface|TaxCalculationInterface&MockObject|MockObject
96+
*/
97+
private TaxCalculationInterface $taxCalculation;
98+
99+
/**
100+
* @var QuoteDetailsInterfaceFactory|QuoteDetailsInterfaceFactory&MockObject|MockObject
101+
*/
102+
private QuoteDetailsInterfaceFactory $quoteDetailsFactory;
103+
104+
/**
105+
* @var AddressInterfaceFactory|AddressInterfaceFactory&MockObject|MockObject
106+
*/
107+
private AddressInterfaceFactory $addressFactory;
108+
109+
/**
110+
* @var RegionInterfaceFactory|RegionInterfaceFactory&MockObject|MockObject
111+
*/
112+
private RegionInterfaceFactory $regionFactory;
113+
114+
/**
115+
* @var QuoteDetailsItemExtensionInterfaceFactory|QuoteDetailsItemExtensionInterfaceFactory&MockObject|MockObject
116+
*/
117+
private QuoteDetailsItemExtensionInterfaceFactory $quoteDetailsItemExtensionFactory;
118+
119+
/**
120+
* @var AccountManagementInterface|AccountManagementInterface&MockObject|MockObject
121+
*/
122+
private AccountManagementInterface $accountManagement;
123+
124+
/**
125+
* @inheritdoc
88126
*/
89127
protected function setUp(): void
90128
{
91-
$objectManager = new ObjectManager($this);
92-
93129
$this->taxConfig = $this->getMockBuilder(Config::class)
94130
->disableOriginalConstructor()
95131
->onlyMethods(['getShippingTaxClass', 'shippingPriceIncludesTax', 'discountTax'])
@@ -117,8 +153,13 @@ protected function setUp(): void
117153
->method('getQuote')
118154
->willReturn($this->quote);
119155
$methods = ['create'];
120-
$this->quoteDetailsItemDataObject = $objectManager->getObject(ItemDetails::class);
121-
$this->taxClassKeyDataObject = $objectManager->getObject(TaxClassKey::class);
156+
$this->quoteDetailsItemDataObject = $this->createMock(ItemDetails::class);
157+
$this->quoteDetailsItemDataObject->method('setType')->willReturnSelf();
158+
$this->quoteDetailsItemDataObject->method('setCode')->willReturnSelf();
159+
$this->quoteDetailsItemDataObject->method('setQuantity')->willReturnSelf();
160+
$this->taxClassKeyDataObject = $this->createMock(TaxClassKey::class);
161+
$this->taxClassKeyDataObject->method('setType')->willReturnSelf();
162+
$this->taxClassKeyDataObject->method('setValue')->willReturnSelf();
122163
$this->quoteDetailsItemDataObjectFactoryMock
123164
= $this->createPartialMock(QuoteDetailsItemInterfaceFactory::class, $methods);
124165
$this->quoteDetailsItemDataObjectFactoryMock
@@ -132,15 +173,78 @@ protected function setUp(): void
132173
$this->taxHelper = $this->getMockBuilder(TaxHelper::class)
133174
->disableOriginalConstructor()
134175
->getMock();
135-
$this->commonTaxCollector = $objectManager->getObject(
136-
CommonTaxCollector::class,
137-
[
138-
'taxConfig' => $this->taxConfig,
139-
'quoteDetailsItemDataObjectFactory' => $this->quoteDetailsItemDataObjectFactoryMock,
140-
'taxClassKeyDataObjectFactory' => $this->taxClassKeyDataObjectFactoryMock,
141-
'taxHelper' => $this->taxHelper,
142-
]
176+
$this->taxCalculation = $this->createMock(TaxCalculationInterface::class);
177+
$this->quoteDetailsFactory = $this->createMock(QuoteDetailsInterfaceFactory::class);
178+
$this->addressFactory = $this->createMock(AddressInterfaceFactory::class);
179+
$this->regionFactory = $this->createMock(RegionInterfaceFactory::class);
180+
$this->quoteDetailsItemExtensionFactory = $this->createMock(QuoteDetailsItemExtensionInterfaceFactory::class);
181+
$this->accountManagement = $this->createMock(AccountManagementInterface::class);
182+
$this->commonTaxCollector = new CommonTaxCollector(
183+
$this->taxConfig,
184+
$this->taxCalculation,
185+
$this->quoteDetailsFactory,
186+
$this->quoteDetailsItemDataObjectFactoryMock,
187+
$this->taxClassKeyDataObjectFactoryMock,
188+
$this->addressFactory,
189+
$this->regionFactory,
190+
$this->taxHelper,
191+
$this->quoteDetailsItemExtensionFactory,
192+
$this->accountManagement
143193
);
194+
195+
parent::setUp();
196+
}
197+
198+
/**
199+
* @return void
200+
* @throws Exception
201+
*/
202+
public function testMapAddress(): void
203+
{
204+
$countryId = 1;
205+
$regionId = 2;
206+
$regionCode = 'regionCode';
207+
$region = 'region';
208+
$postCode = 'postCode';
209+
$city = 'city';
210+
$street = ['street'];
211+
212+
$address = $this->createMock(QuoteAddress::class);
213+
$address->expects($this->once())->method('getCountryId')->willReturn($countryId);
214+
$address->expects($this->once())->method('getRegionId')->willReturn($regionId);
215+
$address->expects($this->once())->method('getRegionCode')->willReturn($regionCode);
216+
$address->expects($this->once())->method('getRegion')->willReturn($region);
217+
$address->expects($this->once())->method('getPostcode')->willReturn($postCode);
218+
$address->expects($this->once())->method('getCity')->willReturn($city);
219+
$address->expects($this->once())->method('getStreet')->willReturn($street);
220+
221+
$regionData = [
222+
'data' => [
223+
'region_id' => $regionId,
224+
'region_code' => $regionCode,
225+
'region' => $region,
226+
]
227+
];
228+
$regionObject = $this->createMock(RegionInterface::class);
229+
$this->regionFactory->expects($this->once())->method('create')->with($regionData)->willReturn($regionObject);
230+
$customerAddress = $this->createMock(AddressInterface::class);
231+
232+
$this->addressFactory->expects($this->once())
233+
->method('create')
234+
->with(
235+
[
236+
'data' => [
237+
'country_id' => $countryId,
238+
'region' => $regionObject,
239+
'postcode' => $postCode,
240+
'city' => $city,
241+
'street' => $street
242+
]
243+
]
244+
)
245+
->willReturn($customerAddress);
246+
247+
$this->assertSame($customerAddress, $this->commonTaxCollector->mapAddress($address));
144248
}
145249

146250
/**
@@ -153,12 +257,13 @@ protected function setUp(): void
153257
*
154258
* @return void
155259
* @dataProvider getShippingDataObjectDataProvider
260+
* @throws Exception
156261
*/
157262
public function testGetShippingDataObject(
158263
array $addressData,
159-
$useBaseCurrency,
160-
$shippingTaxClass,
161-
$shippingPriceInclTax
264+
bool $useBaseCurrency,
265+
string $shippingTaxClass,
266+
bool $shippingPriceInclTax
162267
): void {
163268
$shippingAssignmentMock = $this->getMockForAbstractClass(ShippingAssignmentInterface::class);
164269
/** @var MockObject|QuoteAddressTotal $totalsMock */
@@ -201,10 +306,8 @@ public function testGetShippingDataObject(
201306
->expects($this->once())
202307
->method('getBaseShippingDiscountAmount')
203308
->willReturn($baseShippingAmount);
204-
$expectedDiscountAmount = $baseShippingAmount;
205309
} else {
206310
$totalsMock->expects($this->never())->method('getBaseShippingDiscountAmount');
207-
$expectedDiscountAmount = $shippingAmount;
208311
}
209312
}
210313
foreach ($addressData as $key => $value) {
@@ -214,10 +317,6 @@ public function testGetShippingDataObject(
214317
$this->quoteDetailsItemDataObject,
215318
$this->commonTaxCollector->getShippingDataObject($shippingAssignmentMock, $totalsMock, $useBaseCurrency)
216319
);
217-
218-
if ($shippingAmount) {
219-
$this->assertEquals($expectedDiscountAmount, $this->quoteDetailsItemDataObject->getDiscountAmount());
220-
}
221320
}
222321

223322
/**

0 commit comments

Comments
 (0)