1818use Magento \Framework \App \Helper \Context ;
1919use Magento \Framework \App \RequestInterface ;
2020use Magento \Framework \DataObject ;
21+ use Magento \Framework \Exception \NoSuchEntityException ;
2122use Magento \Framework \Json \Helper \Data as JsonDataHelper ;
2223use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2324use Magento \Store \Model \ScopeInterface ;
@@ -111,39 +112,45 @@ protected function setUp(): void
111112 $ this ->_object = $ objectManager ->getObject (Data::class, $ arguments );
112113 }
113114
114- public function testGetRegionJson ()
115- {
115+ /**
116+ * @param string|null $configValue
117+ * @param array $countryIds
118+ * @param array $regionList
119+ * @param array $expectedDataToEncode
120+ *
121+ * @throws NoSuchEntityException
122+ * @dataProvider getRegionJsonDataProvider
123+ */
124+ public function testGetRegionJson (
125+ ?string $ configValue ,
126+ array $ countryIds ,
127+ array $ regionList ,
128+ array $ expectedDataToEncode
129+ ) {
116130 $ this ->scopeConfigMock ->method ('getValue ' )
117131 ->willReturnMap (
118132 [
119133 [
120134 AllowedCountries::ALLOWED_COUNTRIES_PATH ,
121135 ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
122136 null ,
123- ' Country1,Country2 '
137+ $ configValue
124138 ],
125139 [Data::XML_PATH_STATES_REQUIRED , ScopeInterface::SCOPE_STORE , null , '' ]
126140 ]
127141 );
128- $ regions = [
129- new DataObject (
130- ['country_id ' => 'Country1 ' , 'region_id ' => 'r1 ' , 'code ' => 'r1-code ' , 'name ' => 'r1-name ' ]
131- ),
132- new DataObject (
133- ['country_id ' => 'Country1 ' , 'region_id ' => 'r2 ' , 'code ' => 'r2-code ' , 'name ' => 'r2-name ' ]
134- ),
135- new DataObject (
136- ['country_id ' => 'Country2 ' , 'region_id ' => 'r3 ' , 'code ' => 'r3-code ' , 'name ' => 'r3-name ' ]
137- )
138- ];
142+ $ regions = [];
143+ foreach ($ regionList as $ region ) {
144+ $ regions [] = new DataObject ($ region );
145+ }
139146 $ regionIterator = new \ArrayIterator ($ regions );
140147
141148 $ this ->_regionCollection ->expects (
142149 $ this ->once ()
143150 )->method (
144151 'addCountryFilter '
145152 )->with (
146- [ ' Country1 ' , ' Country2 ' ]
153+ $ countryIds
147154 )->willReturnSelf ();
148155 $ this ->_regionCollection ->expects ($ this ->once ())->method ('load ' );
149156 $ this ->_regionCollection ->expects (
@@ -153,15 +160,6 @@ public function testGetRegionJson()
153160 )->willReturn (
154161 $ regionIterator
155162 );
156-
157- $ expectedDataToEncode = [
158- 'config ' => ['show_all_regions ' => false , 'regions_required ' => []],
159- 'Country1 ' => [
160- 'r1 ' => ['code ' => 'r1-code ' , 'name ' => 'r1-name ' ],
161- 'r2 ' => ['code ' => 'r2-code ' , 'name ' => 'r2-name ' ]
162- ],
163- 'Country2 ' => ['r3 ' => ['code ' => 'r3-code ' , 'name ' => 'r3-name ' ]]
164- ];
165163 $ this ->jsonHelperMock ->expects (
166164 $ this ->once ()
167165 )->method (
@@ -177,6 +175,75 @@ public function testGetRegionJson()
177175 $ this ->assertEquals ('encoded_json ' , $ result );
178176 }
179177
178+ /**
179+ * @return array
180+ */
181+ public function getRegionJsonDataProvider (): array
182+ {
183+ return [
184+ [
185+ 'Country1,Country2 ' ,
186+ [
187+ 'Country1 ' ,
188+ 'Country2 ' ,
189+ ],
190+ [
191+ [
192+ 'country_id ' => 'Country1 ' ,
193+ 'region_id ' => 'r1 ' ,
194+ 'code ' => 'r1-code ' ,
195+ 'name ' => 'r1-name ' ,
196+ ],
197+ [
198+ 'country_id ' => 'Country1 ' ,
199+ 'region_id ' => 'r2 ' ,
200+ 'code ' => 'r2-code ' ,
201+ 'name ' => 'r2-name ' ,
202+ ],
203+ [
204+ 'country_id ' => 'Country2 ' ,
205+ 'region_id ' => 'r3 ' ,
206+ 'code ' => 'r3-code ' ,
207+ 'name ' => 'r3-name ' ,
208+ ],
209+ ],
210+ [
211+ 'config ' => [
212+ 'show_all_regions ' => false ,
213+ 'regions_required ' => [],
214+ ],
215+ 'Country1 ' => [
216+ 'r1 ' => [
217+ 'code ' => 'r1-code ' ,
218+ 'name ' => 'r1-name ' ,
219+ ],
220+ 'r2 ' => [
221+ 'code ' => 'r2-code ' ,
222+ 'name ' => 'r2-name ' ,
223+ ],
224+ ],
225+ 'Country2 ' => [
226+ 'r3 ' => [
227+ 'code ' => 'r3-code ' ,
228+ 'name ' => 'r3-name ' ,
229+ ]
230+ ],
231+ ],
232+ ],
233+ [
234+ null ,
235+ ['' ],
236+ [],
237+ [
238+ 'config ' => [
239+ 'show_all_regions ' => false ,
240+ 'regions_required ' => [],
241+ ],
242+ ],
243+ ],
244+ ];
245+ }
246+
180247 /**
181248 * @param string $configValue
182249 * @param mixed $expected
0 commit comments