BeforeOrderPaymentSaveObserverTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflinePayments\Test\Unit\Observer;
  7. use Magento\Framework\Event;
  8. use Magento\Framework\Event\Observer;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\OfflinePayments\Model\Banktransfer;
  11. use Magento\OfflinePayments\Model\Cashondelivery;
  12. use Magento\OfflinePayments\Observer\BeforeOrderPaymentSaveObserver;
  13. use Magento\Sales\Model\Order\Payment;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. use Magento\OfflinePayments\Model\Checkmo;
  16. class BeforeOrderPaymentSaveObserverTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var BeforeOrderPaymentSaveObserver
  20. */
  21. protected $_model;
  22. /**
  23. * @var Payment|MockObject
  24. */
  25. private $payment;
  26. /**
  27. * @var Event|MockObject
  28. */
  29. private $event;
  30. /**
  31. * @var Observer|MockObject
  32. */
  33. private $observer;
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function setUp()
  38. {
  39. $objectManagerHelper = new ObjectManager($this);
  40. $this->payment = $this->getMockBuilder(Payment::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->event = $this->getMockBuilder(Event::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['getPayment'])
  46. ->getMock();
  47. $this->event->expects(self::once())
  48. ->method('getPayment')
  49. ->willReturn($this->payment);
  50. $this->observer = $this->getMockBuilder(Observer::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->observer->expects(self::once())
  54. ->method('getEvent')
  55. ->willReturn($this->event);
  56. $this->_model = $objectManagerHelper->getObject(BeforeOrderPaymentSaveObserver::class);
  57. }
  58. /**
  59. * @param string $methodCode
  60. * @dataProvider dataProviderBeforeOrderPaymentSaveWithInstructions
  61. */
  62. public function testBeforeOrderPaymentSaveWithInstructions($methodCode)
  63. {
  64. $this->payment->expects(self::once())
  65. ->method('getMethod')
  66. ->willReturn($methodCode);
  67. $this->payment->expects(self::once())
  68. ->method('setAdditionalInformation')
  69. ->with('instructions', 'payment configuration');
  70. $method = $this->getMockBuilder(Banktransfer::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $method->expects(self::once())
  74. ->method('getInstructions')
  75. ->willReturn('payment configuration');
  76. $this->payment->expects(self::once())
  77. ->method('getMethodInstance')
  78. ->willReturn($method);
  79. $this->_model->execute($this->observer);
  80. }
  81. /**
  82. * Returns list of payment method codes.
  83. *
  84. * @return array
  85. */
  86. public function dataProviderBeforeOrderPaymentSaveWithInstructions()
  87. {
  88. return [
  89. [Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE],
  90. [Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE],
  91. ];
  92. }
  93. public function testBeforeOrderPaymentSaveWithCheckmo()
  94. {
  95. $this->payment->expects(self::exactly(2))
  96. ->method('getMethod')
  97. ->willReturn(Checkmo::PAYMENT_METHOD_CHECKMO_CODE);
  98. $this->payment->expects(self::exactly(2))
  99. ->method('setAdditionalInformation')
  100. ->willReturnMap(
  101. [
  102. ['payable_to', 'payable to', $this->payment],
  103. ['mailing_address', 'mailing address', $this->payment],
  104. ]
  105. );
  106. $method = $this->getMockBuilder(Checkmo::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $method->expects(self::exactly(2))
  110. ->method('getPayableTo')
  111. ->willReturn('payable to');
  112. $method->expects(self::exactly(2))
  113. ->method('getMailingAddress')
  114. ->willReturn('mailing address');
  115. $this->payment->expects(self::once())
  116. ->method('getMethodInstance')
  117. ->willReturn($method);
  118. $this->_model->execute($this->observer);
  119. }
  120. /**
  121. * Checks a case when payment method is Check Money order and
  122. * payable person and mailing address do not specified.
  123. */
  124. public function testBeforeOrderPaymentSaveWithCheckmoWithoutConfig()
  125. {
  126. $this->payment->expects(self::exactly(2))
  127. ->method('getMethod')
  128. ->willReturn(Checkmo::PAYMENT_METHOD_CHECKMO_CODE);
  129. $this->payment->expects(self::never())
  130. ->method('setAdditionalInformation');
  131. $method = $this->getMockBuilder(Checkmo::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $method->expects(self::once())
  135. ->method('getPayableTo')
  136. ->willReturn(null);
  137. $method->expects(self::once())
  138. ->method('getMailingAddress')
  139. ->willReturn(null);
  140. $this->payment->expects(self::once())
  141. ->method('getMethodInstance')
  142. ->willReturn($method);
  143. $this->_model->execute($this->observer);
  144. }
  145. public function testBeforeOrderPaymentSaveWithOthers()
  146. {
  147. $this->payment->expects(self::exactly(2))
  148. ->method('getMethod')
  149. ->willReturn('somepaymentmethod');
  150. $this->payment->expects(self::never())
  151. ->method('setAdditionalInformation');
  152. $this->_model->execute($this->observer);
  153. }
  154. }