Skip to content

Commit

Permalink
MAGETWO-54454: [GITHUB]#4867 Fix СommandExecutor interface mismatch i…
Browse files Browse the repository at this point in the history
…n Payment Adapter

- Covered two cases of command execution: with command executor and command pool
  • Loading branch information
dkvashninbay committed Jun 16, 2016
1 parent b6b99dd commit b1a4f80
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/code/Magento/Payment/Model/Method/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ private function executeCommand($commandCode, array $arguments = [])
return null;
}

/** @var InfoInterface|null $payment */
$payment = null;
if (isset($arguments['payment'])) {
if (isset($arguments['payment']) && $arguments['payment'] instanceof InfoInterface) {
$payment = $arguments['payment'];
$arguments['payment'] = $this->paymentDataObjectFactory->create($arguments['payment']);
}
Expand Down
134 changes: 134 additions & 0 deletions app/code/Magento/Payment/Test/Unit/Model/Method/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
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\ValidatorPoolInterface;
use Magento\Payment\Model\InfoInterface;
use Magento\Payment\Model\Method\Adapter;

class AdapterTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -158,4 +163,133 @@ public function testIsAvailableEmptyQuote()
$this->adapter->setInfoInstance($paymentInfo);
static::assertTrue($this->adapter->isAvailable(null));
}

public function testExecuteCommandWithCommandExecutor()
{
/** @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject $eventManager */
$eventManager = $this->getMock(
ManagerInterface::class
);

/** @var ValueHandlerPoolInterface|\PHPUnit_Framework_MockObject_MockObject $valueHandlerPool */
$valueHandlerPool = $this->getMock(
ValueHandlerPoolInterface::class
);

/** @var CommandManagerInterface|\PHPUnit_Framework_MockObject_MockObject $commandManager */
$commandManager = $this->getMock(
CommandManagerInterface::class
);

/** @var PaymentDataObjectFactory|\PHPUnit_Framework_MockObject_MockObject $paymentDataObjectFactory */
$paymentDataObjectFactory = $this->getMockBuilder(
PaymentDataObjectFactory::class
)
->disableOriginalConstructor()
->getMock();

$paymentInfo = $this->getMock(InfoInterface::class);
$paymentDO = $this->getMock(PaymentDataObjectInterface::class);

$adapter = new Adapter(
$eventManager,
$valueHandlerPool,
$paymentDataObjectFactory,
'CODE',
'\FormBlock',
'\InfoBlock',
null,
null,
$commandManager
);

$valueHandler = $this->getMock(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|\PHPUnit_Framework_MockObject_MockObject $eventManager */
$eventManager = $this->getMock(
ManagerInterface::class
);

/** @var ValueHandlerPoolInterface|\PHPUnit_Framework_MockObject_MockObject $valueHandlerPool */
$valueHandlerPool = $this->getMock(
ValueHandlerPoolInterface::class
);

/** @var CommandPoolInterface|\PHPUnit_Framework_MockObject_MockObject $commandPool */
$commandPool = $this->getMock(
CommandPoolInterface::class
);

/** @var PaymentDataObjectFactory|\PHPUnit_Framework_MockObject_MockObject $paymentDataObjectFactory */
$paymentDataObjectFactory = $this->getMockBuilder(
PaymentDataObjectFactory::class
)
->disableOriginalConstructor()
->getMock();

$paymentInfo = $this->getMock(InfoInterface::class);
$paymentDO = $this->getMock(PaymentDataObjectInterface::class);

$adapter = new Adapter(
$eventManager,
$valueHandlerPool,
$paymentDataObjectFactory,
'CODE',
'\FormBlock',
'\InfoBlock',
$commandPool
);

$valueHandler = $this->getMock(ValueHandlerInterface::class);
$command = $this->getMock(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);
}
}

0 comments on commit b1a4f80

Please sign in to comment.