ReviewPaymentTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order;
  7. /**
  8. * Class PaymentTest
  9. *
  10. * @package Magento\Sales\Controller\Adminhtml\Order
  11. */
  12. class ReviewPaymentTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Sales\Controller\Adminhtml\Order\ReviewPayment | \PHPUnit_Framework_MockObject_MockObject */
  15. protected $reviewPayment;
  16. /** @var \Magento\Backend\App\Action\Context| \PHPUnit_Framework_MockObject_MockObject */
  17. protected $contextMock;
  18. /** @var \Magento\Sales\Api\Data\OrderInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $orderMock;
  20. /** @var \Magento\Backend\Model\View\Result\RedirectFactory | \PHPUnit_Framework_MockObject_MockObject*/
  21. protected $resultRedirectFactoryMock;
  22. /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $resultRedirectMock;
  24. /**@var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $requestMock;
  26. /** @var \Magento\Sales\Model\Order\Payment|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $paymentMock;
  28. /** @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $messageManagerMock;
  30. /**
  31. * @var \Magento\Sales\Api\OrderManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $orderManagementMock;
  34. /**
  35. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $orderRepositoryMock;
  38. /**
  39. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $loggerMock;
  42. /**
  43. * Test setup
  44. */
  45. protected function setUp()
  46. {
  47. $this->contextMock = $this->createPartialMock(\Magento\Backend\App\Action\Context::class, [
  48. 'getRequest',
  49. 'getResponse',
  50. 'getMessageManager',
  51. 'getRedirect',
  52. 'getObjectManager',
  53. 'getSession',
  54. 'getActionFlag',
  55. 'getHelper',
  56. 'getResultRedirectFactory'
  57. ]);
  58. $this->orderManagementMock = $this->getMockBuilder(\Magento\Sales\Api\OrderManagementInterface::class)
  59. ->getMockForAbstractClass();
  60. $this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
  61. ->getMockForAbstractClass();
  62. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  63. ->getMockForAbstractClass();
  64. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
  65. ->setMethods(['getPayment'])
  66. ->getMockForAbstractClass();
  67. $this->messageManagerMock = $this->createPartialMock(
  68. \Magento\Framework\Message\Manager::class,
  69. ['addSuccessMessage', 'addErrorMessage']
  70. );
  71. $this->resultRedirectFactoryMock = $this->createPartialMock(
  72. \Magento\Backend\Model\View\Result\RedirectFactory::class,
  73. ['create']
  74. );
  75. $this->paymentMock = $this->createPartialMock(
  76. \Magento\Sales\Model\Order\Payment::class,
  77. ['update', 'getIsTransactionApproved']
  78. );
  79. $this->resultRedirectMock = $this->createPartialMock(
  80. \Magento\Backend\Model\View\Result\Redirect::class,
  81. ['setPath']
  82. );
  83. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  84. ->setMethods(['getParam'])
  85. ->disableOriginalConstructor()->getMock();
  86. $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
  87. $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
  88. $this->contextMock->expects($this->once())
  89. ->method('getResultRedirectFactory')
  90. ->willReturn($this->resultRedirectFactoryMock);
  91. $this->reviewPayment = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
  92. \Magento\Sales\Controller\Adminhtml\Order\ReviewPayment::class,
  93. [
  94. 'context' => $this->contextMock,
  95. 'orderManager' => $this->orderManagementMock,
  96. 'orderRepository' => $this->orderRepositoryMock
  97. ]
  98. );
  99. }
  100. /**
  101. * testExecuteUpdateAction
  102. */
  103. public function testExecuteUpdateAction()
  104. {
  105. $orderId = 30;
  106. $action = 'update';
  107. $this->requestMock->expects($this->at(0))->method('getParam')->with('order_id')->willReturn($orderId);
  108. $this->requestMock->expects($this->at(1))->method('getParam')->with('action')->willReturn($action);
  109. $this->resultRedirectFactoryMock->expects($this->once())->method('create')
  110. ->willReturn($this->resultRedirectMock);
  111. $this->orderRepositoryMock->expects($this->once())
  112. ->method('get')
  113. ->with($orderId)
  114. ->willReturn($this->orderMock);
  115. $this->orderMock->expects($this->any())->method('getEntityId')->willReturn($orderId);
  116. $this->orderMock->expects($this->any())->method('getPayment')->willReturn($this->paymentMock);
  117. $this->orderRepositoryMock->expects($this->once())
  118. ->method('save')
  119. ->with($this->orderMock)
  120. ->willReturnSelf();
  121. $this->paymentMock->expects($this->once())->method('update');
  122. $this->paymentMock->expects($this->any())->method('getIsTransactionApproved')->willReturn(true);
  123. $this->messageManagerMock->expects($this->once())->method('addSuccessMessage');
  124. $this->resultRedirectMock->expects($this->once())
  125. ->method('setPath')
  126. ->with('sales/order/view')
  127. ->willReturnSelf();
  128. $result = $this->reviewPayment->execute();
  129. $this->assertEquals($this->resultRedirectMock, $result);
  130. }
  131. }