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 use Magento\Directory\Helper\Data as HelperData;
11 use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
20 
24 class DataTest extends \PHPUnit\Framework\TestCase
25 {
27  private $block;
28 
30  private $contextMock;
31 
33  private $helperDataMock;
34 
36  private $cacheTypeConfigMock;
37 
39  private $countryCollectionFactoryMock;
40 
42  private $scopeConfigMock;
43 
45  private $storeManagerMock;
46 
48  private $storeMock;
49 
51  private $countryCollectionMock;
52 
54  private $layoutMock;
55 
57  private $serializerMock;
58 
59  protected function setUp()
60  {
61  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
62  $this->prepareContext();
63 
64  $this->helperDataMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->cacheTypeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Config::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71 
72  $this->prepareCountryCollection();
73 
74  $this->block = $objectManagerHelper->getObject(
75  Data::class,
76  [
77  'context' => $this->contextMock,
78  'directoryHelper' => $this->helperDataMock,
79  'configCacheType' => $this->cacheTypeConfigMock,
80  'countryCollectionFactory' => $this->countryCollectionFactoryMock
81  ]
82  );
83 
84  $this->serializerMock = $this->createMock(SerializerInterface::class);
85  $objectManagerHelper->setBackwardCompatibleProperty(
86  $this->block,
87  'serializer',
88  $this->serializerMock
89  );
90  }
91 
92  protected function prepareContext()
93  {
94  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97 
98  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
99  ->getMockForAbstractClass();
100 
101  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
102  ->getMockForAbstractClass();
103 
104  $this->storeManagerMock->expects($this->any())
105  ->method('getStore')
106  ->willReturn($this->storeMock);
107 
108  $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
109  ->getMockForAbstractClass();
110 
111  $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $this->contextMock->expects($this->any())
116  ->method('getScopeConfig')
117  ->willReturn($this->scopeConfigMock);
118 
119  $this->contextMock->expects($this->any())
120  ->method('getStoreManager')
121  ->willReturn($this->storeManagerMock);
122 
123  $this->contextMock->expects($this->any())
124  ->method('getLayout')
125  ->willReturn($this->layoutMock);
126  }
127 
128  protected function prepareCountryCollection()
129  {
130  $this->countryCollectionMock = $this->getMockBuilder(
131  \Magento\Directory\Model\ResourceModel\Country\Collection::class
132  )->disableOriginalConstructor()->getMock();
133 
134  $this->countryCollectionFactoryMock = $this->getMockBuilder(
135  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class
136  )
137  ->disableOriginalConstructor()
138  ->setMethods([
139  'create'
140  ])
141  ->getMock();
142 
143  $this->countryCollectionFactoryMock->expects($this->any())
144  ->method('create')
145  ->willReturn($this->countryCollectionMock);
146  }
147 
157  public function testGetCountryHtmlSelect(
158  $storeCode,
159  $defaultCountry,
160  $destinations,
161  $expectedDestinations,
162  $options,
163  $resultHtml
164  ) {
165  $this->helperDataMock->expects($this->once())
166  ->method('getDefaultCountry')
167  ->willReturn($defaultCountry);
168 
169  $this->storeMock->expects($this->once())
170  ->method('getCode')
171  ->willReturn($storeCode);
172 
173  $this->serializerMock->expects($this->once())
174  ->method('serialize')
175  ->willReturn('serializedData');
176 
177  $this->cacheTypeConfigMock->expects($this->once())
178  ->method('load')
179  ->with('DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode)
180  ->willReturn(false);
181  $this->cacheTypeConfigMock->expects($this->once())
182  ->method('save')
183  ->with('serializedData', 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode)
184  ->willReturnSelf();
185 
186  $this->scopeConfigMock->expects($this->once())
187  ->method('getValue')
188  ->with('general/country/destinations', ScopeInterface::SCOPE_STORE)
189  ->willReturn($destinations);
190 
191  $this->countryCollectionMock->expects($this->once())
192  ->method('loadByStore')
193  ->willReturnSelf();
194  $this->countryCollectionMock->expects($this->any())
195  ->method('setForegroundCountries')
196  ->with($expectedDestinations)
197  ->willReturnSelf();
198  $this->countryCollectionMock->expects($this->once())
199  ->method('toOptionArray')
200  ->willReturn($options);
201 
202  $elementHtmlSelect = $this->mockElementHtmlSelect($defaultCountry, $options, $resultHtml);
203 
204  $this->layoutMock->expects($this->once())
205  ->method('createBlock')
206  ->willReturn($elementHtmlSelect);
207 
208  $this->assertEquals($resultHtml, $this->block->getCountryHtmlSelect());
209  }
210 
221  {
222  return [
223  [
224  'default',
225  1,
226  '',
227  [],
228  [
229  [
230  'value' => 'US',
231  'label' => 'United States',
232  ],
233  ],
234  'result html',
235  ],
236  [
237  'default',
238  1,
239  'US',
240  [
241  0 => 'US',
242  ],
243  [
244  [
245  'value' => 'US',
246  'label' => 'United States',
247  ],
248  ],
249  'result html',
250  ],
251  [
252  'default',
253  1,
254  'US,GB',
255  [
256  0 => 'US',
257  1 => 'GB',
258  ],
259  [
260  [
261  'value' => 'US',
262  'label' => 'United States',
263  ],
264  [
265  'value' => 'GB',
266  'label' => 'Great Britain',
267  ],
268  ],
269  'result html',
270  ],
271  ];
272  }
273 
280  protected function mockElementHtmlSelect($defaultCountry, $options, $resultHtml)
281  {
282  $name = 'country_id';
283  $id = 'country';
284  $title = 'Country';
285 
286  $elementHtmlSelect = $this->getMockBuilder(\Magento\Framework\View\Element\Html\Select::class)
287  ->disableOriginalConstructor()
288  ->setMethods([
289  'setName',
290  'setId',
291  'setTitle',
292  'setValue',
293  'setOptions',
294  'setExtraParams',
295  'getHtml',
296  ])
297  ->getMock();
298 
299  $elementHtmlSelect->expects($this->once())
300  ->method('setName')
301  ->with($name)
302  ->willReturnSelf();
303  $elementHtmlSelect->expects($this->once())
304  ->method('setId')
305  ->with($id)
306  ->willReturnSelf();
307  $elementHtmlSelect->expects($this->once())
308  ->method('setTitle')
309  ->with(__($title))
310  ->willReturnSelf();
311  $elementHtmlSelect->expects($this->once())
312  ->method('setValue')
313  ->with($defaultCountry)
314  ->willReturnSelf();
315  $elementHtmlSelect->expects($this->once())
316  ->method('setOptions')
317  ->with($options)
318  ->willReturnSelf();
319  $elementHtmlSelect->expects($this->once())
320  ->method('setExtraParams')
321  ->with('data-validate="{\'validate-select\':true}"')
322  ->willReturnSelf();
323  $elementHtmlSelect->expects($this->once())
324  ->method('getHtml')
325  ->willReturn($resultHtml);
326 
327  return $elementHtmlSelect;
328  }
329 }
$title
Definition: default.phtml:14
testGetCountryHtmlSelect( $storeCode, $defaultCountry, $destinations, $expectedDestinations, $options, $resultHtml)
Definition: DataTest.php:157
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$storeCode
Definition: indexer.php:15
mockElementHtmlSelect($defaultCountry, $options, $resultHtml)
Definition: DataTest.php:280
if(!isset($_GET['name'])) $name
Definition: log.php:14