Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
AbstractAddressTest Class Reference
Inheritance diagram for AbstractAddressTest:

Public Member Functions

 testGetRegionWithRegionId ()
 
 testGetRegionWithRegion ()
 
 testGetRegionWithRegionName ()
 
 testGetRegionWithoutRegion ()
 
 testGetRegionCodeWithRegionId ()
 
 testGetRegionCodeWithRegion ()
 
 testGetRegionCodeWithRegionName ()
 
 testGetRegionCodeWithoutRegion ()
 
 testSetData ()
 
 testSetDataWithMultidimensionalArray ()
 
 testSetDataWithValue ()
 
 testSetDataWithObject ()
 
 testValidate (array $data, $expected)
 
 validateDataProvider ()
 
 testGetStreetFullAlwaysReturnsString ($expectedResult, $street)
 
 testSetDataStreetAlwaysConvertedToString ($expectedResult, $street)
 
 getStreetFullDataProvider ()
 

Protected Member Functions

 setUp ()
 
 prepareGetRegion ($countryId, $regionName='RegionName')
 
 prepareGetRegionCode ($countryId, $regionCode='UK')
 
 tearDown ()
 

Protected Attributes

 $contextMock
 
 $registryMock
 
 $directoryDataMock
 
 $eavConfigMock
 
 $addressConfigMock
 
 $regionFactoryMock
 
 $countryFactoryMock
 
 $resourceMock
 
 $resourceCollectionMock
 
 $model
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 14 of file AbstractAddressTest.php.

Member Function Documentation

◆ getStreetFullDataProvider()

getStreetFullDataProvider ( )
Returns
array

Definition at line 382 of file AbstractAddressTest.php.

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  }

◆ prepareGetRegion()

prepareGetRegion (   $countryId,
  $regionName = 'RegionName' 
)
protected
Parameters
$countryId

Definition at line 178 of file AbstractAddressTest.php.

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  }

◆ prepareGetRegionCode()

prepareGetRegionCode (   $countryId,
  $regionCode = 'UK' 
)
protected
Parameters
$countryId

Definition at line 198 of file AbstractAddressTest.php.

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  }

◆ setUp()

setUp ( )
protected

Definition at line 52 of file AbstractAddressTest.php.

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  }

◆ tearDown()

tearDown ( )
protected

Definition at line 393 of file AbstractAddressTest.php.

394  {
395  $this->objectManager->setBackwardCompatibleProperty(
396  $this->model,
397  '_countryModels',
398  []
399  );
400  }

◆ testGetRegionCodeWithoutRegion()

testGetRegionCodeWithoutRegion ( )

Definition at line 168 of file AbstractAddressTest.php.

169  {
170  $this->regionFactoryMock->expects($this->never())->method('create');
171 
172  $this->assertNull($this->model->getRegionCode());
173  }

◆ testGetRegionCodeWithRegion()

testGetRegionCodeWithRegion ( )

Definition at line 148 of file AbstractAddressTest.php.

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  }

◆ testGetRegionCodeWithRegionId()

testGetRegionCodeWithRegionId ( )

Definition at line 137 of file AbstractAddressTest.php.

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  }

◆ testGetRegionCodeWithRegionName()

testGetRegionCodeWithRegionName ( )

Definition at line 159 of file AbstractAddressTest.php.

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  }

◆ testGetRegionWithoutRegion()

testGetRegionWithoutRegion ( )

Definition at line 130 of file AbstractAddressTest.php.

131  {
132  $this->regionFactoryMock->expects($this->never())->method('create');
133 
134  $this->assertNull($this->model->getRegion());
135  }

◆ testGetRegionWithRegion()

testGetRegionWithRegion ( )

Definition at line 110 of file AbstractAddressTest.php.

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  }

◆ testGetRegionWithRegionId()

testGetRegionWithRegionId ( )

Definition at line 99 of file AbstractAddressTest.php.

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  }

◆ testGetRegionWithRegionName()

testGetRegionWithRegionName ( )

Definition at line 121 of file AbstractAddressTest.php.

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  }

◆ testGetStreetFullAlwaysReturnsString()

testGetStreetFullAlwaysReturnsString (   $expectedResult,
  $street 
)

@dataProvider getStreetFullDataProvider

Definition at line 364 of file AbstractAddressTest.php.

365  {
366  $this->model->setData('street', $street);
367  $this->assertEquals($expectedResult, $this->model->getStreetFull());
368  }

◆ testSetData()

testSetData ( )

Test for setData method

Returns
void

Definition at line 220 of file AbstractAddressTest.php.

221  {
222  $key = [
223  'key' => 'value'
224  ];
225 
226  $this->model->setData($key);
227  $this->assertEquals($key, $this->model->getData());
228  }

◆ testSetDataStreetAlwaysConvertedToString()

testSetDataStreetAlwaysConvertedToString (   $expectedResult,
  $street 
)

@dataProvider getStreetFullDataProvider

Definition at line 373 of file AbstractAddressTest.php.

374  {
375  $this->model->setData('street', $street);
376  $this->assertEquals($expectedResult, $this->model->getData('street'));
377  }

◆ testSetDataWithMultidimensionalArray()

testSetDataWithMultidimensionalArray ( )

Test for setData method with multidimensional array in "key" argument

Returns
void

Definition at line 235 of file AbstractAddressTest.php.

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  }

◆ testSetDataWithObject()

testSetDataWithObject ( )

Test for setData method with "value" argument

Returns
void

Definition at line 273 of file AbstractAddressTest.php.

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  }
$value
Definition: gender.phtml:16

◆ testSetDataWithValue()

testSetDataWithValue ( )

Test for setData method with "value" argument

Returns
void

Definition at line 258 of file AbstractAddressTest.php.

259  {
260  $value = [
261  'street' => 'value',
262  ];
263 
264  $this->model->setData('street', $value);
265  $this->assertEquals($value, $this->model->getData());
266  }
$value
Definition: gender.phtml:16

◆ testValidate()

testValidate ( array  $data,
  $expected 
)
Parameters
array$data
array | bool$expected
Returns
void

@dataProvider validateDataProvider

Definition at line 294 of file AbstractAddressTest.php.

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  }
$value
Definition: gender.phtml:16

◆ validateDataProvider()

validateDataProvider ( )
Returns
array

Definition at line 309 of file AbstractAddressTest.php.

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  }

Field Documentation

◆ $addressConfigMock

$addressConfigMock
protected

Definition at line 29 of file AbstractAddressTest.php.

◆ $contextMock

$contextMock
protected

Definition at line 17 of file AbstractAddressTest.php.

◆ $countryFactoryMock

$countryFactoryMock
protected

Definition at line 35 of file AbstractAddressTest.php.

◆ $directoryDataMock

$directoryDataMock
protected

Definition at line 23 of file AbstractAddressTest.php.

◆ $eavConfigMock

$eavConfigMock
protected

Definition at line 26 of file AbstractAddressTest.php.

◆ $model

$model
protected

Definition at line 44 of file AbstractAddressTest.php.

◆ $regionFactoryMock

$regionFactoryMock
protected

Definition at line 32 of file AbstractAddressTest.php.

◆ $registryMock

$registryMock
protected

Definition at line 20 of file AbstractAddressTest.php.

◆ $resourceCollectionMock

$resourceCollectionMock
protected

Definition at line 41 of file AbstractAddressTest.php.

◆ $resourceMock

$resourceMock
protected

Definition at line 38 of file AbstractAddressTest.php.


The documentation for this class was generated from the following file: