AgreementTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Billing;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class AgreementTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Agreement
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $paymentDataMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $paymentMethodInstanceMock;
  22. protected function setUp()
  23. {
  24. $objectManager = new ObjectManager($this);
  25. $this->paymentDataMock = $this->getMockBuilder(\Magento\Payment\Helper\Data::class)
  26. ->disableOriginalConstructor()
  27. ->setMethods(['getMethodInstance'])
  28. ->getMock();
  29. $this->paymentMethodInstanceMock = $this->getMockBuilder(\Magento\Payment\Model\Method\AbstractMethod::class)
  30. ->disableOriginalConstructor()
  31. ->setMethods([
  32. 'setStore',
  33. 'getCode',
  34. 'getFormBlockType',
  35. 'getTitle',
  36. 'getStore',
  37. 'initBillingAgreementToken',
  38. 'getBillingAgreementTokenInfo',
  39. 'placeBillingAgreement'
  40. ])
  41. ->getMock();
  42. $this->model = $objectManager->getObject(
  43. \Magento\Paypal\Model\Billing\Agreement::class,
  44. ['paymentData' => $this->paymentDataMock]
  45. );
  46. }
  47. public function testImportOrderPaymentWithMethodCode()
  48. {
  49. $baData = [
  50. 'billing_agreement_id' => 'B-5E3253653W103435Y',
  51. 'method_code' => 'paypal_billing_agreement'
  52. ];
  53. $paymentMock = $this->importOrderPaymentCommonPart($baData);
  54. $paymentMock->expects($this->once())
  55. ->method('getMethodInstance')
  56. ->willReturn($this->paymentMethodInstanceMock);
  57. $this->paymentMethodInstanceMock->expects($this->once())
  58. ->method('getCode')
  59. ->willReturn($baData['method_code']);
  60. $this->paymentDataMock->expects($this->once())
  61. ->method('getMethodInstance')
  62. ->with($baData['method_code'])
  63. ->willReturn($this->paymentMethodInstanceMock);
  64. $this->assertSame($this->model, $this->model->importOrderPayment($paymentMock));
  65. }
  66. public function testImportOrderPaymentWithoutMethodCode()
  67. {
  68. $baData = [
  69. 'billing_agreement_id' => 'B-5E3253653W103435Y'
  70. ];
  71. $paymentMock = $this->importOrderPaymentCommonPart($baData);
  72. $paymentMock->expects($this->exactly(2))
  73. ->method('getMethodInstance')
  74. ->willReturn($this->paymentMethodInstanceMock);
  75. $this->paymentMethodInstanceMock->expects($this->once())
  76. ->method('getCode')
  77. ->willReturn('paypal_billing_agreement');
  78. $this->assertSame($this->model, $this->model->importOrderPayment($paymentMock));
  79. }
  80. /**
  81. * @param $baData
  82. * @return \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject|
  83. */
  84. private function importOrderPaymentCommonPart($baData)
  85. {
  86. $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  87. ->disableOriginalConstructor()
  88. ->setMethods(['getBillingAgreementData', 'getMethodInstance', 'getOrder'])
  89. ->getMock();
  90. $storeId = null;
  91. $customerId = 2;
  92. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  93. ->disableOriginalConstructor()
  94. ->setMethods(['getCustomerId'])
  95. ->getMock();
  96. $order->expects($this->once())
  97. ->method('getCustomerId')
  98. ->willReturn($customerId);
  99. $paymentMock->expects($this->once())
  100. ->method('getOrder')
  101. ->willReturn($order);
  102. $paymentMock->expects($this->once())
  103. ->method('getBillingAgreementData')
  104. ->willReturn($baData);
  105. $this->paymentMethodInstanceMock->expects($this->once())
  106. ->method('setStore')
  107. ->with($storeId);
  108. $this->paymentMethodInstanceMock->expects($this->once())
  109. ->method('getTitle')
  110. ->willReturn('some title');
  111. $this->paymentMethodInstanceMock->expects($this->once())
  112. ->method('getStore')
  113. ->willReturn($storeId);
  114. return $paymentMock;
  115. }
  116. public function testInitToken()
  117. {
  118. $this->initGetMethodInstance();
  119. $this->paymentMethodInstanceMock->expects($this->once())
  120. ->method('initBillingAgreementToken')
  121. ->with($this->model)
  122. ->willReturn($this->model);
  123. $url = 'http://dddd';
  124. $this->model->setRedirectUrl($url);
  125. $this->assertEquals($url, $this->model->initToken());
  126. }
  127. public function testVerifyToken()
  128. {
  129. $this->initGetMethodInstance();
  130. $this->paymentMethodInstanceMock->expects($this->once())
  131. ->method('getBillingAgreementTokenInfo')
  132. ->with($this->model)
  133. ->willReturn($this->model);
  134. $this->assertEquals($this->model, $this->model->verifyToken());
  135. }
  136. private function initGetMethodInstance()
  137. {
  138. $this->paymentDataMock->expects($this->once())
  139. ->method('getMethodInstance')
  140. ->willReturn($this->paymentMethodInstanceMock);
  141. $this->paymentMethodInstanceMock->expects($this->once())
  142. ->method('setStore');
  143. }
  144. }