Skip to content

Commit a2bb6fc

Browse files
committed
Merge branch 'MAGETWO-58997' into MPI-PR-bugfixes
2 parents 78bee8b + 47eb7b2 commit a2bb6fc

File tree

10 files changed

+322
-209
lines changed

10 files changed

+322
-209
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Vault\Api;
7+
8+
use Magento\Vault\Model\VaultPaymentInterface;
9+
10+
/**
11+
* Contains methods to retrieve vault payment methods
12+
* This interface is consistent with \Magento\Payment\Api\PaymentMethodListInterface
13+
* @api
14+
*/
15+
interface PaymentMethodListInterface
16+
{
17+
/**
18+
* Get list of available vault payments
19+
* @param int $storeId
20+
* @return VaultPaymentInterface[]
21+
*/
22+
public function getList($storeId);
23+
24+
/**
25+
* Get list of enabled in the configuration vault payments
26+
* @param int $storeId
27+
* @return VaultPaymentInterface[]
28+
*/
29+
public function getActiveList($storeId);
30+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Vault\Model;
7+
8+
use Magento\Payment\Api\Data\PaymentMethodInterface;
9+
use Magento\Payment\Api\PaymentMethodListInterface;
10+
use Magento\Payment\Model\Method\InstanceFactory;
11+
use Magento\Payment\Model\MethodInterface;
12+
use Magento\Vault\Api\PaymentMethodListInterface as VaultPaymentMethodListInterface;
13+
14+
/**
15+
* Contains methods to retrieve configured vault payments
16+
*/
17+
class PaymentMethodList implements VaultPaymentMethodListInterface
18+
{
19+
/**
20+
* @var InstanceFactory
21+
*/
22+
private $instanceFactory;
23+
24+
/**
25+
* @var PaymentMethodListInterface
26+
*/
27+
private $paymentMethodList;
28+
29+
/**
30+
* PaymentMethodList constructor.
31+
* @param PaymentMethodListInterface $paymentMethodList
32+
* @param InstanceFactory $instanceFactory
33+
*/
34+
public function __construct(PaymentMethodListInterface $paymentMethodList, InstanceFactory $instanceFactory)
35+
{
36+
$this->instanceFactory = $instanceFactory;
37+
$this->paymentMethodList = $paymentMethodList;
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getList($storeId)
44+
{
45+
return $this->filterList($this->paymentMethodList->getList($storeId));
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function getActiveList($storeId)
52+
{
53+
return $this->filterList($this->paymentMethodList->getActiveList($storeId));
54+
}
55+
56+
/**
57+
* Filter vault methods from payments
58+
* @param PaymentMethodInterface[] $list
59+
* @return VaultPaymentInterface[]
60+
*/
61+
private function filterList(array $list)
62+
{
63+
$paymentMethods = array_map(
64+
function (PaymentMethodInterface $paymentMethod) {
65+
return $this->instanceFactory->create($paymentMethod);
66+
},
67+
$list
68+
);
69+
70+
$availableMethods = array_filter(
71+
$paymentMethods,
72+
function (MethodInterface $methodInstance) {
73+
return $methodInstance instanceof VaultPaymentInterface;
74+
}
75+
);
76+
return array_values($availableMethods);
77+
}
78+
}

app/code/Magento/Vault/Model/Ui/TokensConfigProvider.php

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
use Magento\Checkout\Model\ConfigProviderInterface;
99
use Magento\Framework\App\ObjectManager;
10-
use Magento\Payment\Api\Data\PaymentMethodInterface;
1110
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Vault\Api\PaymentMethodListInterface;
1212
use Magento\Vault\Model\CustomerTokenManagement;
13-
use Magento\Vault\Model\VaultPaymentInterface;
1413

1514
/**
1615
* Class ConfigProvider
@@ -39,14 +38,9 @@ final class TokensConfigProvider implements ConfigProviderInterface
3938
private $customerTokenManagement;
4039

4140
/**
42-
* @var \Magento\Payment\Api\PaymentMethodListInterface
41+
* @var PaymentMethodListInterface
4342
*/
44-
private $paymentMethodList;
45-
46-
/**
47-
* @var \Magento\Payment\Model\Method\InstanceFactory
48-
*/
49-
private $paymentMethodInstanceFactory;
43+
private $vaultPaymentList;
5044

5145
/**
5246
* Constructor
@@ -112,7 +106,8 @@ public function getConfig()
112106
private function getComponentProviders()
113107
{
114108
$providers = [];
115-
$vaultPaymentMethods = $this->getVaultPaymentMethodList();
109+
$storeId = $this->storeManager->getStore()->getId();
110+
$vaultPaymentMethods = $this->getVaultPaymentList()->getActiveList($storeId);
116111

117112
foreach ($vaultPaymentMethods as $method) {
118113
$providerCode = $method->getProviderCode();
@@ -141,60 +136,15 @@ private function getComponentProvider($vaultProviderCode)
141136
}
142137

143138
/**
144-
* Get list of active Vault payment methods.
145-
*
146-
* @return VaultPaymentInterface[]
147-
*/
148-
private function getVaultPaymentMethodList()
149-
{
150-
$storeId = $this->storeManager->getStore()->getId();
151-
152-
$paymentMethods = array_map(
153-
function (PaymentMethodInterface $paymentMethod) {
154-
return $this->getPaymentMethodInstanceFactory()->create($paymentMethod);
155-
},
156-
$this->getPaymentMethodList()->getActiveList($storeId)
157-
);
158-
159-
$availableMethods = array_filter(
160-
$paymentMethods,
161-
function (\Magento\Payment\Model\MethodInterface $methodInstance) {
162-
return $methodInstance instanceof VaultPaymentInterface;
163-
}
164-
);
165-
166-
return $availableMethods;
167-
}
168-
169-
/**
170-
* Get payment method list.
171-
*
172-
* @return \Magento\Payment\Api\PaymentMethodListInterface
173-
* @deprecated
174-
*/
175-
private function getPaymentMethodList()
176-
{
177-
if ($this->paymentMethodList === null) {
178-
$this->paymentMethodList = ObjectManager::getInstance()->get(
179-
\Magento\Payment\Api\PaymentMethodListInterface::class
180-
);
181-
}
182-
return $this->paymentMethodList;
183-
}
184-
185-
/**
186-
* Get payment method instance factory.
187-
*
188-
* @return \Magento\Payment\Model\Method\InstanceFactory
139+
* Get instance of vault payment list instance
140+
* @return PaymentMethodListInterface
189141
* @deprecated
190142
*/
191-
private function getPaymentMethodInstanceFactory()
143+
private function getVaultPaymentList()
192144
{
193-
if ($this->paymentMethodInstanceFactory === null) {
194-
$this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
195-
\Magento\Payment\Model\Method\InstanceFactory::class
196-
);
145+
if ($this->vaultPaymentList === null) {
146+
$this->vaultPaymentList = ObjectManager::getInstance()->get(PaymentMethodListInterface::class);
197147
}
198-
return $this->paymentMethodInstanceFactory;
148+
return $this->vaultPaymentList;
199149
}
200150
}

app/code/Magento/Vault/Model/Ui/VaultConfigProvider.php

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use Magento\Checkout\Model\ConfigProviderInterface;
99
use Magento\Framework\App\ObjectManager;
1010
use Magento\Framework\Session\SessionManagerInterface;
11-
use Magento\Payment\Api\Data\PaymentMethodInterface;
1211
use Magento\Store\Model\StoreManagerInterface;
13-
use Magento\Vault\Model\VaultPaymentInterface;
12+
use Magento\Vault\Api\PaymentMethodListInterface;
1413

1514
class VaultConfigProvider implements ConfigProviderInterface
1615
{
@@ -32,14 +31,9 @@ class VaultConfigProvider implements ConfigProviderInterface
3231
private $session;
3332

3433
/**
35-
* @var \Magento\Payment\Api\PaymentMethodListInterface
34+
* @var PaymentMethodListInterface
3635
*/
37-
private $paymentMethodList;
38-
39-
/**
40-
* @var \Magento\Payment\Model\Method\InstanceFactory
41-
*/
42-
private $paymentMethodInstanceFactory;
36+
private $vaultPaymentList;
4337

4438
/**
4539
* VaultConfigProvider constructor.
@@ -62,9 +56,9 @@ public function __construct(
6256
public function getConfig()
6357
{
6458
$availableMethods = [];
65-
$vaultPayments = $this->getVaultPaymentMethodList();
66-
$customerId = $this->session->getCustomerId();
6759
$storeId = $this->storeManager->getStore()->getId();
60+
$vaultPayments = $this->getVaultPaymentList()->getActiveList($storeId);
61+
$customerId = $this->session->getCustomerId();
6862

6963
foreach ($vaultPayments as $method) {
7064
$availableMethods[$method->getCode()] = [
@@ -78,60 +72,15 @@ public function getConfig()
7872
}
7973

8074
/**
81-
* Get list of active Vault payment methods.
82-
*
83-
* @return VaultPaymentInterface[]
84-
*/
85-
private function getVaultPaymentMethodList()
86-
{
87-
$storeId = $this->storeManager->getStore()->getId();
88-
89-
$paymentMethods = array_map(
90-
function (PaymentMethodInterface $paymentMethod) {
91-
return $this->getPaymentMethodInstanceFactory()->create($paymentMethod);
92-
},
93-
$this->getPaymentMethodList()->getActiveList($storeId)
94-
);
95-
96-
$availableMethods = array_filter(
97-
$paymentMethods,
98-
function (\Magento\Payment\Model\MethodInterface $methodInstance) {
99-
return $methodInstance instanceof VaultPaymentInterface;
100-
}
101-
);
102-
103-
return $availableMethods;
104-
}
105-
106-
/**
107-
* Get payment method list.
108-
*
109-
* @return \Magento\Payment\Api\PaymentMethodListInterface
110-
* @deprecated
111-
*/
112-
private function getPaymentMethodList()
113-
{
114-
if ($this->paymentMethodList === null) {
115-
$this->paymentMethodList = ObjectManager::getInstance()->get(
116-
\Magento\Payment\Api\PaymentMethodListInterface::class
117-
);
118-
}
119-
return $this->paymentMethodList;
120-
}
121-
122-
/**
123-
* Get payment method instance factory.
124-
*
125-
* @return \Magento\Payment\Model\Method\InstanceFactory
75+
* Get vault payment list instance
76+
* @return PaymentMethodListInterface
12677
* @deprecated
12778
*/
128-
private function getPaymentMethodInstanceFactory()
79+
private function getVaultPaymentList()
12980
{
130-
if ($this->paymentMethodInstanceFactory === null) {
131-
$this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
132-
\Magento\Payment\Model\Method\InstanceFactory::class
133-
);
81+
if ($this->vaultPaymentList === null) {
82+
$this->vaultPaymentList = ObjectManager::getInstance()->get(PaymentMethodListInterface::class);
13483
}
135-
return $this->paymentMethodInstanceFactory;
84+
return $this->vaultPaymentList;
13685
}
13786
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Vault\Test\Unit\Model;
7+
8+
use Magento\Payment\Api\Data\PaymentMethodInterface;
9+
use Magento\Payment\Api\PaymentMethodListInterface;
10+
use Magento\Payment\Model\Method\InstanceFactory;
11+
use Magento\Payment\Model\MethodInterface;
12+
use Magento\Vault\Model\VaultPaymentInterface;
13+
use Magento\Vault\Model\PaymentMethodList;
14+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
15+
16+
class PaymentMethodListTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var PaymentMethodListInterface|MockObject
20+
*/
21+
private $paymentMethodList;
22+
23+
/**
24+
* @var InstanceFactory|MockObject
25+
*/
26+
private $instanceFactory;
27+
28+
/**
29+
* @var PaymentMethodList
30+
*/
31+
private $vaultPaymentList;
32+
33+
protected function setUp()
34+
{
35+
$this->paymentMethodList = $this->getMock(PaymentMethodListInterface::class);
36+
$this->instanceFactory = $this->getMockBuilder(InstanceFactory::class)
37+
->disableOriginalConstructor()
38+
->setMethods(['create'])
39+
->getMock();
40+
41+
$this->vaultPaymentList = new PaymentMethodList($this->paymentMethodList, $this->instanceFactory);
42+
}
43+
44+
/**
45+
* @covers \Magento\Vault\Model\PaymentMethodList::getActiveList
46+
*/
47+
public function testGetActivePaymentList()
48+
{
49+
$storeId = 1;
50+
$vaultPayment = $this->getMock(VaultPaymentInterface::class);
51+
$paymentMethodInterface1 = $this->getMock(PaymentMethodInterface::class);
52+
$paymentMethodInterface2 = $this->getMock(PaymentMethodInterface::class);
53+
$activePayments = [
54+
$paymentMethodInterface1,
55+
$paymentMethodInterface2
56+
];
57+
58+
$this->paymentMethodList->expects(static::once())
59+
->method('getActiveList')
60+
->with($storeId)
61+
->willReturn($activePayments);
62+
63+
$this->instanceFactory->expects(static::exactly(2))
64+
->method('create')
65+
->willReturnMap([
66+
[$paymentMethodInterface1, $this->getMock(MethodInterface::class)],
67+
[$paymentMethodInterface2, $vaultPayment]
68+
]);
69+
70+
$vaultPayments = $this->vaultPaymentList->getActiveList($storeId);
71+
static::assertCount(1, $vaultPayments);
72+
static::assertInstanceOf(VaultPaymentInterface::class, $vaultPayment);
73+
}
74+
}

0 commit comments

Comments
 (0)