77
88namespace Magento \Directory \Test \Unit \Helper ;
99
10- use ArrayIterator ;
1110use Magento \Directory \Helper \Data ;
1211use Magento \Directory \Model \AllowedCountries ;
1312use Magento \Directory \Model \CurrencyFactory ;
@@ -53,16 +52,6 @@ class DataTest extends TestCase
5352 */
5453 protected $ _store ;
5554
56- /**
57- * @var RequestInterface|MockObject
58- */
59- protected $ _request ;
60-
61- /**
62- * @var StoreManagerInterface|MockObject
63- */
64- protected $ _storeManager ;
65-
6655 /**
6756 * @var ScopeConfigInterface|MockObject
6857 */
@@ -78,10 +67,10 @@ protected function setUp(): void
7867 $ objectManager = new ObjectManager ($ this );
7968 $ this ->scopeConfigMock = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
8069 $ this ->scopeConfigMock ->expects ($ this ->any ())->method ('isSetFlag ' )->willReturn (false );
81- $ this -> _request = $ this ->getMockForAbstractClass (RequestInterface::class);
70+ $ requestMock = $ this ->getMockForAbstractClass (RequestInterface::class);
8271 $ context = $ this ->createMock (Context::class);
8372 $ context ->method ('getRequest ' )
84- ->willReturn ($ this -> _request );
73+ ->willReturn ($ requestMock );
8574 $ context ->expects ($ this ->any ())
8675 ->method ('getScopeConfig ' )
8776 ->willReturn ($ this ->scopeConfigMock );
@@ -105,7 +94,8 @@ protected function setUp(): void
10594 $ this ->jsonHelperMock = $ this ->createMock (JsonDataHelper::class);
10695
10796 $ this ->_store = $ this ->createMock (Store::class);
108- $ this ->_storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
97+ $ storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
98+ $ storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ this ->_store );
10999
110100 $ currencyFactory = $ this ->createMock (CurrencyFactory::class);
111101
@@ -115,151 +105,45 @@ protected function setUp(): void
115105 'countryCollection ' => $ this ->_countryCollection ,
116106 'regCollectionFactory ' => $ regCollectionFactory ,
117107 'jsonHelper ' => $ this ->jsonHelperMock ,
118- 'storeManager ' => $ this -> _storeManager ,
108+ 'storeManager ' => $ storeManager ,
119109 'currencyFactory ' => $ currencyFactory ,
120110 ];
121111 $ this ->_object = $ objectManager ->getObject (Data::class, $ arguments );
122112 }
123113
124- /**
125- * @return array
126- */
127- public function regionJsonProvider (): array
128- {
129- $ countries = [
130- 'Country1 ' => [
131- 'r1 ' => ['code ' => 'r1-code ' , 'name ' => 'r1-name ' ],
132- 'r2 ' => ['code ' => 'r2-code ' , 'name ' => 'r2-name ' ]
133- ],
134- 'Country2 ' => [
135- 'r3 ' => ['code ' => 'r3-code ' , 'name ' => 'r3-name ' ],
136- ],
137- 'Country3 ' => [],
138- ];
139-
140- return [
141- [
142- null ,
143- $ countries ,
144- ],
145- [
146- null ,
147- [
148- 'Country1 ' => $ countries ['Country1 ' ],
149- ],
150- [ScopeInterface::SCOPE_WEBSITE => 1 ],
151- ],
152- [
153- 1 ,
154- [
155- 'Country2 ' => $ countries ['Country2 ' ],
156- ],
157- ],
158- [
159- null ,
160- [
161- 'Country2 ' => $ countries ['Country2 ' ],
162- ],
163- [
164- ScopeInterface::SCOPE_WEBSITE => null ,
165- ScopeInterface::SCOPE_STORE => 1 ,
166- ],
167- ],
168- [
169- 2 ,
170- [
171- 'Country3 ' => $ countries ['Country3 ' ],
172- ],
173- ],
174- [
175- null ,
176- [
177- 'Country3 ' => $ countries ['Country3 ' ],
178- ],
179- [ScopeInterface::SCOPE_STORE => 2 ],
180- ],
181- ];
182- }
183-
184- /**
185- * @param int|null $currentStoreId
186- * @param array $allowedCountries
187- * @param array $requestParams
188- * @dataProvider regionJsonProvider
189- */
190- public function testGetRegionJson (?int $ currentStoreId , array $ allowedCountries , array $ requestParams = [])
114+ public function testGetRegionJson ()
191115 {
192- if ($ currentStoreId ) {
193- $ this ->_store ->method ('getId ' )->willReturn ($ currentStoreId );
194- $ this ->_storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ this ->_store );
195- } else {
196- $ this ->_storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn (null );
197- }
198-
199- if ($ requestParams ) {
200- $ map = [];
201-
202- foreach ($ requestParams as $ name => $ value ) {
203- $ map [] = [$ name , null , $ value ];
204- }
205-
206- $ this ->_request
207- ->method ('getParam ' )
208- ->willReturnMap ($ map );
209- }
210-
211- $ expectedDataToEncode = array_merge ([
212- 'config ' => ['show_all_regions ' => false , 'regions_required ' => []],
213- ], array_filter ($ allowedCountries ));
214-
215116 $ this ->scopeConfigMock ->method ('getValue ' )
216117 ->willReturnMap (
217118 [
218119 [
219120 AllowedCountries::ALLOWED_COUNTRIES_PATH ,
220121 ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
221122 null ,
222- 'Country1,Country2,Country3 '
223- ],
224- [
225- AllowedCountries::ALLOWED_COUNTRIES_PATH ,
226- ScopeInterface::SCOPE_WEBSITE ,
227- 1 ,
228- 'Country1 '
229- ],
230- [
231- AllowedCountries::ALLOWED_COUNTRIES_PATH ,
232- ScopeInterface::SCOPE_STORE ,
233- 1 ,
234- 'Country2 '
235- ],
236- [
237- AllowedCountries::ALLOWED_COUNTRIES_PATH ,
238- ScopeInterface::SCOPE_STORE ,
239- 2 ,
240- 'Country3 '
123+ 'Country1,Country2 '
241124 ],
242125 [Data::XML_PATH_STATES_REQUIRED , ScopeInterface::SCOPE_STORE , null , '' ]
243126 ]
244127 );
245-
246128 $ regions = [
247- new DataObject (['country_id ' => 'Country1 ' , 'region_id ' => 'r1 ' , 'code ' => 'r1-code ' , 'name ' => 'r1-name ' ]),
248- new DataObject (['country_id ' => 'Country1 ' , 'region_id ' => 'r2 ' , 'code ' => 'r2-code ' , 'name ' => 'r2-name ' ]),
249- new DataObject (['country_id ' => 'Country2 ' , 'region_id ' => 'r3 ' , 'code ' => 'r3-code ' , 'name ' => 'r3-name ' ]),
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+ )
250138 ];
251- $ regions = array_filter ($ regions , function (DataObject $ region ) use ($ allowedCountries ) {
252- return array_key_exists ($ region ->getData ('country_id ' ), $ allowedCountries );
253- });
254-
255- $ regionIterator = new ArrayIterator ($ regions );
139+ $ regionIterator = new \ArrayIterator ($ regions );
256140
257141 $ this ->_regionCollection ->expects (
258142 $ this ->once ()
259143 )->method (
260144 'addCountryFilter '
261145 )->with (
262- array_keys ( $ allowedCountries )
146+ [ ' Country1 ' , ' Country2 ' ]
263147 )->willReturnSelf ();
264148 $ this ->_regionCollection ->expects ($ this ->once ())->method ('load ' );
265149 $ this ->_regionCollection ->expects (
@@ -270,6 +154,14 @@ public function testGetRegionJson(?int $currentStoreId, array $allowedCountries,
270154 $ regionIterator
271155 );
272156
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+ ];
273165 $ this ->jsonHelperMock ->expects (
274166 $ this ->once ()
275167 )->method (
0 commit comments