PlaceOrderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Paypal\Test\Unit\Controller\Express;
  8. class PlaceOrderTest extends \Magento\Paypal\Test\Unit\Controller\ExpressTest
  9. {
  10. protected $name = 'PlaceOrder';
  11. /**
  12. * @param bool $isGeneral
  13. * @dataProvider trueFalseDataProvider
  14. */
  15. public function testExecuteNonProcessableException($isGeneral)
  16. {
  17. if (!$isGeneral) {
  18. $this->request->expects($this->once())
  19. ->method('getPost')
  20. ->with('agreement', [])
  21. ->will($this->returnValue([]));
  22. }
  23. $this->_expectRedirect();
  24. $this->model->execute();
  25. }
  26. /**
  27. * @param string $path
  28. */
  29. protected function _expectRedirect($path = '*/*/review')
  30. {
  31. $this->redirect->expects($this->once())
  32. ->method('redirect')
  33. ->with($this->anything(), $path, []);
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function trueFalseDataProvider()
  39. {
  40. return [[true], [false]];
  41. }
  42. /**
  43. * @param int $code
  44. * @param null|string $paymentAction
  45. * @dataProvider executeProcessableExceptionDataProvider
  46. */
  47. public function testExecuteProcessableException($code, $paymentAction = null)
  48. {
  49. $this->request->expects($this->once())
  50. ->method('getPost')
  51. ->with('agreement', [])
  52. ->will($this->returnValue([]));
  53. $oldCallback = &$this->objectManagerCallback;
  54. $this->objectManagerCallback = function ($className) use ($code, $oldCallback) {
  55. $instance = call_user_func($oldCallback, $className);
  56. if ($className == \Magento\CheckoutAgreements\Model\AgreementsValidator::class) {
  57. $exception = $this->getMock(
  58. \Magento\Paypal\Model\Api\ProcessableException::class,
  59. ['getUserMessage'],
  60. ['message', $code]
  61. );
  62. $exception->expects($this->any())
  63. ->method('getUserMessage')
  64. ->will($this->returnValue('User Message'));
  65. $instance->expects($this->once())
  66. ->method('isValid')
  67. ->will($this->throwException($exception));
  68. }
  69. return $instance;
  70. };
  71. if (isset($paymentAction)) {
  72. $this->config->expects($this->once())
  73. ->method('getPaymentAction')
  74. ->will($this->returnValue($paymentAction));
  75. }
  76. $this->_expectErrorCodes($code, $paymentAction);
  77. $this->model->execute();
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function executeProcessableExceptionDataProvider()
  83. {
  84. return [
  85. [\Magento\Paypal\Model\Api\ProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED],
  86. [\Magento\Paypal\Model\Api\ProcessableException::API_TRANSACTION_EXPIRED],
  87. [\Magento\Paypal\Model\Api\ProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL],
  88. [
  89. \Magento\Paypal\Model\Api\ProcessableException::API_UNABLE_TRANSACTION_COMPLETE,
  90. \Magento\Payment\Model\Method\AbstractMethod::ACTION_ORDER
  91. ],
  92. [\Magento\Paypal\Model\Api\ProcessableException::API_UNABLE_TRANSACTION_COMPLETE, 'other'],
  93. [999999],
  94. ];
  95. }
  96. /**
  97. * @param int $code
  98. * @param null|string $paymentAction
  99. */
  100. protected function _expectErrorCodes($code, $paymentAction)
  101. {
  102. $redirectUrl = 'redirect by test';
  103. if (in_array(
  104. $code,
  105. [
  106. \Magento\Paypal\Model\Api\ProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED,
  107. \Magento\Paypal\Model\Api\ProcessableException::API_TRANSACTION_EXPIRED,
  108. ]
  109. )
  110. ) {
  111. $payment = new \Magento\Framework\DataObject(['checkout_redirect_url' => $redirectUrl]);
  112. $this->quote->expects($this->once())
  113. ->method('getPayment')
  114. ->will($this->returnValue($payment));
  115. }
  116. if ($code == \Magento\Paypal\Model\Api\ProcessableException::API_UNABLE_TRANSACTION_COMPLETE
  117. && $paymentAction == \Magento\Payment\Model\Method\AbstractMethod::ACTION_ORDER
  118. ) {
  119. $this->config->expects($this->once())
  120. ->method('getExpressCheckoutOrderUrl')
  121. ->will($this->returnValue($redirectUrl));
  122. }
  123. if ($code == \Magento\Paypal\Model\Api\ProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL
  124. || $code == \Magento\Paypal\Model\Api\ProcessableException::API_UNABLE_TRANSACTION_COMPLETE
  125. && $paymentAction != \Magento\Payment\Model\Method\AbstractMethod::ACTION_ORDER
  126. ) {
  127. $this->config->expects($this->once())
  128. ->method('getExpressCheckoutStartUrl')
  129. ->will($this->returnValue($redirectUrl));
  130. $this->request->expects($this->once())
  131. ->method('getParam')
  132. ->with('token');
  133. }
  134. if (in_array(
  135. $code,
  136. [
  137. \Magento\Paypal\Model\Api\ProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED,
  138. \Magento\Paypal\Model\Api\ProcessableException::API_TRANSACTION_EXPIRED,
  139. \Magento\Paypal\Model\Api\ProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL,
  140. \Magento\Paypal\Model\Api\ProcessableException::API_UNABLE_TRANSACTION_COMPLETE,
  141. ]
  142. )
  143. ) {
  144. $this->response->expects($this->once())
  145. ->method('setRedirect')
  146. ->with($redirectUrl);
  147. } else {
  148. $this->messageManager->expects($this->once())
  149. ->method('addError')
  150. ->with('User Message');
  151. $this->_expectRedirect('checkout/cart');
  152. }
  153. }
  154. }