PayflowConfigTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Paypal\Model\PayflowConfig;
  10. use Magento\Payment\Model\MethodInterface;
  11. use Magento\Payment\Model\Method\AbstractMethod;
  12. use Magento\Paypal\Model\Config;
  13. use Magento\Store\Model\ScopeInterface;
  14. /**
  15. * Class PayflowConfigTest
  16. */
  17. class PayflowConfigTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $scopeConfigMock;
  23. /**
  24. * @var MethodInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $methodInterfaceMock;
  27. /**
  28. * @var PayflowConfig|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $config;
  31. protected function setUp()
  32. {
  33. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  34. ->setMethods(['getValue', 'isSetFlag'])
  35. ->getMockForAbstractClass();
  36. $this->methodInterfaceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  37. ->getMockForAbstractClass();
  38. $om = new ObjectManager($this);
  39. $this->config = $om->getObject(
  40. \Magento\Paypal\Model\PayflowConfig::class,
  41. [
  42. 'scopeConfig' => $this->scopeConfigMock
  43. ]
  44. );
  45. }
  46. /**
  47. * @param string $paymentAction
  48. * @param string|null $expectedValue
  49. *
  50. * @dataProvider getTrxTypeDataProvider
  51. */
  52. public function testGetTrxType($paymentAction, $expectedValue)
  53. {
  54. $this->scopeConfigMock->expects($this->any())
  55. ->method('getValue')
  56. ->willReturn($paymentAction);
  57. $this->assertEquals($expectedValue, $this->config->getTrxType());
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getTrxTypeDataProvider()
  63. {
  64. return [
  65. [PayflowConfig::PAYMENT_ACTION_AUTH, PayflowConfig::TRXTYPE_AUTH_ONLY],
  66. [PayflowConfig::PAYMENT_ACTION_SALE, PayflowConfig::TRXTYPE_SALE],
  67. ['other', null],
  68. ];
  69. }
  70. /**
  71. * @param string $paymentAction
  72. * @param string|null $expectedValue
  73. *
  74. * @dataProvider getPaymentActionDataProvider
  75. */
  76. public function testGetPaymentAction($paymentAction, $expectedValue)
  77. {
  78. $this->scopeConfigMock->expects($this->any())
  79. ->method('getValue')
  80. ->willReturn($paymentAction);
  81. $this->assertEquals($expectedValue, $this->config->getPaymentAction());
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function getPaymentActionDataProvider()
  87. {
  88. return [
  89. [PayflowConfig::PAYMENT_ACTION_AUTH, AbstractMethod::ACTION_AUTHORIZE],
  90. [PayflowConfig::PAYMENT_ACTION_SALE, AbstractMethod::ACTION_AUTHORIZE_CAPTURE],
  91. ['other', null],
  92. ];
  93. }
  94. public function testGetTransactionUrlWithTestModeOn()
  95. {
  96. $this->scopeConfigMock->expects($this->never())
  97. ->method('getValue');
  98. $this->methodInterfaceMock->expects($this->once())
  99. ->method('getConfigData')
  100. ->with('transaction_url_test_mode')
  101. ->willReturn('transaction_url_test_mode');
  102. $this->config->setMethodInstance($this->methodInterfaceMock);
  103. $this->assertEquals('transaction_url_test_mode', $this->config->getTransactionUrl(1));
  104. }
  105. public function testGetTransactionUrlWithTestModeOff()
  106. {
  107. $this->scopeConfigMock->expects($this->never())
  108. ->method('getValue');
  109. $this->methodInterfaceMock->expects($this->once())
  110. ->method('getConfigData')
  111. ->with('transaction_url')
  112. ->willReturn('transaction_url');
  113. $this->config->setMethodInstance($this->methodInterfaceMock);
  114. $this->assertEquals('transaction_url', $this->config->getTransactionUrl(0));
  115. }
  116. public function testGetTransactionUrlWithTestModeEmptyAndSandboxOn()
  117. {
  118. $this->scopeConfigMock->expects($this->once())
  119. ->method('getValue')
  120. ->willReturn(1);
  121. $this->methodInterfaceMock->expects($this->once())
  122. ->method('getConfigData')
  123. ->with('transaction_url_test_mode')
  124. ->willReturn('transaction_url_test_mode');
  125. $this->config->setMethodInstance($this->methodInterfaceMock);
  126. $this->assertEquals('transaction_url_test_mode', $this->config->getTransactionUrl());
  127. }
  128. public function testGetTransactionUrlWithTestModeEmptyAndSandboxOff()
  129. {
  130. $this->scopeConfigMock->expects($this->once())
  131. ->method('getValue')
  132. ->willReturn(0);
  133. $this->methodInterfaceMock->expects($this->once())
  134. ->method('getConfigData')
  135. ->with('transaction_url')
  136. ->willReturn('transaction_url');
  137. $this->config->setMethodInstance($this->methodInterfaceMock);
  138. $this->assertEquals('transaction_url', $this->config->getTransactionUrl());
  139. }
  140. /**
  141. * @param array $expectsMethods
  142. * @param string $currentMethod
  143. * @param bool $result
  144. *
  145. * @dataProvider dataProviderForTestIsMethodActive
  146. */
  147. public function testIsMethodActive(array $expectsMethods, $currentMethod, $result)
  148. {
  149. $this->config->setStoreId(5);
  150. $this->scopeConfigMock->expects($this->any())
  151. ->method('getValue')
  152. ->with('paypal/general/merchant_country')
  153. ->will($this->returnValue('US'));
  154. $i = 0;
  155. foreach ($expectsMethods as $method => $isActive) {
  156. $this->scopeConfigMock->expects($this->at($i++))
  157. ->method('isSetFlag')
  158. ->with(
  159. "payment/{$method}/active",
  160. ScopeInterface::SCOPE_STORE,
  161. 5
  162. )->willReturn($isActive);
  163. }
  164. $this->assertEquals($result, $this->config->isMethodActive($currentMethod));
  165. }
  166. /**
  167. * @return array
  168. */
  169. public function dataProviderForTestIsMethodActive()
  170. {
  171. return [
  172. [
  173. 'expectsMethods' => [
  174. Config::METHOD_PAYMENT_PRO => 0,
  175. Config::METHOD_PAYFLOWPRO => 1,
  176. ],
  177. 'currentMethod' => Config::METHOD_PAYMENT_PRO,
  178. 'result' => true,
  179. ],
  180. [
  181. 'expectsMethods' => [
  182. Config::METHOD_PAYMENT_PRO => 1
  183. ],
  184. 'currentMethod' => Config::METHOD_PAYFLOWPRO,
  185. 'result' => true,
  186. ],
  187. [
  188. 'expectsMethods' => [
  189. Config::METHOD_PAYMENT_PRO => 0,
  190. Config::METHOD_PAYFLOWPRO => 0,
  191. ],
  192. 'currentMethod' => 777,
  193. 'result' => false,
  194. ],
  195. ];
  196. }
  197. }