public function testToOptionArray() { $items = [['name' => 'Region Name 1', 'default_name' => 'Default Region Name 1', 'region_id' => 1, 'country_id' => 1], ['name' => 'Region Name 2', 'default_name' => 'Default Region Name 2', 'region_id' => 2, 'country_id' => 1]]; foreach ($items as $itemData) { $this->collection->addItem(new DataObject($itemData)); } $expectedResult = [['label' => __('Please select a region, state or province.'), 'value' => null, 'title' => null], ['value' => 1, 'title' => 'Default Region Name 1', 'country_id' => 1, 'label' => 'Region Name 1'], ['value' => 2, 'title' => 'Default Region Name 2', 'country_id' => 1, 'label' => 'Region Name 2']]; $this->assertEquals($expectedResult, $this->collection->toOptionArray()); }
/** * Process js Layout of block * * @param array $jsLayout * @return array * @SuppressWarnings(PHPMD.NPathComplexity) */ public function process($jsLayout) { $elements = ['city' => ['visible' => $this->isCityActive(), 'formElement' => 'input', 'label' => __('City'), 'value' => null], 'country_id' => ['visible' => true, 'formElement' => 'select', 'label' => __('Country'), 'options' => $this->countryCollection->load()->toOptionArray(), 'value' => null], 'region_id' => ['visible' => true, 'formElement' => 'select', 'label' => __('State/Province'), 'options' => $this->regionCollection->load()->toOptionArray(), 'value' => null], 'postcode' => ['visible' => true, 'formElement' => 'input', 'label' => __('Zip/Postal Code'), 'value' => null]]; if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets']['children'])) { $fieldSetPointer =& $jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets']['children']; $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer); } return $jsLayout; }
/** * Retrieve region collection * * @return \Magento\Directory\Model\ResourceModel\Region\Collection */ public function getRegionCollection() { if (!$this->_regionCollection) { $this->_regionCollection = $this->_regCollectionFactory->create(); $this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load(); } return $this->_regionCollection; }
/** * Return region code by id * * @param $regionId * @return string|null * @throws LocalizedException */ protected function getRegionCodeById($regionId) { if (!$regionId) { return null; } /** @var \Magento\Directory\Model\Region $region */ $region = $this->regionCollection->getItemById($regionId); if (!$region instanceof Region) { throw new LocalizedException(__('Region "%1" was not found.', [$regionId])); } return $region->getCode(); }
/** * @dataProvider toOptionArrayDataProvider * @param bool $isMultiselect * @param array $countries * @param array $regions * @param array $expectedResult */ public function testToOptionArray($isMultiselect, $countries, $regions, $expectedResult) { $this->countryCollection->expects($this->once())->method('toOptionArray')->with(false)->will($this->returnValue(new \ArrayIterator($countries))); $this->regionCollection->expects($this->once())->method('getIterator')->will($this->returnValue(new \ArrayIterator($regions))); $this->assertEquals($expectedResult, $this->model->toOptionArray($isMultiselect)); }