ExpressTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model;
  7. use Magento\Checkout\Model\Session;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\Event\ManagerInterface;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Payment\Model\InfoInterface;
  12. use Magento\Payment\Observer\AbstractDataAssignObserver;
  13. use Magento\Paypal\Model\Api\Nvp;
  14. use Magento\Paypal\Model\Api\ProcessableException as ApiProcessableException;
  15. use Magento\Paypal\Model\Express;
  16. use Magento\Paypal\Model\Pro;
  17. use Magento\Quote\Api\Data\PaymentInterface;
  18. use Magento\Sales\Model\Order;
  19. use Magento\Sales\Model\Order\Payment;
  20. use Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface;
  21. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  22. /**
  23. * Class ExpressTest
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class ExpressTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * @var array
  30. */
  31. protected $errorCodes = [
  32. ApiProcessableException::API_INTERNAL_ERROR,
  33. ApiProcessableException::API_UNABLE_PROCESS_PAYMENT_ERROR_CODE,
  34. ApiProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL,
  35. ApiProcessableException::API_UNABLE_TRANSACTION_COMPLETE,
  36. ApiProcessableException::API_TRANSACTION_EXPIRED,
  37. ApiProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED,
  38. ApiProcessableException::API_COUNTRY_FILTER_DECLINE,
  39. ApiProcessableException::API_MAXIMUM_AMOUNT_FILTER_DECLINE,
  40. ApiProcessableException::API_OTHER_FILTER_DECLINE,
  41. ApiProcessableException::API_ADDRESS_MATCH_FAIL
  42. ];
  43. /**
  44. * @var Express
  45. */
  46. private $model;
  47. /**
  48. * @var Session|MockObject
  49. */
  50. private $checkoutSession;
  51. /**
  52. * @var Pro|MockObject
  53. */
  54. private $pro;
  55. /**
  56. * @var Nvp|MockObject
  57. */
  58. private $nvp;
  59. /**
  60. * @var ObjectManager
  61. */
  62. private $helper;
  63. /**
  64. * @var BuilderInterface|MockObject
  65. */
  66. private $transactionBuilder;
  67. /**
  68. * @var ManagerInterface|MockObject
  69. */
  70. private $eventManager;
  71. protected function setUp()
  72. {
  73. $this->checkoutSession = $this->createPartialMock(
  74. Session::class,
  75. ['getPaypalTransactionData', 'setPaypalTransactionData']
  76. );
  77. $this->transactionBuilder = $this->getMockForAbstractClass(
  78. BuilderInterface::class,
  79. [],
  80. '',
  81. false,
  82. false
  83. );
  84. $this->nvp = $this->createPartialMock(
  85. Nvp::class,
  86. [
  87. 'setProcessableErrors',
  88. 'setAmount',
  89. 'setCurrencyCode',
  90. 'setTransactionId',
  91. 'callDoAuthorization',
  92. 'setData',
  93. ]
  94. );
  95. $this->pro = $this->createPartialMock(
  96. Pro::class,
  97. ['setMethod', 'getApi', 'importPaymentInfo', 'resetApi']
  98. );
  99. $this->eventManager = $this->getMockBuilder(ManagerInterface::class)
  100. ->setMethods(['dispatch'])
  101. ->getMockForAbstractClass();
  102. $this->pro->expects($this->any())->method('getApi')->will($this->returnValue($this->nvp));
  103. $this->helper = new ObjectManager($this);
  104. }
  105. public function testSetApiProcessableErrors()
  106. {
  107. $this->nvp->expects($this->once())->method('setProcessableErrors')->with($this->errorCodes);
  108. $this->model = $this->helper->getObject(
  109. \Magento\Paypal\Model\Express::class,
  110. [
  111. 'data' => [$this->pro],
  112. 'checkoutSession' => $this->checkoutSession,
  113. 'transactionBuilder' => $this->transactionBuilder,
  114. ]
  115. );
  116. }
  117. /**
  118. * Tests order payment action.
  119. *
  120. * @return void
  121. */
  122. public function testOrder()
  123. {
  124. $transactionData = ['TOKEN' => 'EC-7NJ4634216284232D'];
  125. $this->checkoutSession
  126. ->method('getPaypalTransactionData')
  127. ->willReturn($transactionData);
  128. $order = $this->createPartialMock(Order::class, ['setActionFlag']);
  129. $order->method('setActionFlag')
  130. ->with(Order::ACTION_FLAG_INVOICE, false)
  131. ->willReturnSelf();
  132. $paymentModel = $this->createPartialMock(Payment::class, ['getOrder']);
  133. $paymentModel->method('getOrder')
  134. ->willReturn($order);
  135. $this->model = $this->helper->getObject(
  136. \Magento\Paypal\Model\Express::class,
  137. [
  138. 'data' => [$this->pro],
  139. 'checkoutSession' => $this->checkoutSession,
  140. ]
  141. );
  142. $this->nvp->method('setData')
  143. ->with($transactionData)
  144. ->willReturnSelf();
  145. static::assertEquals($this->model, $this->model->order($paymentModel, 12.3));
  146. }
  147. public function testAssignData()
  148. {
  149. $transportValue = 'something';
  150. $extensionAttribute = $this->getMockForAbstractClass(
  151. \Magento\Quote\Api\Data\PaymentExtensionInterface::class,
  152. [],
  153. '',
  154. false,
  155. false
  156. );
  157. $data = new DataObject(
  158. [
  159. PaymentInterface::KEY_ADDITIONAL_DATA => [
  160. Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => $transportValue,
  161. Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID => $transportValue,
  162. Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN => $transportValue,
  163. \Magento\Framework\Api\ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttribute
  164. ]
  165. ]
  166. );
  167. $this->model = $this->helper->getObject(
  168. \Magento\Paypal\Model\Express::class,
  169. [
  170. 'data' => [$this->pro],
  171. 'checkoutSession' => $this->checkoutSession,
  172. 'transactionBuilder' => $this->transactionBuilder,
  173. 'eventDispatcher' => $this->eventManager,
  174. ]
  175. );
  176. $paymentInfo = $this->createMock(InfoInterface::class);
  177. $this->model->setInfoInstance($paymentInfo);
  178. $this->parentAssignDataExpectation($data);
  179. $paymentInfo->expects(static::exactly(3))
  180. ->method('setAdditionalInformation')
  181. ->withConsecutive(
  182. [Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT, $transportValue],
  183. [Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID, $transportValue],
  184. [Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN, $transportValue]
  185. );
  186. $this->model->assignData($data);
  187. }
  188. /**
  189. * @param DataObject $data
  190. */
  191. private function parentAssignDataExpectation(DataObject $data)
  192. {
  193. $eventData = [
  194. AbstractDataAssignObserver::METHOD_CODE => $this,
  195. AbstractDataAssignObserver::MODEL_CODE => $this->model->getInfoInstance(),
  196. AbstractDataAssignObserver::DATA_CODE => $data
  197. ];
  198. $this->eventManager->expects(static::exactly(2))
  199. ->method('dispatch')
  200. ->willReturnMap(
  201. [
  202. [
  203. 'payment_method_assign_data_' . $this->model->getCode(),
  204. $eventData,
  205. ],
  206. [
  207. 'payment_method_assign_data',
  208. $eventData,
  209. ]
  210. ]
  211. );
  212. }
  213. }