123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Payment\Test\Unit\Model\Method;
- use Magento\Framework\Event\ManagerInterface;
- use Magento\Payment\Gateway\Command\CommandManagerInterface;
- use Magento\Payment\Gateway\Command\CommandPoolInterface;
- use Magento\Payment\Gateway\CommandInterface;
- use Magento\Payment\Gateway\Config\ValueHandlerInterface;
- use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
- use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
- use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
- use Magento\Payment\Gateway\Validator\ResultInterface;
- use Magento\Payment\Gateway\Validator\ValidatorInterface;
- use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
- use Magento\Payment\Model\InfoInterface;
- use Magento\Payment\Model\Method\Adapter;
- use PHPUnit_Framework_MockObject_MockObject as MockObject;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AdapterTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var MockObject|ManagerInterface
- */
- private $eventManager;
- /**
- * @var MockObject|ValueHandlerPoolInterface
- */
- private $valueHandlerPool;
- /**
- * @var MockObject|ValidatorPoolInterface
- */
- private $validatorPool;
- /**
- * @var MockObject|CommandPoolInterface
- */
- private $commandPool;
- /**
- * @var MockObject|PaymentDataObjectFactory
- */
- private $paymentDataObjectFactory;
- /**
- * @var MockObject
- */
- private $logger;
- /**
- * @var string
- */
- private $code;
- /**
- * @var string
- */
- private $formBlockType;
- /**
- * @var string
- */
- private $infoBlockType;
- /**
- * @var Adapter
- */
- private $adapter;
- protected function setUp()
- {
- $this->eventManager = $this->createMock(ManagerInterface::class);
- $this->valueHandlerPool = $this->createMock(ValueHandlerPoolInterface::class);
- $this->validatorPool = $this->createMock(ValidatorPoolInterface::class);
- $this->commandPool = $this->createMock(CommandPoolInterface::class);
- $this->paymentDataObjectFactory = $this->getMockBuilder(PaymentDataObjectFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->code = 'CODE';
- $this->formBlockType = '\FormBlock';
- $this->infoBlockType = '\InfoBlock';
- $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
- ->getMock();
- $this->adapter = new Adapter(
- $this->eventManager,
- $this->valueHandlerPool,
- $this->paymentDataObjectFactory,
- $this->code,
- $this->formBlockType,
- $this->infoBlockType,
- $this->commandPool,
- $this->validatorPool,
- null,
- $this->logger
- );
- }
- public function testFetchTransactionInfo()
- {
- $transactionId = 10555;
- $transactionInfo = ['test_key' => 'test_value'];
- $valueHandler = $this->getMockForAbstractClass(ValueHandlerInterface::class);
- $command = $this->getMockForAbstractClass(CommandInterface::class);
- /** @var InfoInterface|MockObject $paymentInfo */
- $paymentInfo = $this->getMockForAbstractClass(InfoInterface::class);
- $paymentDO = $this->getMockForAbstractClass(PaymentDataObjectInterface::class);
- $this->valueHandlerPool->method('get')
- ->with('can_fetch_transaction_information')
- ->willReturn($valueHandler);
- $valueHandler->expects($this->atLeastOnce())
- ->method('handle')
- ->with(['field' => 'can_fetch_transaction_information'])
- ->willReturn(true);
- $this->paymentDataObjectFactory->method('create')
- ->with($paymentInfo)
- ->willReturn($paymentDO);
- $this->commandPool->method('get')
- ->with('fetch_transaction_information')
- ->willReturn($command);
- $command->expects($this->atLeastOnce())
- ->method('execute')
- ->with(['transactionId' => $transactionId, 'payment' => $paymentDO])
- ->willReturn($transactionInfo);
- $this->assertEquals(
- $transactionInfo,
- $this->adapter->fetchTransactionInfo($paymentInfo, $transactionId)
- );
- }
- /**
- * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
- */
- public function testIsAvailableNotActive()
- {
- $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
- $this->valueHandlerPool->expects(static::once())
- ->method('get')
- ->with('active')
- ->willReturn($activeValueHandler);
- $activeValueHandler->expects(static::once())
- ->method('handle')
- ->with(['field' => 'active'])
- ->willReturn(false);
- $this->eventManager->expects(static::never())
- ->method('dispatch');
- static::assertFalse($this->adapter->isAvailable(null));
- }
- /**
- * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
- */
- public function testIsAvailableEmptyQuote()
- {
- $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
- $availabilityValidator = $this->createMock(ValidatorInterface::class);
- $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
- $validationResult = $this->createMock(ResultInterface::class);
- $paymentInfo = $this->createMock(InfoInterface::class);
- $this->valueHandlerPool->expects(static::once())
- ->method('get')
- ->with('active')
- ->willReturn($activeValueHandler);
- $activeValueHandler->expects(static::once())
- ->method('handle')
- ->with(['field' => 'active', 'payment' => $paymentDO])
- ->willReturn(true);
- $this->validatorPool->expects(static::once())
- ->method('get')
- ->with('availability')
- ->willReturn($availabilityValidator);
- $this->paymentDataObjectFactory->expects(static::exactly(2))
- ->method('create')
- ->with($paymentInfo)
- ->willReturn($paymentDO);
- $availabilityValidator->expects(static::once())
- ->method('validate')
- ->willReturn($validationResult);
- $validationResult->expects(static::once())
- ->method('isValid')
- ->willReturn(true);
- $this->eventManager->expects(static::once())
- ->method('dispatch');
- $this->adapter->setInfoInstance($paymentInfo);
- static::assertTrue($this->adapter->isAvailable(null));
- }
- /**
- * @covers \Magento\Payment\Model\Method\Adapter::isAvailable
- */
- public function testIsAvailableWithEmptyInfoInstance()
- {
- $activeValueHandler = $this->createMock(ValueHandlerInterface::class);
- $this->valueHandlerPool->expects(static::once())
- ->method('get')
- ->with('active')
- ->willReturn($activeValueHandler);
- $activeValueHandler->expects(static::once())
- ->method('handle')
- ->with(['field' => 'active'])
- ->willReturn(true);
- $this->validatorPool->expects(static::never())
- ->method('get')
- ->with('availability');
- $this->eventManager->expects(static::once())
- ->method('dispatch');
- static::assertTrue($this->adapter->isAvailable(null));
- }
- public function testExecuteCommandWithCommandExecutor()
- {
- /** @var ManagerInterface|MockObject $eventManager */
- $eventManager = $this->createMock(
- ManagerInterface::class
- );
- /** @var ValueHandlerPoolInterface|MockObject $valueHandlerPool */
- $valueHandlerPool = $this->createMock(
- ValueHandlerPoolInterface::class
- );
- /** @var CommandManagerInterface|MockObject $commandManager */
- $commandManager = $this->createMock(
- CommandManagerInterface::class
- );
- /** @var PaymentDataObjectFactory|MockObject $paymentDataObjectFactory */
- $paymentDataObjectFactory = $this->getMockBuilder(
- PaymentDataObjectFactory::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $paymentInfo = $this->createMock(InfoInterface::class);
- $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
- $adapter = new Adapter(
- $eventManager,
- $valueHandlerPool,
- $paymentDataObjectFactory,
- 'CODE',
- '\FormBlock',
- '\InfoBlock',
- null,
- null,
- $commandManager,
- $this->logger
- );
- $valueHandler = $this->createMock(ValueHandlerInterface::class);
- $valueHandlerPool->expects(static::once())
- ->method('get')
- ->with('can_authorize')
- ->willReturn($valueHandler);
- $valueHandler->expects(static::once())
- ->method('handle')
- ->with(['field' => 'can_authorize'])
- ->willReturn(true);
- $paymentDataObjectFactory->expects(static::once())
- ->method('create')
- ->with($paymentInfo)
- ->willReturn($paymentDO);
- $commandManager->expects(static::once())
- ->method('executeByCode')
- ->with('authorize', $paymentInfo, ['amount' => 10, 'payment' => $paymentDO])
- ->willReturn(null);
- $adapter->authorize($paymentInfo, 10);
- }
- public function testExecuteCommandWithCommandPool()
- {
- /** @var ManagerInterface|MockObject $eventManager */
- $eventManager = $this->createMock(ManagerInterface::class);
- /** @var ValueHandlerPoolInterface|MockObject $valueHandlerPool */
- $valueHandlerPool = $this->createMock(ValueHandlerPoolInterface::class);
- /** @var CommandPoolInterface|MockObject $commandPool */
- $commandPool = $this->createMock(CommandPoolInterface::class);
- /** @var PaymentDataObjectFactory|MockObject $paymentDataObjectFactory */
- $paymentDataObjectFactory = $this->getMockBuilder(PaymentDataObjectFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $paymentInfo = $this->createMock(InfoInterface::class);
- $paymentDO = $this->createMock(PaymentDataObjectInterface::class);
- $adapter = new Adapter(
- $eventManager,
- $valueHandlerPool,
- $paymentDataObjectFactory,
- 'CODE',
- '\FormBlock',
- '\InfoBlock',
- $commandPool,
- null,
- null,
- $this->logger
- );
- $valueHandler = $this->createMock(ValueHandlerInterface::class);
- $command = $this->createMock(CommandInterface::class);
- $valueHandlerPool->expects(static::once())
- ->method('get')
- ->with('can_authorize')
- ->willReturn($valueHandler);
- $valueHandler->expects(static::once())
- ->method('handle')
- ->with(['field' => 'can_authorize'])
- ->willReturn(true);
- $paymentDataObjectFactory->expects(static::once())
- ->method('create')
- ->with($paymentInfo)
- ->willReturn($paymentDO);
- $commandPool->expects(static::once())
- ->method('get')
- ->with('authorize')
- ->willReturn($command);
- $command->expects(static::once())
- ->method('execute')
- ->with(['amount' => 10, 'payment' => $paymentDO])
- ->willReturn(null);
- $adapter->authorize($paymentInfo, 10);
- }
- }
|