ExpressTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Test\Unit\Model\Adminhtml;
  8. use Magento\Payment\Model\MethodInterface;
  9. use Magento\Paypal\Model\Adminhtml\Express;
  10. use Magento\Paypal\Model\Api\Nvp;
  11. use Magento\Paypal\Model\Pro;
  12. use Magento\Sales\Model\Order;
  13. use Magento\Sales\Model\Order\Payment;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  16. use Magento\Sales\Model\Order\Payment\Transaction\Repository as TransactionRepository;
  17. use Magento\Sales\Api\Data\TransactionInterface;
  18. use Magento\Sales\Model\Order\Payment\Transaction;
  19. use PHPUnit\Framework\TestCase;
  20. /**
  21. * Test ability to make an authorization calls to Paypal API from admin.
  22. */
  23. class ExpressTest extends TestCase
  24. {
  25. /**
  26. * @var Express
  27. */
  28. private $express;
  29. /**
  30. * @var Payment|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $payment;
  33. /**
  34. * @var MethodInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $paymentInstance;
  37. /**
  38. * @var Pro|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $pro;
  41. /**
  42. * @var Nvp|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $nvp;
  45. /**
  46. * @var Order|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $order;
  49. /**
  50. * @var TransactionRepository|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $transactionRepository;
  53. /**
  54. * @var TransactionInterface|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $transaction;
  57. protected function setUp()
  58. {
  59. $objectManager = new ObjectManager($this);
  60. $this->nvp = $this->createPartialMock(
  61. Nvp::class,
  62. ['getData','setProcessableErrors', 'callDoAuthorization']
  63. );
  64. $this->nvp->method('getData')->willReturn([]);
  65. $this->nvp->method('setProcessableErrors')->willReturnSelf();
  66. $this->pro = $this->createPartialMock(
  67. Pro::class,
  68. ['setMethod', 'getApi', 'importPaymentInfo']
  69. );
  70. $this->pro->method('getApi')->willReturn($this->nvp);
  71. $this->transaction = $this->getMockForAbstractClass(TransactionInterface::class);
  72. $this->transactionRepository = $this->createPartialMock(
  73. TransactionRepository::class,
  74. ['getByTransactionType']
  75. );
  76. $this->transactionRepository->method('getByTransactionType')->willReturn($this->transaction);
  77. $this->express = $objectManager->getObject(
  78. Express::class,
  79. [
  80. 'data' => [$this->pro],
  81. 'transactionRepository' => $this->transactionRepository,
  82. ]
  83. );
  84. $this->paymentInstance = $this->getMockForAbstractClass(MethodInterface::class);
  85. $this->payment = $this->createPartialMock(
  86. Payment::class,
  87. [
  88. 'getAmountAuthorized',
  89. 'getMethod',
  90. 'getMethodInstance',
  91. 'getId',
  92. 'getOrder',
  93. 'addTransaction',
  94. 'addTransactionCommentsToOrder',
  95. 'setAmountAuthorized',
  96. ]
  97. );
  98. $this->payment->method('getMethodInstance')
  99. ->willReturn($this->paymentInstance);
  100. $this->payment->method('addTransaction')
  101. ->willReturn($this->transaction);
  102. }
  103. /**
  104. * Tests payment authorization flow for order.
  105. *
  106. * @throws LocalizedException
  107. */
  108. public function testAuthorizeOrder()
  109. {
  110. $this->order = $this->createPartialMock(
  111. Order::class,
  112. ['getId', 'getPayment', 'getTotalDue', 'getBaseTotalDue']
  113. );
  114. $this->order->method('getPayment')
  115. ->willReturn($this->payment);
  116. $this->order->method('getId')
  117. ->willReturn(1);
  118. $totalDue = 15;
  119. $baseTotalDue = 10;
  120. $this->order->method('getTotalDue')
  121. ->willReturn($totalDue);
  122. $this->order->method('getBaseTotalDue')
  123. ->willReturn($baseTotalDue);
  124. $this->payment->method('getMethod')
  125. ->willReturn('paypal_express');
  126. $this->payment->method('getId')
  127. ->willReturn(1);
  128. $this->payment->method('getOrder')
  129. ->willReturn($this->order);
  130. $this->payment->method('getAmountAuthorized')
  131. ->willReturn(0);
  132. $this->paymentInstance->method('getConfigPaymentAction')
  133. ->willReturn('order');
  134. $this->nvp->expects(static::once())
  135. ->method('callDoAuthorization')
  136. ->willReturnSelf();
  137. $this->payment->expects(static::once())
  138. ->method('addTransaction')
  139. ->with(Transaction::TYPE_AUTH)
  140. ->willReturn($this->transaction);
  141. $this->payment->method('addTransactionCommentsToOrder')
  142. ->with($this->transaction);
  143. $this->payment->method('setAmountAuthorized')
  144. ->with($totalDue);
  145. $this->express->authorizeOrder($this->order);
  146. }
  147. /**
  148. * Checks if payment authorization is allowed.
  149. *
  150. * @param string $method
  151. * @param string $action
  152. * @param float $authorizedAmount
  153. * @param bool $isAuthAllowed
  154. * @throws LocalizedException
  155. * @dataProvider paymentDataProvider
  156. */
  157. public function testIsOrderAuthorizationAllowed(
  158. string $method,
  159. string $action,
  160. float $authorizedAmount,
  161. bool $isAuthAllowed
  162. ) {
  163. $this->payment->method('getMethod')
  164. ->willReturn($method);
  165. $this->paymentInstance->method('getConfigPaymentAction')
  166. ->willReturn($action);
  167. $this->payment->method('getAmountAuthorized')
  168. ->willReturn($authorizedAmount);
  169. static::assertEquals($isAuthAllowed, $this->express->isOrderAuthorizationAllowed($this->payment));
  170. }
  171. /**
  172. * Data provider for payment methods call.
  173. *
  174. * @return array
  175. */
  176. public function paymentDataProvider(): array
  177. {
  178. return [
  179. ['paypal_express', 'sale', 10, false],
  180. ['paypal_express', 'order', 50, false],
  181. ['paypal_express', 'capture', 0, false],
  182. ['paypal_express', 'order', 0, true],
  183. ['braintree', 'authorize', 10, false],
  184. ['braintree', 'authorize', 0, false],
  185. ];
  186. }
  187. }