123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Test\Unit\Model\Order\Invoice;
- /**
- * Unit test for Invoice pay operation.
- */
- class PayOperationTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Sales\Model\Order\Invoice\PayOperation
- */
- private $subject;
- /**
- * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderMock;
- /**
- * @var \Magento\Sales\Api\Data\InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $invoiceMock;
- /**
- * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- private $contextMock;
- /**
- * @var \Magento\Sales\Api\Data\InvoiceItemInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $invoiceItemMock;
- /**
- * @var \Magento\Sales\Api\Data\OrderPaymentInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderPaymentMock;
- /**
- * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $eventManagerMock;
- /**
- * @var \Magento\Payment\Model\MethodInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $paymentMethodMock;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->orderMock = $this->getMockForAbstractClass(
- \Magento\Sales\Api\Data\OrderInterface::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'getPayment',
- 'setTotalInvoiced',
- 'getTotalInvoiced',
- 'setBaseTotalInvoiced',
- 'getBaseTotalInvoiced',
- 'setSubtotalInvoiced',
- 'getSubtotalInvoiced',
- 'setBaseSubtotalInvoiced',
- 'getBaseSubtotalInvoiced',
- 'setTaxInvoiced',
- 'getTaxInvoiced',
- 'setBaseTaxInvoiced',
- 'getBaseTaxInvoiced',
- 'setDiscountTaxCompensationInvoiced',
- 'getDiscountTaxCompensationInvoiced',
- 'setBaseDiscountTaxCompensationInvoiced',
- 'getBaseDiscountTaxCompensationInvoiced',
- 'setShippingTaxInvoiced',
- 'getShippingTaxInvoiced',
- 'setBaseShippingTaxInvoiced',
- 'getBaseShippingTaxInvoiced',
- 'setShippingInvoiced',
- 'getShippingInvoiced',
- 'setBaseShippingInvoiced',
- 'getBaseShippingInvoiced',
- 'setDiscountInvoiced',
- 'getDiscountInvoiced',
- 'setBaseDiscountInvoiced',
- 'getBaseDiscountInvoiced',
- 'setBaseTotalInvoicedCost',
- 'getBaseTotalInvoicedCost',
- ]
- );
- $this->orderMock->expects($this->any())
- ->method('getTotalInvoiced')
- ->willReturn(43);
- $this->orderMock->expects($this->any())
- ->method('getBaseTotalInvoiced')
- ->willReturn(43);
- $this->orderMock->expects($this->any())
- ->method('getSubtotalInvoiced')
- ->willReturn(22);
- $this->orderMock->expects($this->any())
- ->method('getBaseSubtotalInvoiced')
- ->willReturn(22);
- $this->orderMock->expects($this->any())
- ->method('getTaxInvoiced')
- ->willReturn(15);
- $this->orderMock->expects($this->any())
- ->method('getBaseTaxInvoiced')
- ->willReturn(15);
- $this->orderMock->expects($this->any())
- ->method('getDiscountTaxCompensationInvoiced')
- ->willReturn(11);
- $this->orderMock->expects($this->any())
- ->method('getBaseDiscountTaxCompensationInvoiced')
- ->willReturn(11);
- $this->orderMock->expects($this->any())
- ->method('getShippingTaxInvoiced')
- ->willReturn(12);
- $this->orderMock->expects($this->any())
- ->method('getBaseShippingTaxInvoiced')
- ->willReturn(12);
- $this->orderMock->expects($this->any())
- ->method('getShippingInvoiced')
- ->willReturn(28);
- $this->orderMock->expects($this->any())
- ->method('getBaseShippingInvoiced')
- ->willReturn(28);
- $this->orderMock->expects($this->any())
- ->method('getDiscountInvoiced')
- ->willReturn(19);
- $this->orderMock->expects($this->any())
- ->method('getBaseDiscountInvoiced')
- ->willReturn(19);
- $this->orderMock->expects($this->any())
- ->method('getBaseTotalInvoicedCost')
- ->willReturn(31);
- $this->invoiceMock = $this->getMockForAbstractClass(
- \Magento\Sales\Api\Data\InvoiceInterface::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'getItems',
- 'getState',
- 'capture',
- 'setCanVoidFlag',
- 'pay',
- 'getGrandTotal',
- 'getBaseGrandTotal',
- 'getSubtotal',
- 'getBaseSubtotal',
- 'getTaxAmount',
- 'getBaseTaxAmount',
- 'getDiscountTaxCompensationAmount',
- 'getBaseDiscountTaxCompensationAmount',
- 'getShippingTaxAmount',
- 'getBaseShippingTaxAmount',
- 'getShippingAmount',
- 'getBaseShippingAmount',
- 'getDiscountAmount',
- 'getBaseDiscountAmount',
- 'getBaseCost',
- ]
- );
- $this->invoiceMock->expects($this->any())
- ->method('getGrandTotal')
- ->willReturn(43);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseGrandTotal')
- ->willReturn(43);
- $this->invoiceMock->expects($this->any())
- ->method('getSubtotal')
- ->willReturn(22);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseSubtotal')
- ->willReturn(22);
- $this->invoiceMock->expects($this->any())
- ->method('getTaxAmount')
- ->willReturn(15);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseTaxAmount')
- ->willReturn(15);
- $this->invoiceMock->expects($this->any())
- ->method('getDiscountTaxCompensationAmount')
- ->willReturn(11);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseDiscountTaxCompensationAmount')
- ->willReturn(11);
- $this->invoiceMock->expects($this->any())
- ->method('getShippingTaxAmount')
- ->willReturn(12);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseShippingTaxAmount')
- ->willReturn(12);
- $this->invoiceMock->expects($this->any())
- ->method('getShippingAmount')
- ->willReturn(28);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseShippingAmount')
- ->willReturn(28);
- $this->invoiceMock->expects($this->any())
- ->method('getDiscountAmount')
- ->willReturn(19);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseDiscountAmount')
- ->willReturn(19);
- $this->invoiceMock->expects($this->any())
- ->method('getBaseCost')
- ->willReturn(31);
- $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
- $this->invoiceItemMock = $this->getMockForAbstractClass(
- \Magento\Sales\Api\Data\InvoiceItemInterface::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'isDeleted',
- 'register',
- ]
- );
- $this->invoiceItemMock->expects($this->any())
- ->method('isDeleted')
- ->willReturn(false);
- $this->invoiceItemMock->expects($this->any())
- ->method('getQty')
- ->willReturn(1);
- $this->orderPaymentMock = $this->getMockForAbstractClass(
- \Magento\Sales\Api\Data\OrderPaymentInterface::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'canCapture',
- 'getMethodInstance',
- 'getIsTransactionPending',
- ]
- );
- $this->orderMock->expects($this->any())
- ->method('getPayment')
- ->willReturn($this->orderPaymentMock);
- $this->eventManagerMock = $this->getMockForAbstractClass(
- \Magento\Framework\Event\ManagerInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $this->contextMock->expects($this->any())
- ->method('getEventDispatcher')
- ->willReturn($this->eventManagerMock);
- $this->paymentMethodMock = $this->getMockForAbstractClass(
- \Magento\Payment\Model\MethodInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $this->orderPaymentMock->expects($this->any())
- ->method('getMethodInstance')
- ->willReturn($this->paymentMethodMock);
- $this->subject = new \Magento\Sales\Model\Order\Invoice\PayOperation(
- $this->contextMock
- );
- }
- /**
- * @param bool|null $canCapture
- * @param bool|null $isOnline
- * @param bool|null $isGateway
- * @param bool|null $isTransactionPending
- *
- * @dataProvider payDataProvider
- *
- * @return void
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecute($canCapture, $isOnline, $isGateway, $isTransactionPending)
- {
- $this->invoiceMock->expects($this->any())
- ->method('getItems')
- ->willReturn([$this->invoiceItemMock]);
- if ($canCapture) {
- $this->invoiceMock->expects($this->any())
- ->method('getState')
- ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
- $this->orderPaymentMock->expects($this->any())
- ->method('canCapture')
- ->willReturn(true);
- if ($isOnline) {
- $this->invoiceMock->expects($this->once())
- ->method('capture');
- } else {
- $this->invoiceMock->expects($this->never())
- ->method('capture');
- $this->invoiceMock->expects($this->once())
- ->method('setCanVoidFlag')
- ->with(false);
- $this->invoiceMock->expects($this->once())
- ->method('pay');
- }
- } else {
- $this->paymentMethodMock->expects($this->any())
- ->method('isGateway')
- ->willReturn($isGateway);
- $this->orderPaymentMock->expects($this->any())
- ->method('getIsTransactionPending')
- ->willReturn($isTransactionPending);
- $this->invoiceMock->expects($this->never())
- ->method('capture');
- if ((!$isGateway || !$isOnline) && !$isTransactionPending) {
- $this->invoiceMock->expects($this->once())
- ->method('setCanVoidFlag')
- ->with(false);
- $this->invoiceMock->expects($this->once())
- ->method('pay');
- }
- }
- $this->orderMock->expects($this->once())
- ->method('setTotalInvoiced')
- ->with(86);
- $this->orderMock->expects($this->once())
- ->method('setBaseTotalInvoiced')
- ->with(86);
- $this->orderMock->expects($this->once())
- ->method('setSubtotalInvoiced')
- ->with(44);
- $this->orderMock->expects($this->once())
- ->method('setBaseSubtotalInvoiced')
- ->with(44);
- $this->orderMock->expects($this->once())
- ->method('setTaxInvoiced')
- ->with(30);
- $this->orderMock->expects($this->once())
- ->method('setBaseTaxInvoiced')
- ->with(30);
- $this->orderMock->expects($this->once())
- ->method('setDiscountTaxCompensationInvoiced')
- ->with(22);
- $this->orderMock->expects($this->once())
- ->method('setBaseDiscountTaxCompensationInvoiced')
- ->with(22);
- $this->orderMock->expects($this->once())
- ->method('setShippingTaxInvoiced')
- ->with(24);
- $this->orderMock->expects($this->once())
- ->method('setBaseShippingTaxInvoiced')
- ->with(24);
- $this->orderMock->expects($this->once())
- ->method('setShippingInvoiced')
- ->with(56);
- $this->orderMock->expects($this->once())
- ->method('setBaseShippingInvoiced')
- ->with(56);
- $this->orderMock->expects($this->once())
- ->method('setDiscountInvoiced')
- ->with(38);
- $this->orderMock->expects($this->once())
- ->method('setBaseDiscountInvoiced')
- ->with(38);
- $this->orderMock->expects($this->once())
- ->method('setBaseTotalInvoicedCost')
- ->with(62);
- $this->eventManagerMock->expects($this->once())
- ->method('dispatch')
- ->with(
- 'sales_order_invoice_register',
- [
- 'invoice' => $this->invoiceMock,
- 'order' => $this->orderMock,
- ]
- );
- $this->assertEquals(
- $this->orderMock,
- $this->subject->execute(
- $this->orderMock,
- $this->invoiceMock,
- $isOnline
- )
- );
- }
- /**
- * @return array
- */
- public function payDataProvider()
- {
- return [
- 'Invoice can capture, online' => [
- true, true, null, null,
- ],
- 'Invoice can capture, offline' => [
- true, false, null, null,
- ],
- 'Invoice can not capture, online, is not gateway, transaction is not pending' => [
- false, true, false, false,
- ],
- 'Invoice can not capture, offline, gateway, transaction is not pending' => [
- false, false, true, false,
- ],
- ];
- }
- }
|