ConfigTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Gateway\Config;
  7. use Magento\Braintree\Gateway\Config\Config;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * Class ConfigTest
  14. */
  15. class ConfigTest extends \PHPUnit\Framework\TestCase
  16. {
  17. const METHOD_CODE = 'braintree';
  18. /**
  19. * @var Config
  20. */
  21. private $model;
  22. /**
  23. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $scopeConfigMock;
  26. /**
  27. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $serializerMock;
  30. protected function setUp()
  31. {
  32. $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
  33. $this->serializerMock = $this->createMock(Json::class);
  34. $objectManager = new ObjectManager($this);
  35. $this->model = $objectManager->getObject(
  36. Config::class,
  37. [
  38. 'scopeConfig' => $this->scopeConfigMock,
  39. 'methodCode' => self::METHOD_CODE,
  40. 'serializer' => $this->serializerMock
  41. ]
  42. );
  43. }
  44. /**
  45. * @param string $encodedValue
  46. * @param string|array $value
  47. * @param array $expected
  48. * @dataProvider getCountrySpecificCardTypeConfigDataProvider
  49. */
  50. public function testGetCountrySpecificCardTypeConfig($encodedValue, $value, array $expected)
  51. {
  52. $this->scopeConfigMock->expects(static::once())
  53. ->method('getValue')
  54. ->with($this->getPath(Config::KEY_COUNTRY_CREDIT_CARD), ScopeInterface::SCOPE_STORE, null)
  55. ->willReturn($encodedValue);
  56. $this->serializerMock->expects($this->once())
  57. ->method('unserialize')
  58. ->with($encodedValue)
  59. ->willReturn($value);
  60. static::assertEquals(
  61. $expected,
  62. $this->model->getCountrySpecificCardTypeConfig()
  63. );
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function getCountrySpecificCardTypeConfigDataProvider()
  69. {
  70. return [
  71. 'valid data' => [
  72. '{"GB":["VI","AE"],"US":["DI","JCB"]}',
  73. ['GB' => ['VI', 'AE'], 'US' => ['DI', 'JCB']],
  74. ['GB' => ['VI', 'AE'], 'US' => ['DI', 'JCB']]
  75. ],
  76. 'non-array value' => [
  77. '""',
  78. '',
  79. []
  80. ]
  81. ];
  82. }
  83. /**
  84. * @param string $value
  85. * @param array $expected
  86. * @dataProvider getAvailableCardTypesDataProvider
  87. */
  88. public function testGetAvailableCardTypes($value, $expected)
  89. {
  90. $this->scopeConfigMock->expects(static::once())
  91. ->method('getValue')
  92. ->with($this->getPath(Config::KEY_CC_TYPES), ScopeInterface::SCOPE_STORE, null)
  93. ->willReturn($value);
  94. static::assertEquals(
  95. $expected,
  96. $this->model->getAvailableCardTypes()
  97. );
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function getAvailableCardTypesDataProvider()
  103. {
  104. return [
  105. [
  106. 'AE,VI,MC,DI,JCB',
  107. ['AE', 'VI', 'MC', 'DI', 'JCB']
  108. ],
  109. [
  110. '',
  111. []
  112. ]
  113. ];
  114. }
  115. /**
  116. * @param string $value
  117. * @param array $expected
  118. * @dataProvider getCcTypesMapperDataProvider
  119. */
  120. public function testGetCcTypesMapper($value, $expected)
  121. {
  122. $this->scopeConfigMock->expects(static::once())
  123. ->method('getValue')
  124. ->with($this->getPath(Config::KEY_CC_TYPES_BRAINTREE_MAPPER), ScopeInterface::SCOPE_STORE, null)
  125. ->willReturn($value);
  126. static::assertEquals(
  127. $expected,
  128. $this->model->getCcTypesMapper()
  129. );
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getCcTypesMapperDataProvider()
  135. {
  136. return [
  137. [
  138. '{"visa":"VI","american-express":"AE"}',
  139. ['visa' => 'VI', 'american-express' => 'AE']
  140. ],
  141. [
  142. '{invalid json}',
  143. []
  144. ],
  145. [
  146. '',
  147. []
  148. ]
  149. ];
  150. }
  151. /**
  152. * @covers \Magento\Braintree\Gateway\Config\Config::getCountryAvailableCardTypes
  153. * @dataProvider getCountrySpecificCardTypeConfigDataProvider
  154. * @param string $encodedData
  155. * @param string|array $data
  156. * @param array $countryData
  157. */
  158. public function testCountryAvailableCardTypes($encodedData, $data, array $countryData)
  159. {
  160. $this->scopeConfigMock->expects(static::any())
  161. ->method('getValue')
  162. ->with($this->getPath(Config::KEY_COUNTRY_CREDIT_CARD), ScopeInterface::SCOPE_STORE, null)
  163. ->willReturn($encodedData);
  164. $this->serializerMock->expects($this->any())
  165. ->method('unserialize')
  166. ->with($encodedData)
  167. ->willReturn($data);
  168. foreach ($countryData as $countryId => $types) {
  169. $result = $this->model->getCountryAvailableCardTypes($countryId);
  170. static::assertEquals($types, $result);
  171. }
  172. if (empty($countryData)) {
  173. static::assertEquals($data, "");
  174. }
  175. }
  176. /**
  177. * @covers \Magento\Braintree\Gateway\Config\Config::isCvvEnabled
  178. */
  179. public function testUseCvv()
  180. {
  181. $this->scopeConfigMock->expects(static::any())
  182. ->method('getValue')
  183. ->with($this->getPath(Config::KEY_USE_CVV), ScopeInterface::SCOPE_STORE, null)
  184. ->willReturn(1);
  185. static::assertEquals(true, $this->model->isCvvEnabled());
  186. }
  187. /**
  188. * @param mixed $data
  189. * @param boolean $expected
  190. * @dataProvider verify3DSecureDataProvider
  191. * @covers \Magento\Braintree\Gateway\Config\Config::isVerify3DSecure
  192. */
  193. public function testIsVerify3DSecure($data, $expected)
  194. {
  195. $this->scopeConfigMock->expects(static::any())
  196. ->method('getValue')
  197. ->with($this->getPath(Config::KEY_VERIFY_3DSECURE), ScopeInterface::SCOPE_STORE, null)
  198. ->willReturn($data);
  199. static::assertEquals($expected, $this->model->isVerify3DSecure());
  200. }
  201. /**
  202. * Get items to verify 3d secure testing
  203. * @return array
  204. */
  205. public function verify3DSecureDataProvider()
  206. {
  207. return [
  208. ['data' => 1, 'expected' => true],
  209. ['data' => true, 'expected' => true],
  210. ['data' => '1', 'expected' => true],
  211. ['data' => 0, 'expected' => false],
  212. ['data' => '0', 'expected' => false],
  213. ['data' => false, 'expected' => false],
  214. ];
  215. }
  216. /**
  217. * @param mixed $data
  218. * @param double $expected
  219. * @covers \Magento\Braintree\Gateway\Config\Config::getThresholdAmount
  220. * @dataProvider thresholdAmountDataProvider
  221. */
  222. public function testGetThresholdAmount($data, $expected)
  223. {
  224. $this->scopeConfigMock->expects(static::any())
  225. ->method('getValue')
  226. ->with($this->getPath(Config::KEY_THRESHOLD_AMOUNT), ScopeInterface::SCOPE_STORE, null)
  227. ->willReturn($data);
  228. static::assertEquals($expected, $this->model->getThresholdAmount());
  229. }
  230. /**
  231. * Get items for testing threshold amount
  232. * @return array
  233. */
  234. public function thresholdAmountDataProvider()
  235. {
  236. return [
  237. ['data' => '23.01', 'expected' => 23.01],
  238. ['data' => -1.02, 'expected' => -1.02],
  239. ['data' => true, 'expected' => 1],
  240. ['data' => 'true', 'expected' => 0],
  241. ['data' => 'abc', 'expected' => 0],
  242. ['data' => false, 'expected' => 0],
  243. ['data' => 'false', 'expected' => 0],
  244. ['data' => 1, 'expected' => 1],
  245. ];
  246. }
  247. /**
  248. * @param int $value
  249. * @param array $expected
  250. * @covers \Magento\Braintree\Gateway\Config\Config::get3DSecureSpecificCountries
  251. * @dataProvider threeDSecureSpecificCountriesDataProvider
  252. */
  253. public function testGet3DSecureSpecificCountries($value, array $expected)
  254. {
  255. $this->scopeConfigMock->expects(static::at(0))
  256. ->method('getValue')
  257. ->with($this->getPath(Config::KEY_VERIFY_ALLOW_SPECIFIC), ScopeInterface::SCOPE_STORE, null)
  258. ->willReturn($value);
  259. if ($value !== Config::VALUE_3DSECURE_ALL) {
  260. $this->scopeConfigMock->expects(static::at(1))
  261. ->method('getValue')
  262. ->with($this->getPath(Config::KEY_VERIFY_SPECIFIC), ScopeInterface::SCOPE_STORE, null)
  263. ->willReturn('GB,US');
  264. }
  265. static::assertEquals($expected, $this->model->get3DSecureSpecificCountries());
  266. }
  267. /**
  268. * Get variations to test specific countries for 3d secure
  269. * @return array
  270. */
  271. public function threeDSecureSpecificCountriesDataProvider()
  272. {
  273. return [
  274. ['configValue' => 0, 'expected' => []],
  275. ['configValue' => 1, 'expected' => ['GB', 'US']],
  276. ];
  277. }
  278. /**
  279. * @covers \Magento\Braintree\Gateway\Config\Config::getDynamicDescriptors
  280. * @param $name
  281. * @param $phone
  282. * @param $url
  283. * @param array $expected
  284. * @dataProvider descriptorsDataProvider
  285. */
  286. public function testGetDynamicDescriptors($name, $phone, $url, array $expected)
  287. {
  288. $this->scopeConfigMock->expects(static::at(0))
  289. ->method('getValue')
  290. ->with($this->getPath('descriptor_name'), ScopeInterface::SCOPE_STORE, null)
  291. ->willReturn($name);
  292. $this->scopeConfigMock->expects(static::at(1))
  293. ->method('getValue')
  294. ->with($this->getPath('descriptor_phone'), ScopeInterface::SCOPE_STORE, null)
  295. ->willReturn($phone);
  296. $this->scopeConfigMock->expects(static::at(2))
  297. ->method('getValue')
  298. ->with($this->getPath('descriptor_url'), ScopeInterface::SCOPE_STORE, null)
  299. ->willReturn($url);
  300. $actual = $this->model->getDynamicDescriptors();
  301. static::assertEquals($expected, $actual);
  302. }
  303. /**
  304. * Get variations to test dynamic descriptors
  305. * @return array
  306. */
  307. public function descriptorsDataProvider()
  308. {
  309. $name = 'company * product';
  310. $phone = '333-22-22-333';
  311. $url = 'https://test.url.mage.com';
  312. return [
  313. [
  314. $name, $phone, $url,
  315. 'expected' => [
  316. 'name' => $name, 'phone' => $phone, 'url' => $url
  317. ]
  318. ],
  319. [
  320. $name, null, null,
  321. 'expected' => [
  322. 'name' => $name
  323. ]
  324. ],
  325. [
  326. null, null, $url,
  327. 'expected' => [
  328. 'url' => $url
  329. ]
  330. ],
  331. [
  332. null, null, null,
  333. 'expected' => []
  334. ]
  335. ];
  336. }
  337. /**
  338. * Return config path
  339. *
  340. * @param string $field
  341. * @return string
  342. */
  343. private function getPath($field)
  344. {
  345. return sprintf(Config::DEFAULT_PATH_PATTERN, self::METHOD_CODE, $field);
  346. }
  347. }