Skip to content

Commit 6e32886

Browse files
committed
MAGETWO-32016: Payment API for the Payments in US and CA
- create tests and implementation of logic according stubs
1 parent dcbdbf6 commit 6e32886

File tree

3 files changed

+154
-10
lines changed

3 files changed

+154
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,6 @@ abstract class AbstractMethod extends \Magento\Framework\Model\AbstractExtensibl
207207
*/
208208
protected $_scopeConfig;
209209

210-
/**
211-
* Core event manager proxy
212-
*
213-
* @var \Magento\Framework\Event\ManagerInterface
214-
*/
215-
protected $_eventManager;
216-
217210
/**
218211
* @var \Psr\Log\LoggerInterface
219212
*/
@@ -252,7 +245,6 @@ public function __construct(
252245
);
253246
$this->_paymentData = $paymentData;
254247
$this->_scopeConfig = $scopeConfig;
255-
$this->_eventManager = $context->getEventDispatcher();
256248
$this->logger = $context->getLogger();
257249
$this->initializeData($data);
258250
}
@@ -830,18 +822,33 @@ public function prepareSave()
830822
public function isAvailable($quote = null)
831823
{
832824
$checkResult = new \StdClass();
833-
$isActive = (bool)(int)$this->getConfigData('active', $quote ? $quote->getStoreId() : null);
825+
$isActive = $this->isActive($quote ? $quote->getStoreId() : null);
834826
$checkResult->isAvailable = $isActive;
835827
$checkResult->isDeniedInConfig = !$isActive;
836828
// for future use in observers
837829
$this->_eventManager->dispatch(
838830
'payment_method_is_active',
839-
['result' => $checkResult, 'method_instance' => $this, 'quote' => $quote]
831+
[
832+
'result' => $checkResult,
833+
'method_instance' => $this,
834+
'quote' => $quote
835+
]
840836
);
841837

842838
return $checkResult->isAvailable;
843839
}
844840

841+
/**
842+
* Is active
843+
*
844+
* @param int|null $storeId
845+
* @return bool
846+
*/
847+
public function isActive($storeId = null)
848+
{
849+
return (bool)(int)$this->getConfigData('active', $storeId);
850+
}
851+
845852
/**
846853
* Method that will be executed instead of authorize or capture
847854
* if flag isInitializeNeeded set to true
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Payment\Test\Unit\Model\Method\AbstractMethod;
7+
8+
/**
9+
* Class Stub
10+
*
11+
* Stub for \Magento\Payment\Model\Method\AbstractMethod
12+
*/
13+
class Stub extends \Magento\Payment\Model\Method\AbstractMethod
14+
{
15+
const STUB_CODE = 'stub-code';
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getCode()
21+
{
22+
return static::STUB_CODE;
23+
}
24+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Payment\Test\Unit\Model\Method;
7+
8+
use Magento\Store\Model\ScopeInterface;
9+
use Magento\Payment\Test\Unit\Model\Method\AbstractMethod\Stub;
10+
11+
/**
12+
* Class AbstractMethodTest
13+
*
14+
* Test for class \Magento\Payment\Model\Method\AbstractMethod
15+
*/
16+
class AbstractMethodTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var \Magento\Payment\Model\Method\AbstractMethod
20+
*/
21+
protected $payment;
22+
23+
/**
24+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $scopeConfigMock;
27+
28+
/**
29+
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $eventManagerMock;
32+
33+
/**
34+
* @var \Magento\Quote\Api\Data\CartInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $quoteMock;
37+
38+
protected function setUp()
39+
{
40+
$this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
41+
->setMethods(['getValue'])
42+
->getMockForAbstractClass();
43+
$this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
44+
->setMethods(['dispatch'])
45+
->getMockForAbstractClass();
46+
$this->quoteMock = $this->getMockBuilder('Magento\Quote\Api\Data\CartInterface')
47+
->setMethods(['getStoreId'])
48+
->getMockForAbstractClass();
49+
$contextMock = $this->getMockBuilder('Magento\Framework\Model\Context')
50+
->disableOriginalConstructor()
51+
->setMethods(['getEventDispatcher'])
52+
->getMock();
53+
54+
$contextMock->expects($this->once())
55+
->method('getEventDispatcher')
56+
->willReturn($this->eventManagerMock);
57+
58+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59+
60+
$this->payment = $helper->getObject(
61+
'Magento\Payment\Test\Unit\Model\Method\AbstractMethod\Stub',
62+
[
63+
'scopeConfig' => $this->scopeConfigMock,
64+
'context' => $contextMock
65+
]
66+
);
67+
}
68+
69+
/**
70+
* @param bool $result
71+
*
72+
* @dataProvider dataProviderForTestIsAvailable
73+
*/
74+
public function testIsAvailable($result)
75+
{
76+
$storeId = 15;
77+
$this->quoteMock->expects($this->once())
78+
->method('getStoreId')
79+
->willReturn($storeId);
80+
81+
$this->scopeConfigMock->expects($this->once())
82+
->method('getValue')
83+
->with(
84+
'payment/' . Stub::STUB_CODE . '/active',
85+
ScopeInterface::SCOPE_STORE,
86+
$storeId
87+
)->willReturn($result);
88+
89+
$this->eventManagerMock->expects($this->once())
90+
->method('dispatch')
91+
->with(
92+
$this->equalTo('payment_method_is_active'),
93+
$this->countOf(3)
94+
);
95+
96+
$this->assertEquals($result, $this->payment->isAvailable($this->quoteMock));
97+
}
98+
99+
/**
100+
* @return array
101+
*/
102+
public function dataProviderForTestIsAvailable()
103+
{
104+
return [
105+
[
106+
'result' => true
107+
],
108+
[
109+
'result' => false
110+
],
111+
];
112+
}
113+
}

0 commit comments

Comments
 (0)