AddBillingAgreementToSessionObserverTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Observer;
  7. use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
  8. /**
  9. * Class AddBillingAgreementToSessionObserverTest
  10. */
  11. class AddBillingAgreementToSessionObserverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Paypal\Observer\AddBillingAgreementToSessionObserver
  15. */
  16. protected $_model;
  17. /**
  18. * @var \Magento\Framework\Event\Observer
  19. */
  20. protected $_observer;
  21. /**
  22. * @var \Magento\Framework\DataObject
  23. */
  24. protected $_event;
  25. /**
  26. * @var \Magento\Paypal\Model\Billing\Agreement Factory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_agreementFactory;
  29. /**
  30. * @var \Magento\Checkout\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $_checkoutSession;
  33. protected function setUp()
  34. {
  35. $this->_event = new \Magento\Framework\DataObject();
  36. $this->_observer = new \Magento\Framework\Event\Observer();
  37. $this->_observer->setEvent($this->_event);
  38. $this->_agreementFactory = $this->createPartialMock(
  39. \Magento\Paypal\Model\Billing\AgreementFactory::class,
  40. ['create']
  41. );
  42. $this->_checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class);
  43. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  44. $this->_model = $objectManagerHelper->getObject(
  45. \Magento\Paypal\Observer\AddBillingAgreementToSessionObserver::class,
  46. [
  47. 'agreementFactory' => $this->_agreementFactory,
  48. 'checkoutSession' => $this->_checkoutSession,
  49. ]
  50. );
  51. }
  52. public function testAddBillingAgreementToSessionNoData()
  53. {
  54. $payment = $this->createMock(\Magento\Sales\Model\Order\Payment::class);
  55. $payment->expects(
  56. $this->once()
  57. )->method(
  58. '__call'
  59. )->with(
  60. 'getBillingAgreementData'
  61. )->will(
  62. $this->returnValue(null)
  63. );
  64. $this->_event->setPayment($payment);
  65. $this->_agreementFactory->expects($this->never())->method('create');
  66. $this->_checkoutSession->expects($this->once())->method('__call')->with('unsLastBillingAgreementReferenceId');
  67. $this->_model->execute($this->_observer);
  68. }
  69. /**
  70. * @param bool $isValid
  71. * @dataProvider addBillingAgreementToSessionDataProvider
  72. */
  73. public function testAddBillingAgreementToSession($isValid)
  74. {
  75. $agreement = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  76. $agreement->expects($this->once())->method('isValid')->will($this->returnValue($isValid));
  77. $comment = $this->getMockForAbstractClass(
  78. \Magento\Framework\Model\AbstractModel::class,
  79. [],
  80. '',
  81. false,
  82. true,
  83. true,
  84. ['__wakeup']
  85. );
  86. $order = $this->createMock(\Magento\Sales\Model\Order::class);
  87. $order->expects(
  88. $this->once()
  89. )->method(
  90. 'addStatusHistoryComment'
  91. )->with(
  92. $isValid ? __(
  93. 'Created billing agreement #%1.',
  94. 'agreement reference id'
  95. ) : __(
  96. 'We can\'t create a billing agreement for this order.'
  97. )
  98. )->will(
  99. $this->returnValue($comment)
  100. );
  101. if ($isValid) {
  102. $agreement->expects(
  103. $this->any()
  104. )->method(
  105. '__call'
  106. )->with(
  107. 'getReferenceId'
  108. )->will(
  109. $this->returnValue('agreement reference id')
  110. );
  111. $agreement->expects($this->once())->method('addOrderRelation')->with($order);
  112. $order->expects(new MethodInvokedAtIndex(0))->method('addRelatedObject')->with($agreement);
  113. $this->_checkoutSession->expects(
  114. $this->once()
  115. )->method(
  116. '__call'
  117. )->with(
  118. 'setLastBillingAgreementReferenceId',
  119. ['agreement reference id']
  120. );
  121. } else {
  122. $this->_checkoutSession->expects(
  123. $this->once()
  124. )->method(
  125. '__call'
  126. )->with(
  127. 'unsLastBillingAgreementReferenceId'
  128. );
  129. $agreement->expects($this->never())->method('__call');
  130. }
  131. $order->expects(new MethodInvokedAtIndex($isValid ? 1 : 0))->method('addRelatedObject')->with($comment);
  132. $payment = $this->createMock(\Magento\Sales\Model\Order\Payment::class);
  133. $payment->expects(
  134. $this->once()
  135. )->method(
  136. '__call'
  137. )->with(
  138. 'getBillingAgreementData'
  139. )->will(
  140. $this->returnValue('not empty')
  141. );
  142. $payment->expects($this->once())->method('getOrder')->will($this->returnValue($order));
  143. $agreement->expects(
  144. $this->once()
  145. )->method(
  146. 'importOrderPayment'
  147. )->with(
  148. $payment
  149. )->will(
  150. $this->returnValue($agreement)
  151. );
  152. $this->_event->setPayment($payment);
  153. $this->_agreementFactory->expects($this->once())->method('create')->will($this->returnValue($agreement));
  154. $this->_model->execute($this->_observer);
  155. }
  156. /**
  157. * @return array
  158. */
  159. public function addBillingAgreementToSessionDataProvider()
  160. {
  161. return [[true], [false]];
  162. }
  163. }