Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
7 
8 class DataTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_scopeConfigMock;
14 
18  protected $_registryMock;
19 
23  protected $_helper;
24 
25  protected function setUp()
26  {
27  $className = \Magento\GoogleAdwords\Helper\Data::class;
28  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
29  $arguments = $objectManager->getConstructArguments($className);
30  $this->_helper = $objectManager->getObject($className, $arguments);
32  $context = $arguments['context'];
33  $this->_scopeConfigMock = $context->getScopeConfig();
34  $this->_registryMock = $arguments['registry'];
35  }
36 
40  public function dataProviderForTestIsActive()
41  {
42  return [
43  [true, 1234, true],
44  [true, 'conversionId', false],
45  [true, '', false],
46  [false, '', false]
47  ];
48  }
49 
56  public function testIsGoogleAdwordsActive($isActive, $returnConfigValue, $returnValue)
57  {
58  $this->_scopeConfigMock->expects(
59  $this->any()
60  )->method(
61  'isSetFlag'
62  )->with(
63  \Magento\GoogleAdwords\Helper\Data::XML_PATH_ACTIVE
64  )->will(
65  $this->returnValue($isActive)
66  );
67  $this->_scopeConfigMock->expects($this->any())->method('getValue')->with($this->isType('string'))->will(
68  $this->returnCallback(
69  function () use ($returnConfigValue) {
70  return $returnConfigValue;
71  }
72  )
73  );
74 
75  $this->assertEquals($returnValue, $this->_helper->isGoogleAdwordsActive());
76  }
77 
78  public function testGetLanguageCodes()
79  {
80  $languages = ['en', 'ru', 'uk'];
81  $this->_scopeConfigMock->expects(
82  $this->once()
83  )->method(
84  'getValue'
85  )->with(
86  \Magento\GoogleAdwords\Helper\Data::XML_PATH_LANGUAGES,
87  'default'
88  )->will(
89  $this->returnValue($languages)
90  );
91  $this->assertEquals($languages, $this->_helper->getLanguageCodes());
92  }
93 
98  {
99  return [
100  ['some-language', 'some-language'],
101  ['zh_TW', 'zh_Hant'],
102  ['zh_CN', 'zh_Hans'],
103  ['iw', 'he']
104  ];
105  }
106 
112  public function testConvertLanguageCodeToLocaleCode($language, $returnLanguage)
113  {
114  $convertArray = ['zh_TW' => 'zh_Hant', 'iw' => 'he', 'zh_CN' => 'zh_Hans'];
115  $this->_scopeConfigMock->expects(
116  $this->once()
117  )->method(
118  'getValue'
119  )->with(
120  \Magento\GoogleAdwords\Helper\Data::XML_PATH_LANGUAGE_CONVERT,
121  'default'
122  )->will(
123  $this->returnValue($convertArray)
124  );
125  $this->assertEquals($returnLanguage, $this->_helper->convertLanguageCodeToLocaleCode($language));
126  }
127 
128  public function testGetConversionImgSrc()
129  {
130  $conversionId = 123;
131  $label = 'LabEl';
132  $imgSrc = sprintf(
133  'https://www.googleadservices.com/pagead/conversion/%s/?label=%s&amp;guid=ON&amp;script=0',
134  $conversionId,
135  $label
136  );
137  $this->_scopeConfigMock->expects(
138  $this->at(0)
139  )->method(
140  'getValue'
141  )->with(
142  \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_IMG_SRC,
143  'default'
144  )->will(
145  $this->returnValue($imgSrc)
146  );
147  $this->assertEquals($imgSrc, $this->_helper->getConversionImgSrc());
148  }
149 
150  public function testGetConversionJsSrc()
151  {
152  $jsSrc = 'some-js-src';
153  $this->_scopeConfigMock->expects(
154  $this->once()
155  )->method(
156  'getValue'
157  )->with(
158  \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_JS_SRC
159  )->will(
160  $this->returnValue($jsSrc)
161  );
162  $this->assertEquals($jsSrc, $this->_helper->getConversionJsSrc());
163  }
164 
169  {
170  return [
174  ['getConversionColor', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_COLOR, 'ffffff'],
175  ['getConversionLabel', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_LABEL, 'Label'],
177  ['getConversionValueConstant', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE, '0'],
178  ];
179  }
180 
187  public function testGetStoreConfigValue($method, $xmlPath, $returnValue)
188  {
189  $this->_scopeConfigMock->expects(
190  $this->once()
191  )->method(
192  'getValue'
193  )->with(
194  $xmlPath
195  )->will(
196  $this->returnValue($returnValue)
197  );
198 
199  $this->assertEquals($returnValue, $this->_helper->{$method}());
200  }
201 
203  {
204  $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
205 
206  $this->assertTrue($this->_helper->hasSendConversionValueCurrency());
207  }
208 
210  {
211  $returnValue = 4.1;
212  $this->_scopeConfigMock->expects(
213  $this->any()
214  )->method(
215  'getValue'
216  )->with(
217  \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE
218  )->will(
219  $this->returnValue(\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_TYPE_DYNAMIC)
220  );
221  $this->_registryMock->expects(
222  $this->once()
223  )->method(
224  'registry'
225  )->with(
226  \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME
227  )->will(
228  $this->returnValue($returnValue)
229  );
230 
231  $this->assertEquals($returnValue, $this->_helper->getConversionValue());
232  }
233 
235  {
236  $returnValueCurrency = 'USD';
237  $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
238  $this->_registryMock->expects(
239  $this->once()
240  )->method(
241  'registry'
242  )->with(
243  \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME
244  )->will(
245  $this->returnValue($returnValueCurrency)
246  );
247 
248  $this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency());
249  }
250 
255  {
257  }
258 
264  public function testGetConversionValueConstant($conversionValueConst, $returnValue)
265  {
266  $this->_scopeConfigMock->expects(
267  $this->at(0)
268  )->method(
269  'getValue'
270  )->with(
271  \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE
272  )->will(
273  $this->returnValue(\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_TYPE_CONSTANT)
274  );
275  $this->_registryMock->expects($this->never())->method('registry');
276  $this->_scopeConfigMock->expects(
277  $this->at(1)
278  )->method(
279  'getValue'
280  )->with(
281  \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE
282  )->will(
283  $this->returnValue($conversionValueConst)
284  );
285 
286  $this->assertEquals($returnValue, $this->_helper->getConversionValue());
287  }
288 }
$objectManager
Definition: bootstrap.php:17
testGetConversionValueConstant($conversionValueConst, $returnValue)
Definition: DataTest.php:264
testGetStoreConfigValue($method, $xmlPath, $returnValue)
Definition: DataTest.php:187
$label
Definition: details.phtml:21
$method
Definition: info.phtml:13
$arguments
testIsGoogleAdwordsActive($isActive, $returnConfigValue, $returnValue)
Definition: DataTest.php:56
testConvertLanguageCodeToLocaleCode($language, $returnLanguage)
Definition: DataTest.php:112
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31