Skip to content

Commit 4b007c2

Browse files
added unit test; refactoring
1 parent 41c3aa2 commit 4b007c2

File tree

1 file changed

+101
-50
lines changed
  • app/code/Magento/Sales/Test/Unit/Block/Adminhtml/Order/Totals

1 file changed

+101
-50
lines changed

app/code/Magento/Sales/Test/Unit/Block/Adminhtml/Order/Totals/TaxTest.php

+101-50
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
*/
66
declare(strict_types=1);
77

8-
/**
9-
* Test class for \Magento\Sales\Block\Adminhtml\Order\Totals\TaxTest
10-
*/
118
namespace Magento\Sales\Test\Unit\Block\Adminhtml\Order\Totals;
129

1310
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -16,46 +13,75 @@
1613
use Magento\Sales\Model\Order\Creditmemo;
1714
use Magento\Sales\Model\Order\Invoice;
1815
use Magento\Tax\Helper\Data;
16+
use Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection;
17+
use Magento\Tax\Model\Sales\Order\Tax as TaxModel;
18+
use Magento\Tax\Model\Sales\Order\TaxFactory;
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
2121

22+
/**
23+
* Test for \Magento\Sales\Block\Adminhtml\Order\Totals\Tax
24+
*/
2225
class TaxTest extends TestCase
2326
{
24-
/** @var MockObject|Tax */
27+
/**
28+
* @var array
29+
*/
30+
private $calculatedData = [
31+
'tax' => 'tax',
32+
'shipping_tax' => 'shipping_tax',
33+
];
34+
35+
/**
36+
* @var MockObject|Tax
37+
*/
2538
private $taxMock;
2639

40+
/**
41+
* @var Data|MockObject
42+
*/
43+
private $taxHelperMock;
44+
45+
/**
46+
* @var TaxFactory|MockObject
47+
*/
48+
private $taxOrderFactory;
49+
50+
/**
51+
* @inheridoc
52+
*/
2753
protected function setUp(): void
2854
{
29-
$getCalculatedTax = [
30-
'tax' => 'tax',
31-
'shipping_tax' => 'shipping_tax',
32-
];
33-
$taxHelperMock = $this->getMockBuilder(Data::class)
34-
->setMethods(['getCalculatedTaxes'])
55+
$this->taxHelperMock = $this->getMockBuilder(Data::class)
56+
->onlyMethods(['getCalculatedTaxes'])
3557
->disableOriginalConstructor()
3658
->getMock();
37-
$taxHelperMock->expects($this->any())
38-
->method('getCalculatedTaxes')
39-
->willReturn($getCalculatedTax);
4059

60+
$this->taxOrderFactory = $this->createMock(TaxFactory::class);
61+
62+
$arguments = $this->getModelArguments(
63+
['taxHelper' => $this->taxHelperMock, 'taxOrderFactory' => $this->taxOrderFactory]
64+
);
4165
$this->taxMock = $this->getMockBuilder(Tax::class)
42-
->setConstructorArgs($this->_getConstructArguments($taxHelperMock))
43-
->setMethods(['getOrder', 'getSource'])
66+
->setConstructorArgs($arguments)
67+
->onlyMethods(['getOrder', 'getSource'])
4468
->getMock();
4569
}
4670

4771
/**
4872
* Test method for getFullTaxInfo
4973
*
50-
* @param Order $source
51-
* @param array $getCalculatedTax
52-
* @param array $getShippingTax
74+
* @param Order|null $source
5375
* @param array $expectedResult
76+
* @return void
5477
*
5578
* @dataProvider getFullTaxInfoDataProvider
5679
*/
57-
public function testGetFullTaxInfo($source, $expectedResult)
80+
public function testGetFullTaxInfo(?Order $source, array $expectedResult): void
5881
{
82+
$this->taxHelperMock->expects($this->any())
83+
->method('getCalculatedTaxes')
84+
->willReturn($this->calculatedData);
5985
$this->taxMock->expects($this->once())
6086
->method('getOrder')
6187
->willReturn($source);
@@ -69,13 +95,15 @@ public function testGetFullTaxInfo($source, $expectedResult)
6995
*
7096
* @param Invoice|Creditmemo $source
7197
* @param array $expectedResult
98+
* @return void
7299
*
73100
* @dataProvider getCreditAndInvoiceFullTaxInfoDataProvider
74101
*/
75-
public function testGetFullTaxInfoWithCreditAndInvoice(
76-
$source,
77-
$expectedResult
78-
) {
102+
public function testGetFullTaxInfoWithCreditAndInvoice($source, array $expectedResult): void
103+
{
104+
$this->taxHelperMock->expects($this->any())
105+
->method('getCalculatedTaxes')
106+
->willReturn($this->calculatedData);
79107
$this->taxMock->expects($this->once())
80108
->method('getSource')
81109
->willReturn($source);
@@ -84,19 +112,54 @@ public function testGetFullTaxInfoWithCreditAndInvoice(
84112
$this->assertSame($expectedResult, $actualResult);
85113
}
86114

115+
/**
116+
* Test method for getFullTaxInfo when order doesn't have tax
117+
*
118+
* @return void
119+
*/
120+
public function testGetFullTaxInfoOrderWithoutTax(): void
121+
{
122+
$this->taxHelperMock->expects($this->once())
123+
->method('getCalculatedTaxes')
124+
->willReturn(null);
125+
126+
$orderMock = $this->createMock(Order::class);
127+
$taxCollection = $this->createMock(Collection::class);
128+
$taxCollection->expects($this->once())
129+
->method('loadByOrder')
130+
->with($orderMock)
131+
->willReturnSelf();
132+
133+
$taxOrder = $this->createMock(TaxModel::class);
134+
$taxOrder->expects($this->once())
135+
->method('getCollection')
136+
->willReturn($taxCollection);
137+
$this->taxOrderFactory->expects($this->once())
138+
->method('create')
139+
->willReturn($taxOrder);
140+
141+
$invoiceMock = $this->createMock(Invoice::class);
142+
$this->taxMock->expects($this->once())
143+
->method('getSource')
144+
->willReturn($invoiceMock);
145+
$this->taxMock->expects($this->once())
146+
->method('getOrder')
147+
->willReturn($orderMock);
148+
149+
$this->assertNull($this->taxMock->getFullTaxInfo());
150+
}
151+
87152
/**
88153
* Provide the tax helper mock as a constructor argument
89154
*
90-
* @param $taxHelperMock
155+
* @param array $arguments
91156
* @return array
92157
*/
93-
protected function _getConstructArguments($taxHelperMock)
158+
private function getModelArguments(array $arguments): array
94159
{
95160
$objectManagerHelper = new ObjectManager($this);
96-
return $objectManagerHelper->getConstructArguments(
97-
Tax::class,
98-
['taxHelper' => $taxHelperMock]
99-
);
161+
162+
return $objectManagerHelper->getConstructArguments(Tax::class, $arguments);
100163
}
101164

102165
/**
@@ -106,19 +169,15 @@ protected function _getConstructArguments($taxHelperMock)
106169
*
107170
* @return array
108171
*/
109-
public function getFullTaxInfoDataProvider()
172+
public function getFullTaxInfoDataProvider(): array
110173
{
111-
$salesModelOrderMock = $this->getMockBuilder(Order::class)
112-
->disableOriginalConstructor()
113-
->getMock();
174+
$salesModelOrderMock = $this->createMock(Order::class);
175+
114176
return [
115177
'source is not an instance of \Magento\Sales\Model\Order' => [null, []],
116178
'source is an instance of \Magento\Sales\Model\Order and has reasonable data' => [
117179
$salesModelOrderMock,
118-
[
119-
'tax' => 'tax',
120-
'shipping_tax' => 'shipping_tax',
121-
],
180+
$this->calculatedData,
122181
]
123182
];
124183
}
@@ -130,22 +189,14 @@ public function getFullTaxInfoDataProvider()
130189
*
131190
* @return array
132191
*/
133-
public function getCreditAndInvoiceFullTaxInfoDataProvider()
192+
public function getCreditAndInvoiceFullTaxInfoDataProvider(): array
134193
{
135-
$invoiceMock = $this->getMockBuilder(Invoice::class)
136-
->disableOriginalConstructor()
137-
->getMock();
138-
$creditMemoMock = $this->getMockBuilder(Creditmemo::class)
139-
->disableOriginalConstructor()
140-
->getMock();
194+
$invoiceMock = $this->createMock(Invoice::class);
195+
$creditMemoMock = $this->createMock(Creditmemo::class);
141196

142-
$expected = [
143-
'tax' => 'tax',
144-
'shipping_tax' => 'shipping_tax',
145-
];
146197
return [
147-
'invoice' => [$invoiceMock, $expected],
148-
'creditMemo' => [$creditMemoMock, $expected]
198+
'invoice' => [$invoiceMock, $this->calculatedData],
199+
'creditMemo' => [$creditMemoMock, $this->calculatedData]
149200
];
150201
}
151202
}

0 commit comments

Comments
 (0)