AdapterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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\Event\ManagerInterface;
  8. use Magento\Payment\Gateway\Command\CommandManagerInterface;
  9. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  10. use Magento\Payment\Gateway\CommandInterface;
  11. use Magento\Payment\Gateway\Config\ValueHandlerInterface;
  12. use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
  13. use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
  14. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  15. use Magento\Payment\Gateway\Validator\ResultInterface;
  16. use Magento\Payment\Gateway\Validator\ValidatorInterface;
  17. use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
  18. use Magento\Payment\Model\InfoInterface;
  19. use Magento\Payment\Model\Method\Adapter;
  20. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class AdapterTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var MockObject|ManagerInterface
  28. */
  29. private $eventManager;
  30. /**
  31. * @var MockObject|ValueHandlerPoolInterface
  32. */
  33. private $valueHandlerPool;
  34. /**
  35. * @var MockObject|ValidatorPoolInterface
  36. */
  37. private $validatorPool;
  38. /**
  39. * @var MockObject|CommandPoolInterface
  40. */
  41. private $commandPool;
  42. /**
  43. * @var MockObject|PaymentDataObjectFactory
  44. */
  45. private $paymentDataObjectFactory;
  46. /**
  47. * @var MockObject
  48. */
  49. private $logger;
  50. /**
  51. * @var string
  52. */
  53. private $code;
  54. /**
  55. * @var string
  56. */
  57. private $formBlockType;
  58. /**
  59. * @var string
  60. */
  61. private $infoBlockType;
  62. /**
  63. * @var Adapter
  64. */
  65. private $adapter;
  66. protected function setUp()
  67. {
  68. $this->eventManager = $this->createMock(ManagerInterface::class);
  69. $this->valueHandlerPool = $this->createMock(ValueHandlerPoolInterface::class);
  70. $this->validatorPool = $this->createMock(ValidatorPoolInterface::class);
  71. $this->commandPool = $this->createMock(CommandPoolInterface::class);
  72. $this->paymentDataObjectFactory = $this->getMockBuilder(PaymentDataObjectFactory::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->code = 'CODE';
  76. $this->formBlockType = '\FormBlock';
  77. $this->infoBlockType = '\InfoBlock';
  78. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  79. ->getMock();
  80. $this->adapter = new Adapter(
  81. $this->eventManager,
  82. $this->valueHandlerPool,
  83. $this->paymentDataObjectFactory,
  84. $this->code,
  85. $this->formBlockType,
  86. $this->infoBlockType,
  87. $this->commandPool,
  88. $this->validatorPool,
  89. null,
  90. $this->logger
  91. );
  92. }
  93. public function testFetchTransactionInfo()
  94. {
  95. $transactionId = 10555;
  96. $transactionInfo = ['test_key' => 'test_value'];
  97. $valueHandler = $this->getMockForAbstractClass(ValueHandlerInterface::class);
  98. $command = $this->getMockForAbstractClass(CommandInterface::class);
  99. /** @var InfoInterface|MockObject $paymentInfo */
  100. $paymentInfo = $this->getMockForAbstractClass(InfoInterface::class);
  101. $paymentDO = $this->getMockForAbstractClass(PaymentDataObjectInterface::class);
  102. $this->valueHandlerPool->method('get')
  103. ->with('can_fetch_transaction_information')
  104. ->willReturn($valueHandler);
  105. $valueHandler->expects($this->atLeastOnce())
  106. ->method('handle')
  107. ->with(['field' => 'can_fetch_transaction_information'])
  108. ->willReturn(true);
  109. $this->paymentDataObjectFactory->method('create')
  110. ->with($paymentInfo)
  111. ->willReturn($paymentDO);
  112. $this->commandPool->method('get')
  113. ->with('fetch_transaction_information')
  114. ->willReturn($command);
  115. $command->expects($this->atLeastOnce())
  116. ->method('execute')
  117. ->with(['transactionId' => $transactionId, 'payment' => $paymentDO])
  118. ->willReturn($transactionInfo);
  119. $this->assertEquals(
  120. $transactionInfo,
  121. $this->adapter->fetchTransactionInfo($paymentInfo, $transactionId)
  122. );
  123. }
  124. /**
  125. * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
  126. */
  127. public function testIsAvailableNotActive()
  128. {
  129. $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
  130. $this->valueHandlerPool->expects(static::once())
  131. ->method('get')
  132. ->with('active')
  133. ->willReturn($activeValueHandler);
  134. $activeValueHandler->expects(static::once())
  135. ->method('handle')
  136. ->with(['field' => 'active'])
  137. ->willReturn(false);
  138. $this->eventManager->expects(static::never())
  139. ->method('dispatch');
  140. static::assertFalse($this->adapter->isAvailable(null));
  141. }
  142. /**
  143. * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
  144. */
  145. public function testIsAvailableEmptyQuote()
  146. {
  147. $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
  148. $availabilityValidator = $this->createMock(ValidatorInterface::class);
  149. $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
  150. $validationResult = $this->createMock(ResultInterface::class);
  151. $paymentInfo = $this->createMock(InfoInterface::class);
  152. $this->valueHandlerPool->expects(static::once())
  153. ->method('get')
  154. ->with('active')
  155. ->willReturn($activeValueHandler);
  156. $activeValueHandler->expects(static::once())
  157. ->method('handle')
  158. ->with(['field' => 'active', 'payment' => $paymentDO])
  159. ->willReturn(true);
  160. $this->validatorPool->expects(static::once())
  161. ->method('get')
  162. ->with('availability')
  163. ->willReturn($availabilityValidator);
  164. $this->paymentDataObjectFactory->expects(static::exactly(2))
  165. ->method('create')
  166. ->with($paymentInfo)
  167. ->willReturn($paymentDO);
  168. $availabilityValidator->expects(static::once())
  169. ->method('validate')
  170. ->willReturn($validationResult);
  171. $validationResult->expects(static::once())
  172. ->method('isValid')
  173. ->willReturn(true);
  174. $this->eventManager->expects(static::once())
  175. ->method('dispatch');
  176. $this->adapter->setInfoInstance($paymentInfo);
  177. static::assertTrue($this->adapter->isAvailable(null));
  178. }
  179. /**
  180. * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
  181. */
  182. public function testIsAvailableWithEmptyInfoInstance()
  183. {
  184. $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
  185. $this->valueHandlerPool->expects(static::once())
  186. ->method('get')
  187. ->with('active')
  188. ->willReturn($activeValueHandler);
  189. $activeValueHandler->expects(static::once())
  190. ->method('handle')
  191. ->with(['field' => 'active'])
  192. ->willReturn(true);
  193. $this->validatorPool->expects(static::never())
  194. ->method('get')
  195. ->with('availability');
  196. $this->eventManager->expects(static::once())
  197. ->method('dispatch');
  198. static::assertTrue($this->adapter->isAvailable(null));
  199. }
  200. public function testExecuteCommandWithCommandExecutor()
  201. {
  202. /** @var ManagerInterface|MockObject $eventManager */
  203. $eventManager = $this->createMock(
  204. ManagerInterface::class
  205. );
  206. /** @var ValueHandlerPoolInterface|MockObject $valueHandlerPool */
  207. $valueHandlerPool = $this->createMock(
  208. ValueHandlerPoolInterface::class
  209. );
  210. /** @var CommandManagerInterface|MockObject $commandManager */
  211. $commandManager = $this->createMock(
  212. CommandManagerInterface::class
  213. );
  214. /** @var PaymentDataObjectFactory|MockObject $paymentDataObjectFactory */
  215. $paymentDataObjectFactory = $this->getMockBuilder(
  216. PaymentDataObjectFactory::class
  217. )
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $paymentInfo = $this->createMock(InfoInterface::class);
  221. $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
  222. $adapter = new Adapter(
  223. $eventManager,
  224. $valueHandlerPool,
  225. $paymentDataObjectFactory,
  226. 'CODE',
  227. '\FormBlock',
  228. '\InfoBlock',
  229. null,
  230. null,
  231. $commandManager,
  232. $this->logger
  233. );
  234. $valueHandler = $this->createMock(ValueHandlerInterface::class);
  235. $valueHandlerPool->expects(static::once())
  236. ->method('get')
  237. ->with('can_authorize')
  238. ->willReturn($valueHandler);
  239. $valueHandler->expects(static::once())
  240. ->method('handle')
  241. ->with(['field' => 'can_authorize'])
  242. ->willReturn(true);
  243. $paymentDataObjectFactory->expects(static::once())
  244. ->method('create')
  245. ->with($paymentInfo)
  246. ->willReturn($paymentDO);
  247. $commandManager->expects(static::once())
  248. ->method('executeByCode')
  249. ->with('authorize', $paymentInfo, ['amount' => 10, 'payment' => $paymentDO])
  250. ->willReturn(null);
  251. $adapter->authorize($paymentInfo, 10);
  252. }
  253. public function testExecuteCommandWithCommandPool()
  254. {
  255. /** @var ManagerInterface|MockObject $eventManager */
  256. $eventManager = $this->createMock(ManagerInterface::class);
  257. /** @var ValueHandlerPoolInterface|MockObject $valueHandlerPool */
  258. $valueHandlerPool = $this->createMock(ValueHandlerPoolInterface::class);
  259. /** @var CommandPoolInterface|MockObject $commandPool */
  260. $commandPool = $this->createMock(CommandPoolInterface::class);
  261. /** @var PaymentDataObjectFactory|MockObject $paymentDataObjectFactory */
  262. $paymentDataObjectFactory = $this->getMockBuilder(PaymentDataObjectFactory::class)
  263. ->disableOriginalConstructor()
  264. ->getMock();
  265. $paymentInfo = $this->createMock(InfoInterface::class);
  266. $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
  267. $adapter = new Adapter(
  268. $eventManager,
  269. $valueHandlerPool,
  270. $paymentDataObjectFactory,
  271. 'CODE',
  272. '\FormBlock',
  273. '\InfoBlock',
  274. $commandPool,
  275. null,
  276. null,
  277. $this->logger
  278. );
  279. $valueHandler = $this->createMock(ValueHandlerInterface::class);
  280. $command = $this->createMock(CommandInterface::class);
  281. $valueHandlerPool->expects(static::once())
  282. ->method('get')
  283. ->with('can_authorize')
  284. ->willReturn($valueHandler);
  285. $valueHandler->expects(static::once())
  286. ->method('handle')
  287. ->with(['field' => 'can_authorize'])
  288. ->willReturn(true);
  289. $paymentDataObjectFactory->expects(static::once())
  290. ->method('create')
  291. ->with($paymentInfo)
  292. ->willReturn($paymentDO);
  293. $commandPool->expects(static::once())
  294. ->method('get')
  295. ->with('authorize')
  296. ->willReturn($command);
  297. $command->expects(static::once())
  298. ->method('execute')
  299. ->with(['amount' => 10, 'payment' => $paymentDO])
  300. ->willReturn(null);
  301. $adapter->authorize($paymentInfo, 10);
  302. }
  303. }