123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Braintree\Test\Unit\Gateway\Config;
- use Magento\Braintree\Gateway\Config\Config;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\Serialize\Serializer\Json;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Store\Model\ScopeInterface;
- /**
- * Class ConfigTest
- */
- class ConfigTest extends \PHPUnit\Framework\TestCase
- {
- const METHOD_CODE = 'braintree';
- /**
- * @var Config
- */
- private $model;
- /**
- * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $scopeConfigMock;
- /**
- * @var Json|\PHPUnit_Framework_MockObject_MockObject
- */
- private $serializerMock;
- protected function setUp()
- {
- $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
- $this->serializerMock = $this->createMock(Json::class);
- $objectManager = new ObjectManager($this);
- $this->model = $objectManager->getObject(
- Config::class,
- [
- 'scopeConfig' => $this->scopeConfigMock,
- 'methodCode' => self::METHOD_CODE,
- 'serializer' => $this->serializerMock
- ]
- );
- }
- /**
- * @param string $encodedValue
- * @param string|array $value
- * @param array $expected
- * @dataProvider getCountrySpecificCardTypeConfigDataProvider
- */
- public function testGetCountrySpecificCardTypeConfig($encodedValue, $value, array $expected)
- {
- $this->scopeConfigMock->expects(static::once())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_COUNTRY_CREDIT_CARD), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($encodedValue);
- $this->serializerMock->expects($this->once())
- ->method('unserialize')
- ->with($encodedValue)
- ->willReturn($value);
- static::assertEquals(
- $expected,
- $this->model->getCountrySpecificCardTypeConfig()
- );
- }
- /**
- * @return array
- */
- public function getCountrySpecificCardTypeConfigDataProvider()
- {
- return [
- 'valid data' => [
- '{"GB":["VI","AE"],"US":["DI","JCB"]}',
- ['GB' => ['VI', 'AE'], 'US' => ['DI', 'JCB']],
- ['GB' => ['VI', 'AE'], 'US' => ['DI', 'JCB']]
- ],
- 'non-array value' => [
- '""',
- '',
- []
- ]
- ];
- }
- /**
- * @param string $value
- * @param array $expected
- * @dataProvider getAvailableCardTypesDataProvider
- */
- public function testGetAvailableCardTypes($value, $expected)
- {
- $this->scopeConfigMock->expects(static::once())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_CC_TYPES), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($value);
- static::assertEquals(
- $expected,
- $this->model->getAvailableCardTypes()
- );
- }
- /**
- * @return array
- */
- public function getAvailableCardTypesDataProvider()
- {
- return [
- [
- 'AE,VI,MC,DI,JCB',
- ['AE', 'VI', 'MC', 'DI', 'JCB']
- ],
- [
- '',
- []
- ]
- ];
- }
- /**
- * @param string $value
- * @param array $expected
- * @dataProvider getCcTypesMapperDataProvider
- */
- public function testGetCcTypesMapper($value, $expected)
- {
- $this->scopeConfigMock->expects(static::once())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_CC_TYPES_BRAINTREE_MAPPER), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($value);
- static::assertEquals(
- $expected,
- $this->model->getCcTypesMapper()
- );
- }
- /**
- * @return array
- */
- public function getCcTypesMapperDataProvider()
- {
- return [
- [
- '{"visa":"VI","american-express":"AE"}',
- ['visa' => 'VI', 'american-express' => 'AE']
- ],
- [
- '{invalid json}',
- []
- ],
- [
- '',
- []
- ]
- ];
- }
- /**
- * @covers \Magento\Braintree\Gateway\Config\Config::getCountryAvailableCardTypes
- * @dataProvider getCountrySpecificCardTypeConfigDataProvider
- * @param string $encodedData
- * @param string|array $data
- * @param array $countryData
- */
- public function testCountryAvailableCardTypes($encodedData, $data, array $countryData)
- {
- $this->scopeConfigMock->expects(static::any())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_COUNTRY_CREDIT_CARD), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($encodedData);
- $this->serializerMock->expects($this->any())
- ->method('unserialize')
- ->with($encodedData)
- ->willReturn($data);
- foreach ($countryData as $countryId => $types) {
- $result = $this->model->getCountryAvailableCardTypes($countryId);
- static::assertEquals($types, $result);
- }
- if (empty($countryData)) {
- static::assertEquals($data, "");
- }
- }
- /**
- * @covers \Magento\Braintree\Gateway\Config\Config::isCvvEnabled
- */
- public function testUseCvv()
- {
- $this->scopeConfigMock->expects(static::any())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_USE_CVV), ScopeInterface::SCOPE_STORE, null)
- ->willReturn(1);
- static::assertEquals(true, $this->model->isCvvEnabled());
- }
- /**
- * @param mixed $data
- * @param boolean $expected
- * @dataProvider verify3DSecureDataProvider
- * @covers \Magento\Braintree\Gateway\Config\Config::isVerify3DSecure
- */
- public function testIsVerify3DSecure($data, $expected)
- {
- $this->scopeConfigMock->expects(static::any())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_VERIFY_3DSECURE), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($data);
- static::assertEquals($expected, $this->model->isVerify3DSecure());
- }
- /**
- * Get items to verify 3d secure testing
- * @return array
- */
- public function verify3DSecureDataProvider()
- {
- return [
- ['data' => 1, 'expected' => true],
- ['data' => true, 'expected' => true],
- ['data' => '1', 'expected' => true],
- ['data' => 0, 'expected' => false],
- ['data' => '0', 'expected' => false],
- ['data' => false, 'expected' => false],
- ];
- }
- /**
- * @param mixed $data
- * @param double $expected
- * @covers \Magento\Braintree\Gateway\Config\Config::getThresholdAmount
- * @dataProvider thresholdAmountDataProvider
- */
- public function testGetThresholdAmount($data, $expected)
- {
- $this->scopeConfigMock->expects(static::any())
- ->method('getValue')
- ->with($this->getPath(Config::KEY_THRESHOLD_AMOUNT), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($data);
- static::assertEquals($expected, $this->model->getThresholdAmount());
- }
- /**
- * Get items for testing threshold amount
- * @return array
- */
- public function thresholdAmountDataProvider()
- {
- return [
- ['data' => '23.01', 'expected' => 23.01],
- ['data' => -1.02, 'expected' => -1.02],
- ['data' => true, 'expected' => 1],
- ['data' => 'true', 'expected' => 0],
- ['data' => 'abc', 'expected' => 0],
- ['data' => false, 'expected' => 0],
- ['data' => 'false', 'expected' => 0],
- ['data' => 1, 'expected' => 1],
- ];
- }
- /**
- * @param int $value
- * @param array $expected
- * @covers \Magento\Braintree\Gateway\Config\Config::get3DSecureSpecificCountries
- * @dataProvider threeDSecureSpecificCountriesDataProvider
- */
- public function testGet3DSecureSpecificCountries($value, array $expected)
- {
- $this->scopeConfigMock->expects(static::at(0))
- ->method('getValue')
- ->with($this->getPath(Config::KEY_VERIFY_ALLOW_SPECIFIC), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($value);
- if ($value !== Config::VALUE_3DSECURE_ALL) {
- $this->scopeConfigMock->expects(static::at(1))
- ->method('getValue')
- ->with($this->getPath(Config::KEY_VERIFY_SPECIFIC), ScopeInterface::SCOPE_STORE, null)
- ->willReturn('GB,US');
- }
- static::assertEquals($expected, $this->model->get3DSecureSpecificCountries());
- }
- /**
- * Get variations to test specific countries for 3d secure
- * @return array
- */
- public function threeDSecureSpecificCountriesDataProvider()
- {
- return [
- ['configValue' => 0, 'expected' => []],
- ['configValue' => 1, 'expected' => ['GB', 'US']],
- ];
- }
- /**
- * @covers \Magento\Braintree\Gateway\Config\Config::getDynamicDescriptors
- * @param $name
- * @param $phone
- * @param $url
- * @param array $expected
- * @dataProvider descriptorsDataProvider
- */
- public function testGetDynamicDescriptors($name, $phone, $url, array $expected)
- {
- $this->scopeConfigMock->expects(static::at(0))
- ->method('getValue')
- ->with($this->getPath('descriptor_name'), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($name);
- $this->scopeConfigMock->expects(static::at(1))
- ->method('getValue')
- ->with($this->getPath('descriptor_phone'), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($phone);
- $this->scopeConfigMock->expects(static::at(2))
- ->method('getValue')
- ->with($this->getPath('descriptor_url'), ScopeInterface::SCOPE_STORE, null)
- ->willReturn($url);
- $actual = $this->model->getDynamicDescriptors();
- static::assertEquals($expected, $actual);
- }
- /**
- * Get variations to test dynamic descriptors
- * @return array
- */
- public function descriptorsDataProvider()
- {
- $name = 'company * product';
- $phone = '333-22-22-333';
- $url = 'https://test.url.mage.com';
- return [
- [
- $name, $phone, $url,
- 'expected' => [
- 'name' => $name, 'phone' => $phone, 'url' => $url
- ]
- ],
- [
- $name, null, null,
- 'expected' => [
- 'name' => $name
- ]
- ],
- [
- null, null, $url,
- 'expected' => [
- 'url' => $url
- ]
- ],
- [
- null, null, null,
- 'expected' => []
- ]
- ];
- }
- /**
- * Return config path
- *
- * @param string $field
- * @return string
- */
- private function getPath($field)
- {
- return sprintf(Config::DEFAULT_PATH_PATTERN, self::METHOD_CODE, $field);
- }
- }
|