Skip to content

Commit 1900ffc

Browse files
#25147: Totals Information Management - setting quote shipping method when address method is null.
1 parent a99d980 commit 1900ffc

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Test\Unit\Model;
9+
10+
use Magento\Checkout\Api\Data\TotalsInformationInterface;
11+
use Magento\Checkout\Model\TotalsInformationManagement;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Api\CartTotalRepositoryInterface;
15+
use Magento\Quote\Model\Quote\Address;
16+
17+
class TotalsInformationManagementTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var ObjectManager
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var CartRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
26+
*/
27+
private $cartRepositoryMock;
28+
29+
/**
30+
* @var CartTotalRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
31+
*/
32+
private $cartTotalRepositoryMock;
33+
34+
/**
35+
* @var TotalsInformationManagement
36+
*/
37+
private $totalsInformationManagement;
38+
39+
protected function setUp()
40+
{
41+
$this->objectManager = new ObjectManager($this);
42+
$this->cartRepositoryMock = $this->createMock(
43+
CartRepositoryInterface::class
44+
);
45+
$this->cartTotalRepositoryMock = $this->createMock(
46+
CartTotalRepositoryInterface::class
47+
);
48+
49+
$this->totalsInformationManagement = $this->objectManager->getObject(
50+
TotalsInformationManagement::class,
51+
[
52+
'cartRepository' => $this->cartRepositoryMock,
53+
'cartTotalRepository' => $this->cartTotalRepositoryMock,
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Test for \Magento\Checkout\Model\TotalsInformationManagement::calculate.
60+
*
61+
* @param string|null $carrierCode
62+
* @param string|null $carrierMethod
63+
* @param int $methodSetCount
64+
* @dataProvider dataProviderCalculate
65+
*/
66+
public function testCalculate(?string $carrierCode, ?string $carrierMethod, int $methodSetCount)
67+
{
68+
$cartId = 1;
69+
$cartMock = $this->createMock(
70+
\Magento\Quote\Model\Quote::class
71+
);
72+
$cartMock->expects($this->once())->method('getItemsCount')->willReturn(1);
73+
$cartMock->expects($this->once())->method('getIsVirtual')->willReturn(false);
74+
$this->cartRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($cartMock);
75+
$this->cartTotalRepositoryMock->expects($this->once())->method('get')->with($cartId);
76+
77+
$addressInformationMock = $this->createMock(
78+
TotalsInformationInterface::class
79+
);
80+
$addressMock = $this->createPartialMock(
81+
Address::class,
82+
[
83+
'setShippingMethod',
84+
'setCollectShippingRates'
85+
]
86+
);
87+
88+
$addressInformationMock->expects($this->once())->method('getAddress')->willReturn($addressMock);
89+
$addressInformationMock->expects($this->any())->method('getShippingCarrierCode')->willReturn($carrierCode);
90+
$addressInformationMock->expects($this->any())->method('getShippingMethodCode')->willReturn($carrierMethod);
91+
$cartMock->expects($this->once())->method('setShippingAddress')->with($addressMock);
92+
$cartMock->expects($this->exactly($methodSetCount))->method('getShippingAddress')->willReturn($addressMock);
93+
$addressMock->expects($this->exactly($methodSetCount))
94+
->method('setCollectShippingRates')->with(true)->willReturn($addressMock);
95+
$addressMock->expects($this->exactly($methodSetCount))
96+
->method('setShippingMethod')->with($carrierCode . '_' . $carrierMethod);
97+
$cartMock->expects($this->once())->method('collectTotals');
98+
99+
$this->totalsInformationManagement->calculate($cartId, $addressInformationMock);
100+
}
101+
102+
/**
103+
* Data provider for testCalculate.
104+
*
105+
* @return array
106+
*/
107+
public function dataProviderCalculate(): array
108+
{
109+
return [
110+
[
111+
null,
112+
null,
113+
0
114+
],
115+
[
116+
null,
117+
'carrier_method',
118+
0
119+
],
120+
[
121+
'carrier_code',
122+
null,
123+
0
124+
],
125+
[
126+
'carrier_code',
127+
'carrier_method',
128+
1
129+
]
130+
];
131+
}
132+
}

0 commit comments

Comments
 (0)