Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAddressTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class AbstractAddressTest extends \PHPUnit\Framework\TestCase
15 {
17  protected $contextMock;
18 
20  protected $registryMock;
21 
23  protected $directoryDataMock;
24 
26  protected $eavConfigMock;
27 
29  protected $addressConfigMock;
30 
32  protected $regionFactoryMock;
33 
36 
38  protected $resourceMock;
39 
42 
44  protected $model;
45 
47  private $objectManager;
48 
50  private $compositeValidatorMock;
51 
52  protected function setUp()
53  {
54  $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
55  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
56  $this->directoryDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
57  $this->eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
58  $this->addressConfigMock = $this->createMock(\Magento\Customer\Model\Address\Config::class);
59  $this->regionFactoryMock = $this->createPartialMock(\Magento\Directory\Model\RegionFactory::class, ['create']);
60  $this->countryFactoryMock = $this->createPartialMock(
61  \Magento\Directory\Model\CountryFactory::class,
62  ['create']
63  );
64  $regionCollectionMock = $this->createMock(\Magento\Directory\Model\ResourceModel\Region\Collection::class);
65  $regionCollectionMock->expects($this->any())
66  ->method('getSize')
67  ->will($this->returnValue(0));
68  $countryMock = $this->createMock(\Magento\Directory\Model\Country::class);
69  $countryMock->expects($this->any())
70  ->method('getRegionCollection')
71  ->will($this->returnValue($regionCollectionMock));
72  $this->countryFactoryMock->expects($this->any())
73  ->method('create')
74  ->will($this->returnValue($countryMock));
75 
76  $this->resourceMock = $this->createMock(\Magento\Customer\Model\ResourceModel\Customer::class);
77  $this->resourceCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
78  ->disableOriginalConstructor()
79  ->getMockForAbstractClass();
80  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
81  $this->compositeValidatorMock = $this->createMock(CompositeValidator::class);
82  $this->model = $this->objectManager->getObject(
83  \Magento\Customer\Model\Address\AbstractAddress::class,
84  [
85  'context' => $this->contextMock,
86  'registry' => $this->registryMock,
87  'directoryData' => $this->directoryDataMock,
88  'eavConfig' => $this->eavConfigMock,
89  'addressConfig' => $this->addressConfigMock,
90  'regionFactory' => $this->regionFactoryMock,
91  'countryFactory' => $this->countryFactoryMock,
92  'resource' => $this->resourceMock,
93  'resourceCollection' => $this->resourceCollectionMock,
94  'compositeValidator' => $this->compositeValidatorMock,
95  ]
96  );
97  }
98 
99  public function testGetRegionWithRegionId()
100  {
101  $countryId = 1;
102  $this->prepareGetRegion($countryId);
103 
104  $this->model->setData('region_id', 1);
105  $this->model->setData('region', '');
106  $this->model->setData('country_id', $countryId);
107  $this->assertEquals('RegionName', $this->model->getRegion());
108  }
109 
110  public function testGetRegionWithRegion()
111  {
112  $countryId = 2;
113  $this->prepareGetRegion($countryId);
114 
115  $this->model->setData('region_id', '');
116  $this->model->setData('region', 2);
117  $this->model->setData('country_id', $countryId);
118  $this->assertEquals('RegionName', $this->model->getRegion());
119  }
120 
121  public function testGetRegionWithRegionName()
122  {
123  $this->regionFactoryMock->expects($this->never())->method('create');
124 
125  $this->model->setData('region_id', '');
126  $this->model->setData('region', 'RegionName');
127  $this->assertEquals('RegionName', $this->model->getRegion());
128  }
129 
130  public function testGetRegionWithoutRegion()
131  {
132  $this->regionFactoryMock->expects($this->never())->method('create');
133 
134  $this->assertNull($this->model->getRegion());
135  }
136 
138  {
139  $countryId = 1;
140  $this->prepareGetRegionCode($countryId);
141 
142  $this->model->setData('region_id', 3);
143  $this->model->setData('region', '');
144  $this->model->setData('country_id', $countryId);
145  $this->assertEquals('UK', $this->model->getRegionCode());
146  }
147 
148  public function testGetRegionCodeWithRegion()
149  {
150  $countryId = 2;
151  $this->prepareGetRegionCode($countryId);
152 
153  $this->model->setData('region_id', '');
154  $this->model->setData('region', 4);
155  $this->model->setData('country_id', $countryId);
156  $this->assertEquals('UK', $this->model->getRegionCode());
157  }
158 
160  {
161  $this->regionFactoryMock->expects($this->never())->method('create');
162 
163  $this->model->setData('region_id', '');
164  $this->model->setData('region', 'UK');
165  $this->assertEquals('UK', $this->model->getRegionCode());
166  }
167 
169  {
170  $this->regionFactoryMock->expects($this->never())->method('create');
171 
172  $this->assertNull($this->model->getRegionCode());
173  }
174 
178  protected function prepareGetRegion($countryId, $regionName = 'RegionName')
179  {
180  $region = $this->createPartialMock(
181  \Magento\Directory\Model\Region::class,
182  ['getCountryId', 'getName', '__wakeup', 'load']
183  );
184  $region->expects($this->once())
185  ->method('getName')
186  ->will($this->returnValue($regionName));
187  $region->expects($this->once())
188  ->method('getCountryId')
189  ->will($this->returnValue($countryId));
190  $this->regionFactoryMock->expects($this->once())
191  ->method('create')
192  ->will($this->returnValue($region));
193  }
194 
198  protected function prepareGetRegionCode($countryId, $regionCode = 'UK')
199  {
200  $region = $this->createPartialMock(
201  \Magento\Directory\Model\Region::class,
202  ['getCountryId', 'getCode', '__wakeup', 'load']
203  );
204  $region->expects($this->once())
205  ->method('getCode')
206  ->will($this->returnValue($regionCode));
207  $region->expects($this->once())
208  ->method('getCountryId')
209  ->will($this->returnValue($countryId));
210  $this->regionFactoryMock->expects($this->once())
211  ->method('create')
212  ->will($this->returnValue($region));
213  }
214 
220  public function testSetData()
221  {
222  $key = [
223  'key' => 'value'
224  ];
225 
226  $this->model->setData($key);
227  $this->assertEquals($key, $this->model->getData());
228  }
229 
236  {
237  $expected = [
238  'key' => 'value',
239  'street' => 'value1',
240  ];
241 
242  $key = [
243  'key' => 'value',
244  'street' => [
245  'key1' => 'value1',
246  ]
247  ];
248 
249  $this->model->setData($key);
250  $this->assertEquals($expected, $this->model->getData());
251  }
252 
258  public function testSetDataWithValue()
259  {
260  $value = [
261  'street' => 'value',
262  ];
263 
264  $this->model->setData('street', $value);
265  $this->assertEquals($value, $this->model->getData());
266  }
267 
273  public function testSetDataWithObject()
274  {
275  $value = [
276  'key' => new \Magento\Framework\DataObject(),
277  ];
278  $expected = [
279  'key' => [
280  'key' => new \Magento\Framework\DataObject()
281  ]
282  ];
283  $this->model->setData('key', $value);
284  $this->assertEquals($expected, $this->model->getData());
285  }
286 
294  public function testValidate(array $data, $expected)
295  {
296  $this->compositeValidatorMock->method('validate')->with($this->model)->willReturn($expected);
297 
298  foreach ($data as $key => $value) {
299  $this->model->setData($key, $value);
300  }
301 
302  $actual = $this->model->validate();
303  $this->assertEquals($expected, $actual);
304  }
305 
309  public function validateDataProvider()
310  {
311  $countryId = 1;
312  $data = [
313  'firstname' => 'First Name',
314  'lastname' => 'Last Name',
315  'street' => "Street 1\nStreet 2",
316  'city' => 'Odessa',
317  'telephone' => '555-55-55',
318  'country_id' => $countryId,
319  'postcode' => 07201,
320  'region_id' => 1,
321  'company' => 'Magento',
322  'fax' => '222-22-22'
323  ];
324  return [
325  'firstname' => [
326  array_merge(array_diff_key($data, ['firstname' => '']), ['country_id' => $countryId++]),
327  ['"firstname" is required. Enter and try again.'],
328  ],
329  'lastname' => [
330  array_merge(array_diff_key($data, ['lastname' => '']), ['country_id' => $countryId++]),
331  ['"lastname" is required. Enter and try again.'],
332  ],
333  'street' => [
334  array_merge(array_diff_key($data, ['street' => '']), ['country_id' => $countryId++]),
335  ['"street" is required. Enter and try again.'],
336  ],
337  'city' => [
338  array_merge(array_diff_key($data, ['city' => '']), ['country_id' => $countryId++]),
339  ['"city" is required. Enter and try again.'],
340  ],
341  'telephone' => [
342  array_merge(array_diff_key($data, ['telephone' => '']), ['country_id' => $countryId++]),
343  ['"telephone" is required. Enter and try again.'],
344  ],
345  'postcode' => [
346  array_merge(array_diff_key($data, ['postcode' => '']), ['country_id' => $countryId++]),
347  ['"postcode" is required. Enter and try again.'],
348  ],
349  'region_id' => [
350  array_merge($data, ['country_id' => $countryId++, 'region_id' => 2]),
351  ['Invalid value of "2" provided for the regionId field.'],
352  ],
353  'country_id' => [
354  array_diff_key($data, ['country_id' => '']),
355  ['"countryId" is required. Enter and try again.'],
356  ],
357  'validated' => [array_merge($data, ['country_id' => $countryId++]), true],
358  ];
359  }
360 
364  public function testGetStreetFullAlwaysReturnsString($expectedResult, $street)
365  {
366  $this->model->setData('street', $street);
367  $this->assertEquals($expectedResult, $this->model->getStreetFull());
368  }
369 
373  public function testSetDataStreetAlwaysConvertedToString($expectedResult, $street)
374  {
375  $this->model->setData('street', $street);
376  $this->assertEquals($expectedResult, $this->model->getData('street'));
377  }
378 
382  public function getStreetFullDataProvider()
383  {
384  return [
385  [null, null],
386  ['', []],
387  ["first line\nsecond line", ['first line', 'second line']],
388  ['single line', ['single line']],
389  ['single line', 'single line'],
390  ];
391  }
392 
393  protected function tearDown()
394  {
395  $this->objectManager->setBackwardCompatibleProperty(
396  $this->model,
397  '_countryModels',
398  []
399  );
400  }
401 }
$value
Definition: gender.phtml:16