Skip to content

Commit fb31ed9

Browse files
authored
Cover changes with unit test
Ensure that token not generated when payment is not active
1 parent 0fe0cdc commit fb31ed9

File tree

1 file changed

+170
-71
lines changed

1 file changed

+170
-71
lines changed

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

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

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

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

2528
/**
26-
* @var Config
29+
* @var Config|MockObject
2730
*/
2831
private $config;
2932

3033
/**
31-
* @var BraintreeAdapterFactory
34+
* @var BraintreeAdapter|MockObject
3235
*/
33-
private $adapterFactory;
36+
private $braintreeAdapter;
3437

3538
/**
36-
* @var string
39+
* @var Session|MockObject
3740
*/
38-
private $clientToken = '';
41+
private $session;
3942

4043
/**
41-
* @var SessionManagerInterface
44+
* @var ConfigProvider
4245
*/
43-
private $session;
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+
}
4477

4578
/**
46-
* Constructor
79+
* Ensure that get config returns correct data if payment is active or not
4780
*
48-
* @param Config $config
49-
* @param BraintreeAdapterFactory $adapterFactory
50-
* @param SessionManagerInterface $session
81+
* @param array $config
82+
* @param array $expected
83+
* @dataProvider getConfigDataProvider
5184
*/
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;
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
118+
*/
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());
60131
}
61132

62133
/**
63-
* Retrieve assoc array of checkout configuration
64-
*
65134
* @return array
66135
*/
67-
public function getConfig()
136+
public function getConfigDataProvider()
68137
{
69-
$storeId = $this->session->getStoreId();
70-
$isActive = $this->config->isActive($storeId);
71138
return [
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,
86-
],
87-
Config::CODE_3DSECURE => [
88-
'enabled' => $this->config->isVerify3DSecure($storeId),
89-
'thresholdAmount' => $this->config->getThresholdAmount($storeId),
90-
'specificCountries' => $this->config->get3DSecureSpecificCountries($storeId),
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,
91157
],
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+
]
92184
],
185+
[
186+
'config' => [
187+
'isActive' => false,
188+
],
189+
'expected' => [
190+
'payment' => [
191+
ConfigProvider::CODE => [
192+
'isActive' => false,
193+
'clientToken' => null,
194+
]
195+
]
196+
]
197+
]
93198
];
94199
}
95200

96201
/**
97-
* Generate a new client token if necessary
98-
*
99-
* @return string
202+
* @return array
100203
*/
101-
public function getClientToken()
204+
public function getClientTokenDataProvider()
102205
{
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;
206+
return [
207+
[
208+
'merchantAccountId' => '',
209+
'params' => []
210+
],
211+
[
212+
'merchantAccountId' => self::MERCHANT_ACCOUNT_ID,
213+
'params' => ['merchantAccountId' => self::MERCHANT_ACCOUNT_ID]
214+
]
215+
];
117216
}
118217
}

0 commit comments

Comments
 (0)