5
5
*/
6
6
declare (strict_types=1 );
7
7
8
- /**
9
- * Test class for \Magento\Sales\Block\Adminhtml\Order\Totals\TaxTest
10
- */
11
8
namespace Magento \Sales \Test \Unit \Block \Adminhtml \Order \Totals ;
12
9
13
10
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
13
use Magento \Sales \Model \Order \Creditmemo ;
17
14
use Magento \Sales \Model \Order \Invoice ;
18
15
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 ;
19
19
use PHPUnit \Framework \MockObject \MockObject ;
20
20
use PHPUnit \Framework \TestCase ;
21
21
22
+ /**
23
+ * Test for \Magento\Sales\Block\Adminhtml\Order\Totals\Tax
24
+ */
22
25
class TaxTest extends TestCase
23
26
{
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
+ */
25
38
private $ taxMock ;
26
39
40
+ /**
41
+ * @var Data|MockObject
42
+ */
43
+ private $ taxHelperMock ;
44
+
45
+ /**
46
+ * @var TaxFactory|MockObject
47
+ */
48
+ private $ taxOrderFactory ;
49
+
50
+ /**
51
+ * @inheridoc
52
+ */
27
53
protected function setUp (): void
28
54
{
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 ' ])
35
57
->disableOriginalConstructor ()
36
58
->getMock ();
37
- $ taxHelperMock ->expects ($ this ->any ())
38
- ->method ('getCalculatedTaxes ' )
39
- ->willReturn ($ getCalculatedTax );
40
59
60
+ $ this ->taxOrderFactory = $ this ->createMock (TaxFactory::class);
61
+
62
+ $ arguments = $ this ->getModelArguments (
63
+ ['taxHelper ' => $ this ->taxHelperMock , 'taxOrderFactory ' => $ this ->taxOrderFactory ]
64
+ );
41
65
$ this ->taxMock = $ this ->getMockBuilder (Tax::class)
42
- ->setConstructorArgs ($ this -> _getConstructArguments ( $ taxHelperMock ) )
43
- ->setMethods (['getOrder ' , 'getSource ' ])
66
+ ->setConstructorArgs ($ arguments )
67
+ ->onlyMethods (['getOrder ' , 'getSource ' ])
44
68
->getMock ();
45
69
}
46
70
47
71
/**
48
72
* Test method for getFullTaxInfo
49
73
*
50
- * @param Order $source
51
- * @param array $getCalculatedTax
52
- * @param array $getShippingTax
74
+ * @param Order|null $source
53
75
* @param array $expectedResult
76
+ * @return void
54
77
*
55
78
* @dataProvider getFullTaxInfoDataProvider
56
79
*/
57
- public function testGetFullTaxInfo ($ source , $ expectedResult )
80
+ public function testGetFullTaxInfo (? Order $ source , array $ expectedResult ): void
58
81
{
82
+ $ this ->taxHelperMock ->expects ($ this ->any ())
83
+ ->method ('getCalculatedTaxes ' )
84
+ ->willReturn ($ this ->calculatedData );
59
85
$ this ->taxMock ->expects ($ this ->once ())
60
86
->method ('getOrder ' )
61
87
->willReturn ($ source );
@@ -69,13 +95,15 @@ public function testGetFullTaxInfo($source, $expectedResult)
69
95
*
70
96
* @param Invoice|Creditmemo $source
71
97
* @param array $expectedResult
98
+ * @return void
72
99
*
73
100
* @dataProvider getCreditAndInvoiceFullTaxInfoDataProvider
74
101
*/
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 );
79
107
$ this ->taxMock ->expects ($ this ->once ())
80
108
->method ('getSource ' )
81
109
->willReturn ($ source );
@@ -84,19 +112,54 @@ public function testGetFullTaxInfoWithCreditAndInvoice(
84
112
$ this ->assertSame ($ expectedResult , $ actualResult );
85
113
}
86
114
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
+
87
152
/**
88
153
* Provide the tax helper mock as a constructor argument
89
154
*
90
- * @param $taxHelperMock
155
+ * @param array $arguments
91
156
* @return array
92
157
*/
93
- protected function _getConstructArguments ( $ taxHelperMock )
158
+ private function getModelArguments ( array $ arguments ): array
94
159
{
95
160
$ objectManagerHelper = new ObjectManager ($ this );
96
- return $ objectManagerHelper ->getConstructArguments (
97
- Tax::class,
98
- ['taxHelper ' => $ taxHelperMock ]
99
- );
161
+
162
+ return $ objectManagerHelper ->getConstructArguments (Tax::class, $ arguments );
100
163
}
101
164
102
165
/**
@@ -106,19 +169,15 @@ protected function _getConstructArguments($taxHelperMock)
106
169
*
107
170
* @return array
108
171
*/
109
- public function getFullTaxInfoDataProvider ()
172
+ public function getFullTaxInfoDataProvider (): array
110
173
{
111
- $ salesModelOrderMock = $ this ->getMockBuilder (Order::class)
112
- ->disableOriginalConstructor ()
113
- ->getMock ();
174
+ $ salesModelOrderMock = $ this ->createMock (Order::class);
175
+
114
176
return [
115
177
'source is not an instance of \Magento\Sales\Model\Order ' => [null , []],
116
178
'source is an instance of \Magento\Sales\Model\Order and has reasonable data ' => [
117
179
$ salesModelOrderMock ,
118
- [
119
- 'tax ' => 'tax ' ,
120
- 'shipping_tax ' => 'shipping_tax ' ,
121
- ],
180
+ $ this ->calculatedData ,
122
181
]
123
182
];
124
183
}
@@ -130,22 +189,14 @@ public function getFullTaxInfoDataProvider()
130
189
*
131
190
* @return array
132
191
*/
133
- public function getCreditAndInvoiceFullTaxInfoDataProvider ()
192
+ public function getCreditAndInvoiceFullTaxInfoDataProvider (): array
134
193
{
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);
141
196
142
- $ expected = [
143
- 'tax ' => 'tax ' ,
144
- 'shipping_tax ' => 'shipping_tax ' ,
145
- ];
146
197
return [
147
- 'invoice ' => [$ invoiceMock , $ expected ],
148
- 'creditMemo ' => [$ creditMemoMock , $ expected ]
198
+ 'invoice ' => [$ invoiceMock , $ this -> calculatedData ],
199
+ 'creditMemo ' => [$ creditMemoMock , $ this -> calculatedData ]
149
200
];
150
201
}
151
202
}
0 commit comments