77
88namespace Magento \Directory \Test \Unit \Helper ;
99
10+ use ArrayIterator ;
1011use Magento \Directory \Helper \Data ;
1112use Magento \Directory \Model \AllowedCountries ;
1213use Magento \Directory \Model \CurrencyFactory ;
@@ -52,6 +53,16 @@ class DataTest extends TestCase
5253 */
5354 protected $ _store ;
5455
56+ /**
57+ * @var RequestInterface|MockObject
58+ */
59+ protected $ _request ;
60+
61+ /**
62+ * @var StoreManagerInterface|MockObject
63+ */
64+ protected $ _storeManager ;
65+
5566 /**
5667 * @var ScopeConfigInterface|MockObject
5768 */
@@ -67,10 +78,10 @@ protected function setUp(): void
6778 $ objectManager = new ObjectManager ($ this );
6879 $ this ->scopeConfigMock = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
6980 $ this ->scopeConfigMock ->expects ($ this ->any ())->method ('isSetFlag ' )->willReturn (false );
70- $ requestMock = $ this ->getMockForAbstractClass (RequestInterface::class);
81+ $ this -> _request = $ this ->getMockForAbstractClass (RequestInterface::class);
7182 $ context = $ this ->createMock (Context::class);
7283 $ context ->method ('getRequest ' )
73- ->willReturn ($ requestMock );
84+ ->willReturn ($ this -> _request );
7485 $ context ->expects ($ this ->any ())
7586 ->method ('getScopeConfig ' )
7687 ->willReturn ($ this ->scopeConfigMock );
@@ -94,8 +105,7 @@ protected function setUp(): void
94105 $ this ->jsonHelperMock = $ this ->createMock (JsonDataHelper::class);
95106
96107 $ this ->_store = $ this ->createMock (Store::class);
97- $ storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
98- $ storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ this ->_store );
108+ $ this ->_storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
99109
100110 $ currencyFactory = $ this ->createMock (CurrencyFactory::class);
101111
@@ -105,22 +115,129 @@ protected function setUp(): void
105115 'countryCollection ' => $ this ->_countryCollection ,
106116 'regCollectionFactory ' => $ regCollectionFactory ,
107117 'jsonHelper ' => $ this ->jsonHelperMock ,
108- 'storeManager ' => $ storeManager ,
118+ 'storeManager ' => $ this -> _storeManager ,
109119 'currencyFactory ' => $ currencyFactory ,
110120 ];
111121 $ this ->_object = $ objectManager ->getObject (Data::class, $ arguments );
112122 }
113123
114- public function testGetRegionJson ()
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 = [])
115191 {
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+
116215 $ this ->scopeConfigMock ->method ('getValue ' )
117216 ->willReturnMap (
118217 [
119218 [
120219 AllowedCountries::ALLOWED_COUNTRIES_PATH ,
121220 ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
122221 null ,
123- 'Country1,Country2 '
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 '
124241 ],
125242 [Data::XML_PATH_STATES_REQUIRED , ScopeInterface::SCOPE_STORE , null , '' ]
126243 ]
@@ -136,14 +253,16 @@ public function testGetRegionJson()
136253 ['country_id ' => 'Country2 ' , 'region_id ' => 'r3 ' , 'code ' => 'r3-code ' , 'name ' => 'r3-name ' ]
137254 )
138255 ];
139- $ regionIterator = new \ArrayIterator ($ regions );
256+ $ regionIterator = new ArrayIterator (array_filter ($ regions , function (DataObject $ region ) use ($ allowedCountries ) {
257+ return array_key_exists ($ region ->getData ('country_id ' ), $ allowedCountries );
258+ }));
140259
141260 $ this ->_regionCollection ->expects (
142261 $ this ->once ()
143262 )->method (
144263 'addCountryFilter '
145264 )->with (
146- [ ' Country1 ' , ' Country2 ' ]
265+ array_keys ( $ allowedCountries )
147266 )->willReturnSelf ();
148267 $ this ->_regionCollection ->expects ($ this ->once ())->method ('load ' );
149268 $ this ->_regionCollection ->expects (
@@ -154,14 +273,6 @@ public function testGetRegionJson()
154273 $ regionIterator
155274 );
156275
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- ];
165276 $ this ->jsonHelperMock ->expects (
166277 $ this ->once ()
167278 )->method (
0 commit comments