AbstractMethodTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Model\Method;
  7. use Magento\Framework\DataObject;
  8. use Magento\Payment\Model\InfoInterface;
  9. use Magento\Payment\Observer\AbstractDataAssignObserver;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Payment\Test\Unit\Model\Method\AbstractMethod\Stub;
  12. /**
  13. * Class AbstractMethodTest
  14. *
  15. * Test for class \Magento\Payment\Model\Method\AbstractMethod
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class AbstractMethodTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var \Magento\Payment\Model\Method\AbstractMethod
  22. */
  23. protected $payment;
  24. /**
  25. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $scopeConfigMock;
  28. /**
  29. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $eventManagerMock;
  32. /**
  33. * @var \Magento\Quote\Api\Data\CartInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $quoteMock;
  36. /**
  37. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $loggerMock;
  40. protected function setUp()
  41. {
  42. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  43. ->setMethods(['getValue'])
  44. ->getMockForAbstractClass();
  45. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  46. ->setMethods(['dispatch'])
  47. ->getMockForAbstractClass();
  48. $this->quoteMock = $this->getMockBuilder(\Magento\Quote\Api\Data\CartInterface::class)
  49. ->setMethods(['getStoreId'])
  50. ->getMockForAbstractClass();
  51. $contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['getEventDispatcher'])
  54. ->getMock();
  55. $contextMock->expects($this->once())
  56. ->method('getEventDispatcher')
  57. ->willReturn($this->eventManagerMock);
  58. $this->loggerMock = $this->getMockBuilder(\Magento\Payment\Model\Method\Logger::class)
  59. ->setConstructorArgs([$this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class)])
  60. ->setMethods(['debug'])
  61. ->getMock();
  62. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  63. $this->payment = $helper->getObject(
  64. Stub::class,
  65. [
  66. 'scopeConfig' => $this->scopeConfigMock,
  67. 'context' => $contextMock,
  68. 'logger' => $this->loggerMock
  69. ]
  70. );
  71. }
  72. public function testDebugData()
  73. {
  74. $debugData = ['masked' => '123'];
  75. $this->loggerMock->expects($this->once())
  76. ->method('debug')
  77. ->with($this->equalTo($debugData));
  78. $this->payment->debugData($debugData);
  79. }
  80. /**
  81. * @param bool $result
  82. *
  83. * @dataProvider dataProviderForTestIsAvailable
  84. */
  85. public function testIsAvailable($result)
  86. {
  87. $storeId = 15;
  88. $this->quoteMock->expects($this->once())
  89. ->method('getStoreId')
  90. ->willReturn($storeId);
  91. $this->scopeConfigMock->expects($this->once())
  92. ->method('getValue')
  93. ->with(
  94. 'payment/' . Stub::STUB_CODE . '/active',
  95. ScopeInterface::SCOPE_STORE,
  96. $storeId
  97. )->willReturn($result);
  98. $this->eventManagerMock->expects($result ? $this->once() : $this->never())
  99. ->method('dispatch')
  100. ->with(
  101. $this->equalTo('payment_method_is_active'),
  102. $this->countOf(3)
  103. );
  104. $this->assertEquals($result, $this->payment->isAvailable($this->quoteMock));
  105. }
  106. public function testAssignData()
  107. {
  108. $data = new DataObject();
  109. $paymentInfo = $this->createMock(InfoInterface::class);
  110. $this->payment->setInfoInstance($paymentInfo);
  111. $eventData = [
  112. AbstractDataAssignObserver::METHOD_CODE => $this,
  113. AbstractDataAssignObserver::MODEL_CODE => $paymentInfo,
  114. AbstractDataAssignObserver::DATA_CODE => $data
  115. ];
  116. $this->eventManagerMock->expects(static::exactly(2))
  117. ->method('dispatch')
  118. ->willReturnMap(
  119. [
  120. [
  121. 'payment_method_assign_data_' . Stub::STUB_CODE,
  122. $eventData
  123. ],
  124. [
  125. 'payment_method_assign_data',
  126. $eventData
  127. ]
  128. ]
  129. );
  130. $this->payment->assignData($data);
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function dataProviderForTestIsAvailable()
  136. {
  137. return [
  138. [
  139. 'result' => true
  140. ],
  141. [
  142. 'result' => false
  143. ],
  144. ];
  145. }
  146. }