Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class AddressTest extends \PHPUnit\Framework\TestCase
16 {
18  protected $helper;
19 
21  protected $context;
22 
24  protected $blockFactory;
25 
27  protected $storeManager;
28 
30  protected $scopeConfig;
31 
34 
36  protected $addressConfig;
37 
39  private $addressMetadataService;
40 
41  protected function setUp()
42  {
43  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
44  $className = \Magento\Customer\Helper\Address::class;
45  $arguments = $objectManagerHelper->getConstructArguments($className);
47  $this->context = $arguments['context'];
48  $this->blockFactory = $arguments['blockFactory'];
49  $this->storeManager = $arguments['storeManager'];
50  $this->scopeConfig = $this->context->getScopeConfig();
51  $this->customerMetadataService = $arguments['customerMetadataService'];
52  $this->addressConfig = $arguments['addressConfig'];
53  $this->addressMetadataService = $arguments['addressMetadataService'];
54 
55  $this->helper = $objectManagerHelper->getObject($className, $arguments);
56  }
57 
63  public function testGetStreetLines($numLines, $expectedNumLines)
64  {
65  $attributeMock = $this->getMockBuilder(
66  \Magento\Customer\Api\Data\AttributeMetadataInterface::class
67  )->getMock();
68  $attributeMock->expects($this->any())->method('getMultilineCount')->will($this->returnValue($numLines));
69 
70  $this->addressMetadataService
71  ->expects($this->any())
72  ->method('getAttributeMetadata')
73  ->will($this->returnValue($attributeMock));
74 
75  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
76  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
77 
78  $this->assertEquals($expectedNumLines, $this->helper->getStreetLines());
79  }
80 
84  public function providerGetStreetLines()
85  {
86  return [
87  [-1, 2],
88  [0, 2],
89  [1, 1],
90  [2, 2],
91  [3, 3],
92  [4, 4],
93  [5, 5],
94  [10, 10],
95  [15, 15],
96  [20, 20],
97  [21, 20],
98  ];
99  }
100 
104  public function testGetRenderer($renderer, $blockFactory, $result)
105  {
106  $this->helper = new \Magento\Customer\Helper\Address(
107  $this->context,
109  $this->storeManager,
110  $this->customerMetadataService,
111  $this->addressMetadataService,
112  $this->addressConfig
113  );
114  $this->assertEquals($result, $this->helper->getRenderer($renderer));
115  }
116 
120  public function getRendererDataProvider()
121  {
122  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)->getMock();
123  $blockFactory = $this->getMockBuilder(
124  \Magento\Framework\View\Element\BlockFactory::class
125  )->disableOriginalConstructor()->getMock();
126  $blockFactory->expects($this->once())
127  ->method('createBlock')
128  ->with('some_test_block', [])
129  ->will($this->returnValue($blockMock));
130  return [
131  ['some_test_block', $blockFactory, $blockMock],
132  [$blockMock, $blockFactory, $blockMock],
133  ];
134  }
135 
136  public function testGetConfigCanShowConfig()
137  {
138  $result = ['key1' => 'value1', 'key2' => 'value2'];
139  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
140  $store->expects($this->any())
141  ->method('getWebsiteId')
142  ->will($this->returnValue('1'));
143  $this->scopeConfig->expects($this->once())//test method cache
144  ->method('getValue')
145  ->with('customer/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)
146  ->will($this->returnValue($result));
147  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
148  $this->assertNull($this->helper->getConfig('unavailable_key'));
149  $this->assertFalse($this->helper->canShowConfig('unavailable_key'));
150  $this->assertEquals($result['key1'], $this->helper->getConfig('key1'));
151  $this->assertEquals($result['key2'], $this->helper->getConfig('key2'));
152  $this->assertTrue($this->helper->canShowConfig('key1'));
153  $this->assertTrue($this->helper->canShowConfig('key2'));
154  }
155 
157  {
158  $attributeCode = 'attr_code';
159  $attributeClass = 'Attribute_Class';
160 
161  $attributeMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
162  ->getMockForAbstractClass();
163  $attributeMock->expects($this->once())
164  ->method('getFrontendClass')
165  ->willReturn($attributeClass);
166 
167  $this->addressMetadataService->expects($this->any())
168  ->method('getAttributeMetadata')
169  ->willReturn($attributeMock);
170 
171  $this->assertEquals($attributeClass, $this->helper->getAttributeValidationClass($attributeCode));
172  }
173 
175  {
176  $attrCode = 'attr_code';
177 
178  $this->addressMetadataService->expects($this->any())
179  ->method('getAttributeMetadata')
180  ->willReturn(null);
181 
182  $this->assertEquals('', $this->helper->getAttributeValidationClass($attrCode));
183  }
184 
191  public function testConvertStreetLines($origStreets, $toCount, $result)
192  {
193  $this->assertEquals($result, $this->helper->convertStreetLines($origStreets, $toCount));
194  }
195 
200  {
201  return [
202  [['street1', 'street2', 'street3', 'street4'], 3, ['street1 street2', 'street3', 'street4']],
203  [['street1', 'street2', 'street3', 'street4'], 2, ['street1 street2', 'street3 street4']],
204  ];
205  }
206 
213  {
214  $this->scopeConfig->expects($this->once())
215  ->method('getValue')
216  ->with(
217  \Magento\Customer\Helper\Address::XML_PATH_VAT_VALIDATION_ENABLED,
218  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
219  $store
220  )
221  ->will($this->returnValue($result));
222  $this->assertEquals($result, $this->helper->isVatValidationEnabled($store));
223  }
224 
229  {
230  return [
231  [0, true],
232  [1, false],
233  [2, true],
234  ];
235  }
236 
243  {
244  $this->scopeConfig->expects($this->once())
245  ->method('getValue')
246  ->with(
247  \Magento\Customer\Helper\Address::XML_PATH_VIV_ON_EACH_TRANSACTION,
248  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
249  $store
250  )
251  ->will($this->returnValue($result));
252  $this->assertEquals($result, $this->helper->hasValidateOnEachTransaction($store));
253  }
254 
259  {
260  return [
261  [0, true],
262  [1, false],
263  [2, true],
264  ];
265  }
266 
273  {
274  $this->scopeConfig->expects($this->once())
275  ->method('getValue')
276  ->with(
277  \Magento\Customer\Helper\Address::XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE,
278  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
279  $store
280  )
281  ->will($this->returnValue($result));
282  $this->assertEquals($result, $this->helper->getTaxCalculationAddressType($store));
283  }
284 
289  {
290  return [
291  [0, 'address_type_store_0'],
292  [1, 'address_type_store_1'],
293  [2, 'address_type_store_2'],
294  ];
295  }
296 
298  {
299  $this->scopeConfig->expects($this->once())
300  ->method('getValue')
301  ->with(
302  \Magento\Customer\Helper\Address::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT,
303  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
304  )
305  ->will($this->returnValue(true));
306  $this->assertTrue($this->helper->isDisableAutoGroupAssignDefaultValue());
307  }
308 
309  public function testIsVatAttributeVisible()
310  {
311  $this->scopeConfig->expects($this->once())
312  ->method('getValue')
313  ->with(
314  \Magento\Customer\Helper\Address::XML_PATH_VAT_FRONTEND_VISIBILITY,
315  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
316  )
317  ->will($this->returnValue(true));
318  $this->assertTrue($this->helper->isVatAttributeVisible());
319  }
320 
327  {
328  $this->addressConfig->expects($this->once())
329  ->method('getFormatByCode')
330  ->with($code)
331  ->will($this->returnValue(
332  new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
333  ));
334  $this->assertEquals($result, $this->helper->getFormatTypeRenderer($code));
335  }
336 
341  {
342  $renderer = $this->getMockBuilder(\Magento\Customer\Block\Address\Renderer\RendererInterface::class)
343  ->disableOriginalConstructor()->getMock();
344  return [
345  ['valid_code', $renderer],
346  ['invalid_code', null]
347  ];
348  }
349 
355  public function testGetFormat($code, $result)
356  {
357  if ($result) {
358  $renderer = $this->getMockBuilder(\Magento\Customer\Block\Address\Renderer\RendererInterface::class)
359  ->disableOriginalConstructor()->getMock();
360  $renderer->expects($this->once())
361  ->method('getFormatArray')
362  ->will($this->returnValue(['key' => 'value']));
363  }
364  $this->addressConfig->expects($this->once())
365  ->method('getFormatByCode')
366  ->with($code)
367  ->will($this->returnValue(
368  new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
369  ));
370 
371  $this->assertEquals($result, $this->helper->getFormat($code));
372  }
373 
377  public function getFormatDataProvider()
378  {
379  return [
380  ['valid_code', ['key' => 'value']],
381  ['invalid_code', '']
382  ];
383  }
384 
390  public function testIsAttributeVisible($attributeCode, $isMetadataExists)
391  {
392  $attributeMetadata = null;
393  if ($isMetadataExists) {
394  $attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
395  ->getMockForAbstractClass();
396  $attributeMetadata->expects($this->once())
397  ->method('isVisible')
398  ->willReturn(true);
399  }
400  $this->addressMetadataService->expects($this->once())
401  ->method('getAttributeMetadata')
402  ->with($attributeCode)
403  ->willReturn($attributeMetadata);
404  $this->assertEquals($isMetadataExists, $this->helper->isAttributeVisible($attributeCode));
405  }
406 
411  {
412  return [
413  ['fax', true],
414  ['invalid_code', false]
415  ];
416  }
417 }
testGetRenderer($renderer, $blockFactory, $result)
$attributeCode
Definition: extend.phtml:12
$arguments
testIsAttributeVisible($attributeCode, $isMetadataExists)
testGetStreetLines($numLines, $expectedNumLines)
Definition: AddressTest.php:63
testConvertStreetLines($origStreets, $toCount, $result)
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31