PayflowlinkTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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;
  7. use Magento\Paypal\Block\Payment\Info;
  8. use Magento\Paypal\Model\Payflowlink;
  9. use Magento\Store\Model\ScopeInterface;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class PayflowlinkTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var Payflowlink */
  17. protected $model;
  18. /** @var \Magento\Sales\Model\Order\Payment|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $infoInstance;
  20. /** @var \Magento\Paypal\Model\Payflow\Request|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $payflowRequest;
  22. /** @var \Magento\Paypal\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $paypalConfig;
  24. /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $store;
  26. /** @var \Magento\Paypal\Model\Payflow\Service\Gateway|\PHPUnit_Framework_MockObject_MockObject */
  27. private $gatewayMock;
  28. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $scopeConfigMock;
  30. protected function setUp()
  31. {
  32. $this->store = $this->createMock(\Magento\Store\Model\Store::class);
  33. $storeManager = $this->createMock(
  34. \Magento\Store\Model\StoreManagerInterface::class
  35. );
  36. $this->paypalConfig = $this->getMockBuilder(\Magento\Paypal\Model\Config::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $configFactoryMock = $this->getMockBuilder(\Magento\Payment\Model\Method\ConfigInterfaceFactory::class)
  40. ->setMethods(['create'])
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $requestFactory = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\RequestFactory::class)
  44. ->setMethods(['create'])
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->payflowRequest = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Request::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->infoInstance = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  54. ->getMockForAbstractClass();
  55. $this->gatewayMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Service\Gateway::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  59. $configFactoryMock->expects($this->any())
  60. ->method('create')
  61. ->willReturn($this->paypalConfig);
  62. $this->payflowRequest->expects($this->any())
  63. ->method('__call')
  64. ->will($this->returnCallback(function ($method) {
  65. if (strpos($method, 'set') === 0) {
  66. return $this->payflowRequest;
  67. }
  68. return null;
  69. }));
  70. $requestFactory->expects($this->any())->method('create')->will($this->returnValue($this->payflowRequest));
  71. $helper = new ObjectManagerHelper($this);
  72. $this->model = $helper->getObject(
  73. \Magento\Paypal\Model\Payflowlink::class,
  74. [
  75. 'scopeConfig' => $this->scopeConfigMock,
  76. 'storeManager' => $storeManager,
  77. 'configFactory' => $configFactoryMock,
  78. 'requestFactory' => $requestFactory,
  79. 'gateway' => $this->gatewayMock,
  80. ]
  81. );
  82. $this->model->setInfoInstance($this->infoInstance);
  83. }
  84. public function testInitialize()
  85. {
  86. $storeId = 1;
  87. $order = $this->createMock(\Magento\Sales\Model\Order::class);
  88. $order->expects($this->exactly(2))
  89. ->method('getStoreId')
  90. ->willReturn($storeId);
  91. $this->infoInstance->expects($this->any())
  92. ->method('getOrder')
  93. ->willReturn($order);
  94. $this->infoInstance->expects($this->any())
  95. ->method('setAdditionalInformation')
  96. ->willReturnSelf();
  97. $this->paypalConfig->expects($this->once())
  98. ->method('getBuildNotationCode')
  99. ->willReturn('build notation code');
  100. $response = new \Magento\Framework\DataObject(
  101. [
  102. 'result' => '0',
  103. 'pnref' => 'V19A3D27B61E',
  104. 'respmsg' => 'Approved',
  105. 'authcode' => '510PNI',
  106. 'hostcode' => 'A',
  107. 'request_id' => 'f930d3dc6824c1f7230c5529dc37ae5e',
  108. 'result_code' => '0',
  109. ]
  110. );
  111. $this->gatewayMock->expects($this->once())
  112. ->method('postRequest')
  113. ->willReturn($response);
  114. $this->payflowRequest->expects($this->exactly(4))
  115. ->method('setData')
  116. ->willReturnMap(
  117. [
  118. [
  119. 'user' => null,
  120. 'vendor' => null,
  121. 'partner' => null,
  122. 'pwd' => null,
  123. 'verbosity' => null,
  124. 'BUTTONSOURCE' => 'build notation code',
  125. 'tender' => 'C',
  126. ],
  127. $this->returnSelf()
  128. ],
  129. ['USER1', 1, $this->returnSelf()],
  130. ['USER2', 'a20d3dc6824c1f7780c5529dc37ae5e', $this->returnSelf()]
  131. );
  132. $stateObject = new \Magento\Framework\DataObject();
  133. $this->model->initialize(\Magento\Paypal\Model\Config::PAYMENT_ACTION_AUTH, $stateObject);
  134. self::assertEquals($storeId, $this->model->getStore(), '{Store} should be set');
  135. }
  136. /**
  137. * @param bool $expectedResult
  138. * @param string $configResult
  139. * @dataProvider dataProviderForTestIsActive
  140. */
  141. public function testIsActive($expectedResult, $configResult)
  142. {
  143. $storeId = 15;
  144. $this->scopeConfigMock->expects($this->once())
  145. ->method('getValue')
  146. ->with(
  147. "payment/payflow_link/active",
  148. ScopeInterface::SCOPE_STORE,
  149. $storeId
  150. )->willReturn($configResult);
  151. $this->assertEquals($expectedResult, $this->model->isActive($storeId));
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function dataProviderForTestIsActive()
  157. {
  158. return [
  159. [false, '0'],
  160. [true, '1']
  161. ];
  162. }
  163. /**
  164. * @covers \Magento\Paypal\Model\Payflowlink::getInfoBlockType()
  165. */
  166. public function testGetInfoBlockType()
  167. {
  168. static::assertEquals(Info::class, $this->model->getInfoBlockType());
  169. }
  170. }