PaymentVerificationFactoryTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Payment\Api\PaymentVerificationInterface;
  9. use Magento\Signifyd\Model\PaymentVerificationFactory;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  12. use Magento\Payment\Gateway\ConfigInterface;
  13. class PaymentVerificationFactoryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var PaymentVerificationFactory
  17. */
  18. private $factory;
  19. /**
  20. * @var ObjectManager
  21. */
  22. private $objectManager;
  23. /**
  24. * @var ObjectManagerInterface|MockObject
  25. */
  26. private $fakeObjectManager;
  27. /**
  28. * @var ConfigInterface|MockObject
  29. */
  30. private $config;
  31. /**
  32. * @var PaymentVerificationInterface|MockObject
  33. */
  34. private $avsDefaultAdapter;
  35. /**
  36. * @var PaymentVerificationInterface|MockObject
  37. */
  38. private $cvvDefaultAdapter;
  39. /**
  40. * @inheritdoc
  41. */
  42. protected function setUp()
  43. {
  44. $this->objectManager = new ObjectManager($this);
  45. $this->fakeObjectManager = $this->getMockBuilder(ObjectManagerInterface::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->config = $this->getMockBuilder(ConfigInterface::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->avsDefaultAdapter = $this->getMockBuilder(PaymentVerificationInterface::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->cvvDefaultAdapter = $this->getMockBuilder(PaymentVerificationInterface::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->factory = $this->objectManager->getObject(PaymentVerificationFactory::class, [
  58. 'objectManager' => $this->fakeObjectManager,
  59. 'config' => $this->config,
  60. 'avsDefaultAdapter' => $this->avsDefaultAdapter,
  61. 'cvvDefaultAdapter' => $this->cvvDefaultAdapter
  62. ]);
  63. }
  64. /**
  65. * Checks a test case when factory creates CVV mapper for provided payment method.
  66. *
  67. * @covers \Magento\Signifyd\Model\PaymentVerificationFactory::createPaymentCvv
  68. */
  69. public function testCreatePaymentCvv()
  70. {
  71. $paymentMethodCode = 'exists_payment';
  72. $this->config->expects(self::once())
  73. ->method('setMethodCode')
  74. ->with(self::equalTo($paymentMethodCode))
  75. ->willReturnSelf();
  76. $this->config->expects(self::once())
  77. ->method('getValue')
  78. ->with('cvv_ems_adapter')
  79. ->willReturn(PaymentVerificationInterface::class);
  80. /** @var PaymentVerificationInterface|MockObject $cvvAdapter */
  81. $cvvAdapter = $this->getMockBuilder(PaymentVerificationInterface::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->fakeObjectManager->expects(self::once())
  85. ->method('create')
  86. ->with(self::equalTo(PaymentVerificationInterface::class))
  87. ->willReturn($cvvAdapter);
  88. $mapper = $this->factory->createPaymentCvv($paymentMethodCode);
  89. self::assertInstanceOf(PaymentVerificationInterface::class, $mapper);
  90. }
  91. /**
  92. * Checks a test case, when provided payment method does not have cvv mapper.
  93. *
  94. * @covers \Magento\Signifyd\Model\PaymentVerificationFactory::createPaymentCvv
  95. */
  96. public function testCreateDefaultCvvMapper()
  97. {
  98. $paymentMethodCode = 'non_exists_payment';
  99. $this->config->expects(self::once())
  100. ->method('setMethodCode')
  101. ->with(self::equalTo($paymentMethodCode))
  102. ->willReturnSelf();
  103. $this->config->expects(self::once())
  104. ->method('getValue')
  105. ->with('cvv_ems_adapter')
  106. ->willReturn(null);
  107. $this->fakeObjectManager->expects(self::never())
  108. ->method('create');
  109. $mapper = $this->factory->createPaymentCvv($paymentMethodCode);
  110. self::assertSame($this->cvvDefaultAdapter, $mapper);
  111. }
  112. /**
  113. * Checks a test case, when mapper implementation does not corresponding to PaymentVerificationInterface.
  114. *
  115. * @covers \Magento\Signifyd\Model\PaymentVerificationFactory::createPaymentCvv
  116. * @expectedException \Magento\Framework\Exception\ConfigurationMismatchException
  117. * @expectedExceptionMessage stdClass must implement Magento\Payment\Api\PaymentVerificationInterface
  118. */
  119. public function testCreateWithUnsupportedImplementation()
  120. {
  121. $paymentMethodCode = 'exists_payment';
  122. $this->config->expects(self::once())
  123. ->method('setMethodCode')
  124. ->with(self::equalTo($paymentMethodCode))
  125. ->willReturnSelf();
  126. $this->config->expects(self::once())
  127. ->method('getValue')
  128. ->with('cvv_ems_adapter')
  129. ->willReturn(\stdClass::class);
  130. $cvvAdapter = new \stdClass();
  131. $this->fakeObjectManager->expects(self::once())
  132. ->method('create')
  133. ->with(self::equalTo(\stdClass::class))
  134. ->willReturn($cvvAdapter);
  135. $this->factory->createPaymentCvv($paymentMethodCode);
  136. }
  137. /**
  138. * Checks a test case when factory creates AVS mapper for provided payment method.
  139. *
  140. * @covers \Magento\Signifyd\Model\PaymentVerificationFactory::createPaymentAvs
  141. */
  142. public function testCreatePaymentAvs()
  143. {
  144. $paymentMethodCode = 'exists_payment';
  145. $this->config->expects(self::once())
  146. ->method('setMethodCode')
  147. ->with(self::equalTo($paymentMethodCode))
  148. ->willReturnSelf();
  149. $this->config->expects(self::once())
  150. ->method('getValue')
  151. ->with('avs_ems_adapter')
  152. ->willReturn(PaymentVerificationInterface::class);
  153. $avsAdapter = $this->getMockBuilder(PaymentVerificationInterface::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $this->fakeObjectManager->expects(self::once())
  157. ->method('create')
  158. ->with(self::equalTo(PaymentVerificationInterface::class))
  159. ->willReturn($avsAdapter);
  160. $mapper = $this->factory->createPaymentAvs($paymentMethodCode);
  161. self::assertInstanceOf(PaymentVerificationInterface::class, $mapper);
  162. }
  163. /**
  164. * Checks a test case when provided payment method does not support
  165. */
  166. public function testCreateDefaultAvsMapper()
  167. {
  168. $paymentMethodCode = 'non_exists_payment';
  169. $this->config->expects(self::once())
  170. ->method('setMethodCode')
  171. ->with(self::equalTo($paymentMethodCode))
  172. ->willReturnSelf();
  173. $this->config->expects(self::once())
  174. ->method('getValue')
  175. ->with('avs_ems_adapter')
  176. ->willReturn(null);
  177. $this->fakeObjectManager->expects(self::never())
  178. ->method('create');
  179. $mapper = $this->factory->createPaymentAvs($paymentMethodCode);
  180. self::assertSame($this->avsDefaultAdapter, $mapper);
  181. }
  182. }