Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RegionTest.php
Go to the documentation of this file.
1 <?php
7 
8 class RegionTest extends \PHPUnit\Framework\TestCase
9 {
14  public function testRender($regionCollection)
15  {
16  $countryFactoryMock = $this->createMock(
17  \Magento\Directory\Model\CountryFactory::class
18  );
19  $directoryHelperMock = $this->createPartialMock(
20  \Magento\Directory\Helper\Data::class,
21  ['isRegionRequired']
22  );
23  $escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
24  $elementMock = $this->createPartialMock(
25  \Magento\Framework\Data\Form\Element\AbstractElement::class,
26  ['getForm', 'getHtmlAttributes']
27  );
28  $countryMock = $this->createPartialMock(
29  \Magento\Framework\Data\Form\Element\AbstractElement::class,
30  ['getValue']
31  );
32  $regionMock = $this->createMock(
33  \Magento\Framework\Data\Form\Element\AbstractElement::class
34  );
35  $countryModelMock = $this->createPartialMock(
36  \Magento\Directory\Model\Country::class,
37  ['setId', 'getLoadedRegionCollection', 'toOptionArray', '__wakeup']
38  );
39  $formMock = $this->createPartialMock(\Magento\Framework\Data\Form::class, ['getElement']);
40 
41  $elementMock->expects($this->any())->method('getForm')->will($this->returnValue($formMock));
42  $elementMock->expects(
43  $this->any()
44  )->method(
45  'getHtmlAttributes'
46  )->will(
47  $this->returnValue(
48  [
49  'title',
50  'class',
51  'style',
52  'onclick',
53  'onchange',
54  'disabled',
55  'readonly',
56  'tabindex',
57  'placeholder',
58  ]
59  )
60  );
61  $formMock->expects(
62  $this->any()
63  )->method(
64  'getElement'
65  )->will(
66  $this->returnValueMap([['country_id', $countryMock], ['region_id', $regionMock]])
67  );
68  $countryMock->expects($this->any())->method('getValue')->will($this->returnValue('GE'));
69  $directoryHelperMock->expects(
70  $this->any()
71  )->method(
72  'isRegionRequired'
73  )->will(
74  $this->returnValueMap([['GE', true]])
75  );
76  $countryFactoryMock->expects($this->once())->method('create')->will($this->returnValue($countryModelMock));
77  $countryModelMock->expects($this->any())->method('setId')->will($this->returnSelf());
78  $countryModelMock->expects($this->any())->method('getLoadedRegionCollection')->will($this->returnSelf());
79  $countryModelMock->expects($this->any())->method('toOptionArray')->will($this->returnValue($regionCollection));
80 
81  $model = new \Magento\Customer\Model\Renderer\Region($countryFactoryMock, $directoryHelperMock, $escaperMock);
82 
83  $static = new \ReflectionProperty(\Magento\Customer\Model\Renderer\Region::class, '_regionCollections');
84  $static->setAccessible(true);
85  $static->setValue([]);
86 
87  $html = $model->render($elementMock);
88 
89  $this->assertContains('required', $html);
90  $this->assertContains('required-entry', $html);
91  }
92 
96  public function renderDataProvider()
97  {
98  return [
99  'with no defined regions' => [[]],
100  'with defined regions' => [
101  [
102  new \Magento\Framework\DataObject(['value' => 'Bavaria']),
103  new \Magento\Framework\DataObject(['value' => 'Saxony']),
104  ],
105  ]
106  ];
107  }
108 }