Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class DataTest extends \PHPUnit\Framework\TestCase
14 {
19 
23  protected $_regionCollection;
24 
28  protected $jsonHelperMock;
29 
33  protected $_store;
34 
38  protected $scopeConfigMock;
39 
43  protected $_object;
44 
45  protected function setUp()
46  {
47  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
49  $context = $this->createMock(\Magento\Framework\App\Helper\Context::class);
50  $context->expects($this->any())
51  ->method('getScopeConfig')
52  ->willReturn($this->scopeConfigMock);
53 
54  $configCacheType = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
55 
56  $this->_countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
57 
58  $this->_regionCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Region\Collection::class);
59  $regCollectionFactory = $this->createPartialMock(
60  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class,
61  ['create']
62  );
63  $regCollectionFactory->expects(
64  $this->any()
65  )->method(
66  'create'
67  )->will(
68  $this->returnValue($this->_regionCollection)
69  );
70 
71  $this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
72 
73  $this->_store = $this->createMock(\Magento\Store\Model\Store::class);
74  $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
75  $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->_store));
76 
77  $currencyFactory = $this->createMock(\Magento\Directory\Model\CurrencyFactory::class);
78 
79  $arguments = [
80  'context' => $context,
81  'configCacheType' => $configCacheType,
82  'countryCollection' => $this->_countryCollection,
83  'regCollectionFactory' => $regCollectionFactory,
84  'jsonHelper' => $this->jsonHelperMock,
85  'storeManager' => $storeManager,
86  'currencyFactory' => $currencyFactory,
87  ];
88  $this->_object = $objectManager->getObject(\Magento\Directory\Helper\Data::class, $arguments);
89  }
90 
91  public function testGetRegionJson()
92  {
93  $countries = [
94  new \Magento\Framework\DataObject(['country_id' => 'Country1']),
95  new \Magento\Framework\DataObject(['country_id' => 'Country2'])
96  ];
97  $countryIterator = new \ArrayIterator($countries);
98  $this->_countryCollection->expects(
99  $this->atLeastOnce()
100  )->method(
101  'getIterator'
102  )->will(
103  $this->returnValue($countryIterator)
104  );
105 
106  $regions = [
107  new \Magento\Framework\DataObject(
108  ['country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name']
109  ),
110  new \Magento\Framework\DataObject(
111  ['country_id' => 'Country1', 'region_id' => 'r2', 'code' => 'r2-code', 'name' => 'r2-name']
112  ),
113  new \Magento\Framework\DataObject(
114  ['country_id' => 'Country2', 'region_id' => 'r3', 'code' => 'r3-code', 'name' => 'r3-name']
115  )
116  ];
117  $regionIterator = new \ArrayIterator($regions);
118 
119  $this->_regionCollection->expects(
120  $this->once()
121  )->method(
122  'addCountryFilter'
123  )->with(
124  ['Country1', 'Country2']
125  )->will(
126  $this->returnSelf()
127  );
128  $this->_regionCollection->expects($this->once())->method('load');
129  $this->_regionCollection->expects(
130  $this->once()
131  )->method(
132  'getIterator'
133  )->will(
134  $this->returnValue($regionIterator)
135  );
136 
137  $expectedDataToEncode = [
138  'config' => ['show_all_regions' => false, 'regions_required' => []],
139  'Country1' => [
140  'r1' => ['code' => 'r1-code', 'name' => 'r1-name'],
141  'r2' => ['code' => 'r2-code', 'name' => 'r2-name']
142  ],
143  'Country2' => ['r3' => ['code' => 'r3-code', 'name' => 'r3-name']]
144  ];
145  $this->jsonHelperMock->expects(
146  $this->once()
147  )->method(
148  'jsonEncode'
149  )->with(
150  new \PHPUnit\Framework\Constraint\IsIdentical($expectedDataToEncode)
151  )->will(
152  $this->returnValue('encoded_json')
153  );
154 
155  // Test
156  $result = $this->_object->getRegionJson();
157  $this->assertEquals('encoded_json', $result);
158  }
159 
165  public function testGetCountriesWithStatesRequired($configValue, $expected)
166  {
167  $this->scopeConfigMock->expects(
168  $this->once()
169  )->method(
170  'getValue'
171  )->with(
172  'general/region/state_required'
173  )->will(
174  $this->returnValue($configValue)
175  );
176 
177  $result = $this->_object->getCountriesWithStatesRequired();
178  $this->assertEquals($expected, $result);
179  }
180 
186  public function testGetCountriesWithOptionalZip($configValue, $expected)
187  {
188  $this->scopeConfigMock->expects(
189  $this->once()
190  )->method(
191  'getValue'
192  )->with(
193  'general/country/optional_zip_countries'
194  )->will(
195  $this->returnValue($configValue)
196  );
197 
198  $result = $this->_object->getCountriesWithOptionalZip();
199  $this->assertEquals($expected, $result);
200  }
201 
205  public static function countriesCommaListDataProvider()
206  {
207  return [
208  'empty_list' => ['', []],
209  'normal_list' => ['Country1,Country2', ['Country1', 'Country2']]
210  ];
211  }
212 
213  public function testGetDefaultCountry()
214  {
215  $storeId = 'storeId';
216  $country = 'country';
217 
218  $this->scopeConfigMock->expects($this->once())
219  ->method('getValue')
220  ->with(
222  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
223  $storeId
224  )->will($this->returnValue($country));
225 
226  $this->assertEquals($country, $this->_object->getDefaultCountry($storeId));
227  }
228 
229  public function testGetCountryCollection()
230  {
231  $this->_countryCollection->expects(
232  $this->once()
233  )->method(
234  'isLoaded'
235  )->will(
236  $this->returnValue(0)
237  );
238 
239  $store = $this->createMock(\Magento\Store\Model\Store::class);
240  $this->_countryCollection->expects(
241  $this->once()
242  )->method(
243  'loadByStore'
244  )->with(
245  $store
246  );
247 
248  $this->_object->getCountryCollection($store);
249  }
250 
256  public function testGetTopCountryCodesReturnsParsedConfigurationValue($topCountriesValue, $expectedResult)
257  {
258  $this->scopeConfigMock->expects($this->once())
259  ->method('getValue')->with(\Magento\Directory\Helper\Data::XML_PATH_TOP_COUNTRIES)
260  ->willReturn($topCountriesValue);
261 
262  $this->assertEquals($expectedResult, $this->_object->getTopCountryCodes());
263  }
264 
268  public function topCountriesDataProvider()
269  {
270  return [
271  [null, []],
272  ['', []],
273  ['US', ['US']],
274  ['US,RU', ['US', 'RU']],
275  ];
276  }
277 }
$objectManager
Definition: bootstrap.php:17
$storeManager
testGetCountriesWithOptionalZip($configValue, $expected)
Definition: DataTest.php:186
$arguments
testGetCountriesWithStatesRequired($configValue, $expected)
Definition: DataTest.php:165
testGetTopCountryCodesReturnsParsedConfigurationValue($topCountriesValue, $expectedResult)
Definition: DataTest.php:256