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