Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AllRegionTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AllRegionTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $countryCollection;
19 
23  protected $regionCollection;
24 
25  protected function setUp()
26  {
27  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28 
29  $countryCollectionFactory = $this->getMockBuilder(
30  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class
31  )->setMethods(['create', '__wakeup', '__sleep'])->disableOriginalConstructor()->getMock();
32 
33  $this->countryCollection = $this->getMockBuilder(
34  \Magento\Directory\Model\ResourceModel\Country\Collection::class
35  )->setMethods(['load', 'toOptionArray', '__wakeup', '__sleep'])
36  ->disableOriginalConstructor()
37  ->getMock();
38  $countryCollectionFactory->expects($this->once())
39  ->method('create')
40  ->will($this->returnValue($this->countryCollection));
41  $this->countryCollection->expects($this->once())
42  ->method('load')
43  ->will($this->returnSelf());
44 
45  $regionCollectionFactory = $this->getMockBuilder(
46  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class
47  )->disableOriginalConstructor()->setMethods(['create', '__wakeup', '__sleep'])->getMock();
48  $this->regionCollection = $this->getMockBuilder(\Magento\Directory\Model\ResourceModel\Region\Collection::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['load', 'getIterator', '__wakeup', '__sleep'])
51  ->getMock();
52  $regionCollectionFactory->expects($this->once())
53  ->method('create')
54  ->will($this->returnValue($this->regionCollection));
55  $this->regionCollection->expects($this->once())
56  ->method('load')
57  ->will($this->returnValue($this->regionCollection));
58 
59  $this->model = $objectManagerHelper->getObject(
60  \Magento\Directory\Model\Config\Source\Allregion::class,
61  [
62  'countryCollectionFactory' => $countryCollectionFactory,
63  'regionCollectionFactory' => $regionCollectionFactory
64  ]
65  );
66  }
67 
75  public function testToOptionArray($isMultiselect, $countries, $regions, $expectedResult)
76  {
77  $this->countryCollection->expects($this->once())
78  ->method('toOptionArray')
79  ->with(false)
80  ->will($this->returnValue(new \ArrayIterator($countries)));
81  $this->regionCollection->expects($this->once())
82  ->method('getIterator')
83  ->will($this->returnValue(new \ArrayIterator($regions)));
84 
85  $this->assertEquals($expectedResult, $this->model->toOptionArray($isMultiselect));
86  }
87 
93  public function toOptionArrayDataProvider()
94  {
95  return [
96  [
97  false,
98  [
99  $this->generateCountry('France', 'fr'),
100  ],
101  [
102  $this->generateRegion('fr', 1, 'Paris')
103  ],
104  [
105  [
106  'label' => '',
107  'value' => '',
108  ],
109  [
110  'label' => 'France',
111  'value' => [
112  [
113  'label' => 'Paris',
114  'value' => 1,
115  ],
116  ]
117  ]
118  ],
119  ],
120  [
121  true,
122  [
123  $this->generateCountry('France', 'fr'),
124  ],
125  [
126  $this->generateRegion('fr', 1, 'Paris'),
127  $this->generateRegion('fr', 2, 'Marseille')
128  ],
129  [
130  [
131  'label' => 'France',
132  'value' => [
133  [
134  'label' => 'Paris',
135  'value' => 1,
136  ],
137  [
138  'label' => 'Marseille',
139  'value' => 2
140  ],
141  ],
142  ]
143  ]
144  ],
145  [
146  true,
147  [
148  $this->generateCountry('France', 'fr'),
149  $this->generateCountry('Germany', 'de'),
150  ],
151  [
152  $this->generateRegion('fr', 1, 'Paris'),
153  $this->generateRegion('de', 2, 'Berlin')
154  ],
155  [
156  [
157  'label' => 'France',
158  'value' => [
159  [
160  'label' => 'Paris',
161  'value' => 1,
162  ],
163  ],
164  ],
165  [
166  'label' => 'Germany',
167  'value' => [
168  [
169  'label' => 'Berlin',
170  'value' => 2,
171  ],
172  ]
173  ]
174  ]
175  ],
176  ];
177  }
178 
186  private function generateCountry($countryLabel, $countryValue)
187  {
188  return [
189  'label' => $countryLabel,
190  'value' => $countryValue
191  ];
192  }
193 
202  private function generateRegion($countryId, $id, $defaultName)
203  {
204  $region = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
205  ->disableOriginalConstructor()
206  ->setMethods(['getCountryId', 'getId', 'getDefaultName', '__wakeup', '__sleep'])
207  ->getMock();
208  $region->expects($this->once())
209  ->method('getCountryId')
210  ->will($this->returnValue($countryId));
211  $region->expects($this->once())
212  ->method('getId')
213  ->will($this->returnValue($id));
214  $region->expects($this->once())
215  ->method('getDefaultName')
216  ->will($this->returnValue($defaultName));
217 
218  return $region;
219  }
220 }
$id
Definition: fieldset.phtml:14
testToOptionArray($isMultiselect, $countries, $regions, $expectedResult)