88namespace Magento \Sales \Test \Unit \Model \Order \Creditmemo \Validation ;
99
1010use Magento \Framework \Pricing \PriceCurrencyInterface ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12+ use Magento \Sales \Model \Order \Creditmemo ;
1113use Magento \Sales \Api \Data \CreditmemoInterface ;
1214use Magento \Sales \Api \Data \CreditmemoItemInterface ;
1315use Magento \Sales \Api \Data \OrderInterface ;
1416use Magento \Sales \Api \InvoiceRepositoryInterface ;
1517use Magento \Sales \Api \OrderRepositoryInterface ;
1618use Magento \Sales \Model \Order \Creditmemo \Validation \QuantityValidator ;
1719use Magento \Sales \Model \Order \Item ;
20+ use Magento \Store \Api \Data \StoreConfigInterface ;
1821use PHPUnit \Framework \MockObject \MockObject ;
1922use PHPUnit \Framework \TestCase ;
2023
@@ -154,6 +157,30 @@ public function testValidateWithWrongItemId()
154157 );
155158 }
156159
160+ private function getCreditMemoMockParams ()
161+ {
162+ return [
163+ $ this ->createMock (\Magento \Framework \Model \Context::class),
164+ $ this ->createMock (\Magento \Framework \Registry::class),
165+ $ this ->createMock (\Magento \Framework \Api \ExtensionAttributesFactory::class),
166+ $ this ->createMock (\Magento \Framework \Api \AttributeValueFactory::class),
167+ $ this ->createMock (\Magento \Sales \Model \Order \Creditmemo \Config::class),
168+ $ this ->createMock (\Magento \Sales \Model \OrderFactory::class),
169+ $ this ->createMock (\Magento \Sales \Model \ResourceModel \Order \Creditmemo \Item \CollectionFactory::class),
170+ $ this ->createMock (\Magento \Framework \Math \CalculatorFactory::class),
171+ $ this ->createMock (\Magento \Store \Model \StoreManagerInterface::class),
172+ $ this ->createMock ( \Magento \Sales \Model \Order \Creditmemo \CommentFactory::class),
173+ $ this ->createMock (\Magento \Sales \Model \ResourceModel \Order \Creditmemo \Comment \CollectionFactory::class),
174+ $ this ->createMock (\Magento \Framework \Pricing \PriceCurrencyInterface::class),
175+ $ this ->createMock (\Magento \Framework \Model \ResourceModel \AbstractResource::class),
176+ $ this ->createMock (\Magento \Framework \Data \Collection \AbstractDb::class),
177+ [],
178+ $ this ->createMock (\Magento \Sales \Model \Order \InvoiceFactory::class),
179+ $ this ->createMock (ScopeConfigInterface::class),
180+ $ this ->createMock (\Magento \Sales \Api \OrderRepositoryInterface::class)
181+ ];
182+ }
183+
157184 /**
158185 * @param int $orderId
159186 * @param int $orderItemId
@@ -163,6 +190,7 @@ public function testValidateWithWrongItemId()
163190 * @param int $total
164191 * @param array $expected
165192 * @param bool $isQtyDecimalAllowed
193+ * @param bool $isAllowZeroGrandTotal
166194 * @dataProvider dataProviderForValidateQty
167195 */
168196 public function testValidate (
@@ -174,16 +202,23 @@ public function testValidate(
174202 $ total ,
175203 array $ expected ,
176204 bool $ isQtyDecimalAllowed ,
177- bool $ isValidGrandTotal
205+ bool $ isAllowZeroGrandTotal
178206 ) {
179- $ creditmemoMock = $ this ->getMockBuilder (CreditmemoInterface::class)
180- ->disableOriginalConstructor ()
181- ->addMethods (['isValidGrandTotal ' ])
207+ $ scopeConfig = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
208+ $ scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->willReturn ($ isAllowZeroGrandTotal );
209+ $ creditMemoConstructorParams = $ this ->getCreditMemoMockParams ();
210+ $ creditMemoConstructorParams [16 ] = $ scopeConfig ;
211+
212+ $ creditmemoMock = $ this ->getMockBuilder (Creditmemo::class)
213+ ->setConstructorArgs ($ creditMemoConstructorParams )
214+ ->onlyMethods (['getOrderId ' , 'getItems ' , 'getGrandTotal ' , '_construct ' ])
182215 ->getMockForAbstractClass ();
216+
183217 $ creditmemoMock ->expects ($ this ->exactly (2 ))->method ('getOrderId ' )
184218 ->willReturn ($ orderId );
185- $ creditmemoMock ->expects ($ this ->once ())->method ('isValidGrandTotal ' )
186- ->willReturn ($ isValidGrandTotal );
219+ $ creditmemoMock ->expects ($ this ->once ())->method ('getGrandTotal ' )
220+ ->willReturn ($ total );
221+
187222 $ creditmemoItemMock = $ this ->getMockBuilder (
188223 CreditmemoItemInterface::class
189224 )->disableOriginalConstructor ()
@@ -244,7 +279,7 @@ public function dataProviderForValidateQty()
244279 'total ' => 15 ,
245280 'expected ' => [],
246281 'isQtyDecimalAllowed ' => false ,
247- 'isValidGrandTotal ' => true
282+ 'isAllowZeroGrandTotal ' => true
248283 ],
249284 [
250285 'orderId ' => 1 ,
@@ -255,7 +290,7 @@ public function dataProviderForValidateQty()
255290 'total ' => 15 ,
256291 'expected ' => [],
257292 'isQtyDecimalAllowed ' => false ,
258- 'isValidGrandTotal ' => true
293+ 'isAllowZeroGrandTotal ' => true
259294 ],
260295 [
261296 'orderId ' => 1 ,
@@ -271,7 +306,7 @@ public function dataProviderForValidateQty()
271306 )
272307 ],
273308 'isQtyDecimalAllowed ' => false ,
274- 'isValidGrandTotal ' => true
309+ 'isAllowZeroGrandTotal ' => true
275310 ],
276311 [
277312 'orderId ' => 1 ,
@@ -300,7 +335,7 @@ public function dataProviderForValidateQty()
300335 'total ' => 0 ,
301336 'expected ' => [],
302337 'isQtyDecimalAllowed ' => false ,
303- 'isValidGrandTotal ' => true
338+ 'isAllowZeroGrandTotal ' => true
304339 ]
305340 ];
306341 }
0 commit comments