Skip to content

Commit 7c026f4

Browse files
authored
Update ConfigProvider.php
1 parent fb31ed9 commit 7c026f4

File tree

1 file changed

+71
-170
lines changed

1 file changed

+71
-170
lines changed

app/code/Magento/Braintree/Model/Ui/ConfigProvider.php

Lines changed: 71 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -3,215 +3,116 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
declare(strict_types=1);
7-
8-
namespace Magento\Braintree\Test\Unit\Model\Ui;
6+
namespace Magento\Braintree\Model\Ui;
97

108
use Magento\Braintree\Gateway\Config\Config;
11-
use Magento\Braintree\Model\Adapter\BraintreeAdapter;
9+
use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
1210
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
13-
use Magento\Braintree\Model\Ui\ConfigProvider;
14-
use Magento\Customer\Model\Session;
15-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
11+
use Magento\Checkout\Model\ConfigProviderInterface;
12+
use Magento\Framework\Session\SessionManagerInterface;
1613

1714
/**
18-
* Class ConfigProviderTest
15+
* Class ConfigProvider
1916
*
20-
* Test for class \Magento\Braintree\Model\Ui\ConfigProvider
17+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2118
*/
22-
class ConfigProviderTest extends \PHPUnit\Framework\TestCase
19+
class ConfigProvider implements ConfigProviderInterface
2320
{
24-
const SDK_URL = 'https://js.braintreegateway.com/v2/braintree.js';
25-
const CLIENT_TOKEN = 'token';
26-
const MERCHANT_ACCOUNT_ID = '245345';
21+
const CODE = 'braintree';
22+
23+
const CC_VAULT_CODE = 'braintree_cc_vault';
2724

2825
/**
29-
* @var Config|MockObject
26+
* @var Config
3027
*/
3128
private $config;
3229

3330
/**
34-
* @var BraintreeAdapter|MockObject
31+
* @var BraintreeAdapterFactory
3532
*/
36-
private $braintreeAdapter;
33+
private $adapterFactory;
3734

3835
/**
39-
* @var Session|MockObject
36+
* @var string
4037
*/
41-
private $session;
38+
private $clientToken = '';
4239

4340
/**
44-
* @var ConfigProvider
41+
* @var SessionManagerInterface
4542
*/
46-
private $configProvider;
47-
48-
protected function setUp()
49-
{
50-
$this->config = $this->getMockBuilder(Config::class)
51-
->disableOriginalConstructor()
52-
->getMock();
53-
54-
$this->braintreeAdapter = $this->getMockBuilder(BraintreeAdapter::class)
55-
->disableOriginalConstructor()
56-
->getMock();
57-
/** @var BraintreeAdapterFactory|MockObject $adapterFactoryMock */
58-
$adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
59-
->disableOriginalConstructor()
60-
->getMock();
61-
$adapterFactoryMock->method('create')
62-
->willReturn($this->braintreeAdapter);
63-
64-
$this->session = $this->getMockBuilder(Session::class)
65-
->disableOriginalConstructor()
66-
->setMethods(['getStoreId'])
67-
->getMock();
68-
$this->session->method('getStoreId')
69-
->willReturn(null);
70-
71-
$this->configProvider = new ConfigProvider(
72-
$this->config,
73-
$adapterFactoryMock,
74-
$this->session
75-
);
76-
}
43+
private $session;
7744

7845
/**
79-
* Ensure that get config returns correct data if payment is active or not
46+
* Constructor
8047
*
81-
* @param array $config
82-
* @param array $expected
83-
* @dataProvider getConfigDataProvider
84-
*/
85-
public function testGetConfig($config, $expected)
86-
{
87-
if ($config['isActive']) {
88-
$this->braintreeAdapter->expects($this->once())
89-
->method('generate')
90-
->willReturn(self::CLIENT_TOKEN);
91-
} else {
92-
$config = array_replace_recursive(
93-
$this->getConfigDataProvider()[0]['config'],
94-
$config
95-
);
96-
$expected = array_replace_recursive(
97-
$this->getConfigDataProvider()[0]['expected'],
98-
$expected
99-
);
100-
$this->braintreeAdapter->expects($this->never())
101-
->method('generate');
102-
}
103-
104-
foreach ($config as $method => $value) {
105-
$this->config->expects($this->once())
106-
->method($method)
107-
->willReturn($value);
108-
}
109-
110-
$this->assertEquals($expected, $this->configProvider->getConfig());
111-
}
112-
113-
/**
114-
* @covers \Magento\Braintree\Model\Ui\ConfigProvider::getClientToken
115-
* @dataProvider getClientTokenDataProvider
116-
* @param $merchantAccountId
117-
* @param $params
48+
* @param Config $config
49+
* @param BraintreeAdapterFactory $adapterFactory
50+
* @param SessionManagerInterface $session
11851
*/
119-
public function testGetClientToken($merchantAccountId, $params)
120-
{
121-
$this->config->expects(static::once())
122-
->method('getMerchantAccountId')
123-
->willReturn($merchantAccountId);
124-
125-
$this->braintreeAdapter->expects(static::once())
126-
->method('generate')
127-
->with($params)
128-
->willReturn(self::CLIENT_TOKEN);
129-
130-
static::assertEquals(self::CLIENT_TOKEN, $this->configProvider->getClientToken());
52+
public function __construct(
53+
Config $config,
54+
BraintreeAdapterFactory $adapterFactory,
55+
SessionManagerInterface $session
56+
) {
57+
$this->config = $config;
58+
$this->adapterFactory = $adapterFactory;
59+
$this->session = $session;
13160
}
13261

13362
/**
63+
* Retrieve assoc array of checkout configuration
64+
*
13465
* @return array
13566
*/
136-
public function getConfigDataProvider()
67+
public function getConfig()
13768
{
69+
$storeId = $this->session->getStoreId();
70+
$isActive = $this->config->isActive($storeId);
13871
return [
139-
[
140-
'config' => [
141-
'isActive' => true,
142-
'getCcTypesMapper' => ['visa' => 'VI', 'american-express' => 'AE'],
143-
'getSdkUrl' => self::SDK_URL,
144-
'getHostedFieldsSdkUrl' => 'https://sdk.com/test.js',
145-
'getCountrySpecificCardTypeConfig' => [
146-
'GB' => ['VI', 'AE'],
147-
'US' => ['DI', 'JCB']
148-
],
149-
'getAvailableCardTypes' => ['AE', 'VI', 'MC', 'DI', 'JCB'],
150-
'isCvvEnabled' => true,
151-
'isVerify3DSecure' => true,
152-
'getThresholdAmount' => 20,
153-
'get3DSecureSpecificCountries' => ['GB', 'US', 'CA'],
154-
'getEnvironment' => 'test-environment',
155-
'getMerchantId' => 'test-merchant-id',
156-
'hasFraudProtection' => true,
72+
'payment' => [
73+
self::CODE => [
74+
'isActive' => $isActive,
75+
'clientToken' => $isActive ? $this->getClientToken() : null,
76+
'ccTypesMapper' => $this->config->getCcTypesMapper(),
77+
'sdkUrl' => $this->config->getSdkUrl(),
78+
'hostedFieldsSdkUrl' => $this->config->getHostedFieldsSdkUrl(),
79+
'countrySpecificCardTypes' => $this->config->getCountrySpecificCardTypeConfig($storeId),
80+
'availableCardTypes' => $this->config->getAvailableCardTypes($storeId),
81+
'useCvv' => $this->config->isCvvEnabled($storeId),
82+
'environment' => $this->config->getEnvironment($storeId),
83+
'hasFraudProtection' => $this->config->hasFraudProtection($storeId),
84+
'merchantId' => $this->config->getMerchantId($storeId),
85+
'ccVaultCode' => self::CC_VAULT_CODE,
15786
],
158-
'expected' => [
159-
'payment' => [
160-
ConfigProvider::CODE => [
161-
'isActive' => true,
162-
'clientToken' => self::CLIENT_TOKEN,
163-
'ccTypesMapper' => ['visa' => 'VI', 'american-express' => 'AE'],
164-
'sdkUrl' => self::SDK_URL,
165-
'hostedFieldsSdkUrl' => 'https://sdk.com/test.js',
166-
'countrySpecificCardTypes' => [
167-
'GB' => ['VI', 'AE'],
168-
'US' => ['DI', 'JCB']
169-
],
170-
'availableCardTypes' => ['AE', 'VI', 'MC', 'DI', 'JCB'],
171-
'useCvv' => true,
172-
'environment' => 'test-environment',
173-
'merchantId' => 'test-merchant-id',
174-
'hasFraudProtection' => true,
175-
'ccVaultCode' => ConfigProvider::CC_VAULT_CODE
176-
],
177-
Config::CODE_3DSECURE => [
178-
'enabled' => true,
179-
'thresholdAmount' => 20,
180-
'specificCountries' => ['GB', 'US', 'CA']
181-
]
182-
]
183-
]
184-
],
185-
[
186-
'config' => [
187-
'isActive' => false,
87+
Config::CODE_3DSECURE => [
88+
'enabled' => $this->config->isVerify3DSecure($storeId),
89+
'thresholdAmount' => $this->config->getThresholdAmount($storeId),
90+
'specificCountries' => $this->config->get3DSecureSpecificCountries($storeId),
18891
],
189-
'expected' => [
190-
'payment' => [
191-
ConfigProvider::CODE => [
192-
'isActive' => false,
193-
'clientToken' => null,
194-
]
195-
]
196-
]
197-
]
92+
],
19893
];
19994
}
20095

20196
/**
202-
* @return array
97+
* Generate a new client token if necessary
98+
*
99+
* @return string
203100
*/
204-
public function getClientTokenDataProvider()
101+
public function getClientToken()
205102
{
206-
return [
207-
[
208-
'merchantAccountId' => '',
209-
'params' => []
210-
],
211-
[
212-
'merchantAccountId' => self::MERCHANT_ACCOUNT_ID,
213-
'params' => ['merchantAccountId' => self::MERCHANT_ACCOUNT_ID]
214-
]
215-
];
103+
if (empty($this->clientToken)) {
104+
$params = [];
105+
106+
$storeId = $this->session->getStoreId();
107+
$merchantAccountId = $this->config->getMerchantAccountId($storeId);
108+
if (!empty($merchantAccountId)) {
109+
$params[PaymentDataBuilder::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
110+
}
111+
112+
$this->clientToken = $this->adapterFactory->create($storeId)
113+
->generate($params);
114+
}
115+
116+
return $this->clientToken;
216117
}
217118
}

0 commit comments

Comments
 (0)