/** * Returns countries array * * @return array */ protected function _getCountries() { if (!$this->countries) { $restrictedCountries = $this->countrySource->getRestrictedCountries(); $this->countries = $this->countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountries])->loadData()->toOptionArray(false); } return $this->countries; }
public function testToOptionArray() { $excludedCountries = $this->model->getRestrictedCountries(); $countries = [['value' => 'US', 'lable' => 'United States'], ['value' => 'countryCode', 'lable' => 'countryName']]; $this->countryCollectionMock->expects($this->once())->method('addFieldToFilter')->with('country_id', ['nin' => $excludedCountries])->willReturnSelf(); $this->countryCollectionMock->expects($this->once())->method('loadData')->willReturnSelf(); $this->countryCollectionMock->expects($this->once())->method('toOptionArray')->willReturn($countries); $header = ['value' => '', 'label' => new \Magento\Framework\Phrase('--Please Select--')]; array_unshift($countries, $header); $this->assertEquals($countries, $this->model->toOptionArray()); }
/** * Loads country collection * * @return \Magento\Directory\Model\ResourceModel\Country\Collection */ public function getCountryCollection() { $collection = $this->getData('country_collection'); if ($collection == null) { $restrictedCountriesByBraintree = $this->countrySource->getRestrictedCountries(); $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); foreach ($collection as $item) { $countryCode = $item->getData('country_id'); if (!$this->config->canUseForCountry($countryCode)) { $restrictedCountriesByBraintree[] = $item->getData('country_id'); } } $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); $this->setData('country_collection', $collection); } return $collection; }