18
18
use Magento \Framework \App \Helper \Context ;
19
19
use Magento \Framework \App \RequestInterface ;
20
20
use Magento \Framework \DataObject ;
21
+ use Magento \Framework \Exception \NoSuchEntityException ;
21
22
use Magento \Framework \Json \Helper \Data as JsonDataHelper ;
22
23
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
23
24
use Magento \Store \Model \ScopeInterface ;
@@ -111,39 +112,45 @@ protected function setUp(): void
111
112
$ this ->_object = $ objectManager ->getObject (Data::class, $ arguments );
112
113
}
113
114
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
+ ) {
116
130
$ this ->scopeConfigMock ->method ('getValue ' )
117
131
->willReturnMap (
118
132
[
119
133
[
120
134
AllowedCountries::ALLOWED_COUNTRIES_PATH ,
121
135
ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
122
136
null ,
123
- ' Country1,Country2 '
137
+ $ configValue
124
138
],
125
139
[Data::XML_PATH_STATES_REQUIRED , ScopeInterface::SCOPE_STORE , null , '' ]
126
140
]
127
141
);
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
+ }
139
146
$ regionIterator = new \ArrayIterator ($ regions );
140
147
141
148
$ this ->_regionCollection ->expects (
142
149
$ this ->once ()
143
150
)->method (
144
151
'addCountryFilter '
145
152
)->with (
146
- [ ' Country1 ' , ' Country2 ' ]
153
+ $ countryIds
147
154
)->willReturnSelf ();
148
155
$ this ->_regionCollection ->expects ($ this ->once ())->method ('load ' );
149
156
$ this ->_regionCollection ->expects (
@@ -153,15 +160,6 @@ public function testGetRegionJson()
153
160
)->willReturn (
154
161
$ regionIterator
155
162
);
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
- ];
165
163
$ this ->jsonHelperMock ->expects (
166
164
$ this ->once ()
167
165
)->method (
@@ -177,6 +175,75 @@ public function testGetRegionJson()
177
175
$ this ->assertEquals ('encoded_json ' , $ result );
178
176
}
179
177
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
+
180
247
/**
181
248
* @param string $configValue
182
249
* @param mixed $expected
0 commit comments