|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 |
| -declare(strict_types=1); |
7 |
| - |
8 |
| -namespace Magento\Braintree\Test\Unit\Model\Ui; |
| 6 | +namespace Magento\Braintree\Model\Ui; |
9 | 7 |
|
10 | 8 | use Magento\Braintree\Gateway\Config\Config;
|
11 |
| -use Magento\Braintree\Model\Adapter\BraintreeAdapter; |
| 9 | +use Magento\Braintree\Gateway\Request\PaymentDataBuilder; |
12 | 10 | 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; |
16 | 13 |
|
17 | 14 | /**
|
18 |
| - * Class ConfigProviderTest |
| 15 | + * Class ConfigProvider |
19 | 16 | *
|
20 |
| - * Test for class \Magento\Braintree\Model\Ui\ConfigProvider |
| 17 | + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) |
21 | 18 | */
|
22 |
| -class ConfigProviderTest extends \PHPUnit\Framework\TestCase |
| 19 | +class ConfigProvider implements ConfigProviderInterface |
23 | 20 | {
|
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'; |
27 | 24 |
|
28 | 25 | /**
|
29 |
| - * @var Config|MockObject |
| 26 | + * @var Config |
30 | 27 | */
|
31 | 28 | private $config;
|
32 | 29 |
|
33 | 30 | /**
|
34 |
| - * @var BraintreeAdapter|MockObject |
| 31 | + * @var BraintreeAdapterFactory |
35 | 32 | */
|
36 |
| - private $braintreeAdapter; |
| 33 | + private $adapterFactory; |
37 | 34 |
|
38 | 35 | /**
|
39 |
| - * @var Session|MockObject |
| 36 | + * @var string |
40 | 37 | */
|
41 |
| - private $session; |
| 38 | + private $clientToken = ''; |
42 | 39 |
|
43 | 40 | /**
|
44 |
| - * @var ConfigProvider |
| 41 | + * @var SessionManagerInterface |
45 | 42 | */
|
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; |
77 | 44 |
|
78 | 45 | /**
|
79 |
| - * Ensure that get config returns correct data if payment is active or not |
| 46 | + * Constructor |
80 | 47 | *
|
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 |
118 | 51 | */
|
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; |
131 | 60 | }
|
132 | 61 |
|
133 | 62 | /**
|
| 63 | + * Retrieve assoc array of checkout configuration |
| 64 | + * |
134 | 65 | * @return array
|
135 | 66 | */
|
136 |
| - public function getConfigDataProvider() |
| 67 | + public function getConfig() |
137 | 68 | {
|
| 69 | + $storeId = $this->session->getStoreId(); |
| 70 | + $isActive = $this->config->isActive($storeId); |
138 | 71 | 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, |
157 | 86 | ],
|
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), |
188 | 91 | ],
|
189 |
| - 'expected' => [ |
190 |
| - 'payment' => [ |
191 |
| - ConfigProvider::CODE => [ |
192 |
| - 'isActive' => false, |
193 |
| - 'clientToken' => null, |
194 |
| - ] |
195 |
| - ] |
196 |
| - ] |
197 |
| - ] |
| 92 | + ], |
198 | 93 | ];
|
199 | 94 | }
|
200 | 95 |
|
201 | 96 | /**
|
202 |
| - * @return array |
| 97 | + * Generate a new client token if necessary |
| 98 | + * |
| 99 | + * @return string |
203 | 100 | */
|
204 |
| - public function getClientTokenDataProvider() |
| 101 | + public function getClientToken() |
205 | 102 | {
|
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; |
216 | 117 | }
|
217 | 118 | }
|
0 commit comments