Skip to content

Commit b1a4f80

Browse files
committed
MAGETWO-54454: [GITHUB]#4867 Fix СommandExecutor interface mismatch in Payment Adapter
- Covered two cases of command execution: with command executor and command pool
1 parent b6b99dd commit b1a4f80

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

app/code/Magento/Payment/Model/Method/Adapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,9 @@ private function executeCommand($commandCode, array $arguments = [])
507507
return null;
508508
}
509509

510+
/** @var InfoInterface|null $payment */
510511
$payment = null;
511-
if (isset($arguments['payment'])) {
512+
if (isset($arguments['payment']) && $arguments['payment'] instanceof InfoInterface) {
512513
$payment = $arguments['payment'];
513514
$arguments['payment'] = $this->paymentDataObjectFactory->create($arguments['payment']);
514515
}

app/code/Magento/Payment/Test/Unit/Model/Method/AdapterTest.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
namespace Magento\Payment\Test\Unit\Model\Method;
77

88
use Magento\Framework\Event\ManagerInterface;
9+
use Magento\Payment\Gateway\Command\CommandManagerInterface;
910
use Magento\Payment\Gateway\Command\CommandPoolInterface;
11+
use Magento\Payment\Gateway\CommandInterface;
12+
use Magento\Payment\Gateway\Config\ValueHandlerInterface;
1013
use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
1114
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
15+
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
1216
use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
17+
use Magento\Payment\Model\InfoInterface;
1318
use Magento\Payment\Model\Method\Adapter;
1419

1520
class AdapterTest extends \PHPUnit_Framework_TestCase
@@ -158,4 +163,133 @@ public function testIsAvailableEmptyQuote()
158163
$this->adapter->setInfoInstance($paymentInfo);
159164
static::assertTrue($this->adapter->isAvailable(null));
160165
}
166+
167+
public function testExecuteCommandWithCommandExecutor()
168+
{
169+
/** @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject $eventManager */
170+
$eventManager = $this->getMock(
171+
ManagerInterface::class
172+
);
173+
174+
/** @var ValueHandlerPoolInterface|\PHPUnit_Framework_MockObject_MockObject $valueHandlerPool */
175+
$valueHandlerPool = $this->getMock(
176+
ValueHandlerPoolInterface::class
177+
);
178+
179+
/** @var CommandManagerInterface|\PHPUnit_Framework_MockObject_MockObject $commandManager */
180+
$commandManager = $this->getMock(
181+
CommandManagerInterface::class
182+
);
183+
184+
/** @var PaymentDataObjectFactory|\PHPUnit_Framework_MockObject_MockObject $paymentDataObjectFactory */
185+
$paymentDataObjectFactory = $this->getMockBuilder(
186+
PaymentDataObjectFactory::class
187+
)
188+
->disableOriginalConstructor()
189+
->getMock();
190+
191+
$paymentInfo = $this->getMock(InfoInterface::class);
192+
$paymentDO = $this->getMock(PaymentDataObjectInterface::class);
193+
194+
$adapter = new Adapter(
195+
$eventManager,
196+
$valueHandlerPool,
197+
$paymentDataObjectFactory,
198+
'CODE',
199+
'\FormBlock',
200+
'\InfoBlock',
201+
null,
202+
null,
203+
$commandManager
204+
);
205+
206+
$valueHandler = $this->getMock(ValueHandlerInterface::class);
207+
208+
$valueHandlerPool->expects(static::once())
209+
->method('get')
210+
->with('can_authorize')
211+
->willReturn($valueHandler);
212+
$valueHandler->expects(static::once())
213+
->method('handle')
214+
->with(['field' => 'can_authorize'])
215+
->willReturn(true);
216+
217+
$paymentDataObjectFactory->expects(static::once())
218+
->method('create')
219+
->with($paymentInfo)
220+
->willReturn($paymentDO);
221+
222+
$commandManager->expects(static::once())
223+
->method('executeByCode')
224+
->with('authorize', $paymentInfo, ['amount' => 10, 'payment' => $paymentDO])
225+
->willReturn(null);
226+
227+
$adapter->authorize($paymentInfo, 10);
228+
}
229+
230+
public function testExecuteCommandWithCommandPool()
231+
{
232+
/** @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject $eventManager */
233+
$eventManager = $this->getMock(
234+
ManagerInterface::class
235+
);
236+
237+
/** @var ValueHandlerPoolInterface|\PHPUnit_Framework_MockObject_MockObject $valueHandlerPool */
238+
$valueHandlerPool = $this->getMock(
239+
ValueHandlerPoolInterface::class
240+
);
241+
242+
/** @var CommandPoolInterface|\PHPUnit_Framework_MockObject_MockObject $commandPool */
243+
$commandPool = $this->getMock(
244+
CommandPoolInterface::class
245+
);
246+
247+
/** @var PaymentDataObjectFactory|\PHPUnit_Framework_MockObject_MockObject $paymentDataObjectFactory */
248+
$paymentDataObjectFactory = $this->getMockBuilder(
249+
PaymentDataObjectFactory::class
250+
)
251+
->disableOriginalConstructor()
252+
->getMock();
253+
254+
$paymentInfo = $this->getMock(InfoInterface::class);
255+
$paymentDO = $this->getMock(PaymentDataObjectInterface::class);
256+
257+
$adapter = new Adapter(
258+
$eventManager,
259+
$valueHandlerPool,
260+
$paymentDataObjectFactory,
261+
'CODE',
262+
'\FormBlock',
263+
'\InfoBlock',
264+
$commandPool
265+
);
266+
267+
$valueHandler = $this->getMock(ValueHandlerInterface::class);
268+
$command = $this->getMock(CommandInterface::class);
269+
270+
$valueHandlerPool->expects(static::once())
271+
->method('get')
272+
->with('can_authorize')
273+
->willReturn($valueHandler);
274+
$valueHandler->expects(static::once())
275+
->method('handle')
276+
->with(['field' => 'can_authorize'])
277+
->willReturn(true);
278+
279+
$paymentDataObjectFactory->expects(static::once())
280+
->method('create')
281+
->with($paymentInfo)
282+
->willReturn($paymentDO);
283+
284+
$commandPool->expects(static::once())
285+
->method('get')
286+
->with('authorize')
287+
->willReturn($command);
288+
$command->expects(static::once())
289+
->method('execute')
290+
->with(['amount' => 10, 'payment' => $paymentDO])
291+
->willReturn(null);
292+
293+
$adapter->authorize($paymentInfo, 10);
294+
}
161295
}

0 commit comments

Comments
 (0)