AbstractAddressTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Address;
  7. use Magento\Customer\Model\Address\CompositeValidator;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class AbstractAddressTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $contextMock;
  15. /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $registryMock;
  17. /** @var \Magento\Directory\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $directoryDataMock;
  19. /** @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $eavConfigMock;
  21. /** @var \Magento\Customer\Model\Address\Config|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $addressConfigMock;
  23. /** @var \Magento\Directory\Model\RegionFactory|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $regionFactoryMock;
  25. /** @var \Magento\Directory\Model\CountryFactory|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $countryFactoryMock;
  27. /** @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $resourceMock;
  29. /** @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $resourceCollectionMock;
  31. /** @var \Magento\Customer\Model\Address\AbstractAddress */
  32. protected $model;
  33. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  34. private $objectManager;
  35. /** @var \Magento\Customer\Model\Address\CompositeValidator|\PHPUnit_Framework_MockObject_MockObject */
  36. private $compositeValidatorMock;
  37. protected function setUp()
  38. {
  39. $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  40. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  41. $this->directoryDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
  42. $this->eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
  43. $this->addressConfigMock = $this->createMock(\Magento\Customer\Model\Address\Config::class);
  44. $this->regionFactoryMock = $this->createPartialMock(\Magento\Directory\Model\RegionFactory::class, ['create']);
  45. $this->countryFactoryMock = $this->createPartialMock(
  46. \Magento\Directory\Model\CountryFactory::class,
  47. ['create']
  48. );
  49. $regionCollectionMock = $this->createMock(\Magento\Directory\Model\ResourceModel\Region\Collection::class);
  50. $regionCollectionMock->expects($this->any())
  51. ->method('getSize')
  52. ->will($this->returnValue(0));
  53. $countryMock = $this->createMock(\Magento\Directory\Model\Country::class);
  54. $countryMock->expects($this->any())
  55. ->method('getRegionCollection')
  56. ->will($this->returnValue($regionCollectionMock));
  57. $this->countryFactoryMock->expects($this->any())
  58. ->method('create')
  59. ->will($this->returnValue($countryMock));
  60. $this->resourceMock = $this->createMock(\Magento\Customer\Model\ResourceModel\Customer::class);
  61. $this->resourceCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
  62. ->disableOriginalConstructor()
  63. ->getMockForAbstractClass();
  64. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  65. $this->compositeValidatorMock = $this->createMock(CompositeValidator::class);
  66. $this->model = $this->objectManager->getObject(
  67. \Magento\Customer\Model\Address\AbstractAddress::class,
  68. [
  69. 'context' => $this->contextMock,
  70. 'registry' => $this->registryMock,
  71. 'directoryData' => $this->directoryDataMock,
  72. 'eavConfig' => $this->eavConfigMock,
  73. 'addressConfig' => $this->addressConfigMock,
  74. 'regionFactory' => $this->regionFactoryMock,
  75. 'countryFactory' => $this->countryFactoryMock,
  76. 'resource' => $this->resourceMock,
  77. 'resourceCollection' => $this->resourceCollectionMock,
  78. 'compositeValidator' => $this->compositeValidatorMock,
  79. ]
  80. );
  81. }
  82. public function testGetRegionWithRegionId()
  83. {
  84. $countryId = 1;
  85. $this->prepareGetRegion($countryId);
  86. $this->model->setData('region_id', 1);
  87. $this->model->setData('region', '');
  88. $this->model->setData('country_id', $countryId);
  89. $this->assertEquals('RegionName', $this->model->getRegion());
  90. }
  91. public function testGetRegionWithRegion()
  92. {
  93. $countryId = 2;
  94. $this->prepareGetRegion($countryId);
  95. $this->model->setData('region_id', '');
  96. $this->model->setData('region', 2);
  97. $this->model->setData('country_id', $countryId);
  98. $this->assertEquals('RegionName', $this->model->getRegion());
  99. }
  100. public function testGetRegionWithRegionName()
  101. {
  102. $this->regionFactoryMock->expects($this->never())->method('create');
  103. $this->model->setData('region_id', '');
  104. $this->model->setData('region', 'RegionName');
  105. $this->assertEquals('RegionName', $this->model->getRegion());
  106. }
  107. public function testGetRegionWithoutRegion()
  108. {
  109. $this->regionFactoryMock->expects($this->never())->method('create');
  110. $this->assertNull($this->model->getRegion());
  111. }
  112. public function testGetRegionCodeWithRegionId()
  113. {
  114. $countryId = 1;
  115. $this->prepareGetRegionCode($countryId);
  116. $this->model->setData('region_id', 3);
  117. $this->model->setData('region', '');
  118. $this->model->setData('country_id', $countryId);
  119. $this->assertEquals('UK', $this->model->getRegionCode());
  120. }
  121. public function testGetRegionCodeWithRegion()
  122. {
  123. $countryId = 2;
  124. $this->prepareGetRegionCode($countryId);
  125. $this->model->setData('region_id', '');
  126. $this->model->setData('region', 4);
  127. $this->model->setData('country_id', $countryId);
  128. $this->assertEquals('UK', $this->model->getRegionCode());
  129. }
  130. public function testGetRegionCodeWithRegionName()
  131. {
  132. $this->regionFactoryMock->expects($this->never())->method('create');
  133. $this->model->setData('region_id', '');
  134. $this->model->setData('region', 'UK');
  135. $this->assertEquals('UK', $this->model->getRegionCode());
  136. }
  137. public function testGetRegionCodeWithoutRegion()
  138. {
  139. $this->regionFactoryMock->expects($this->never())->method('create');
  140. $this->assertNull($this->model->getRegionCode());
  141. }
  142. /**
  143. * @param $countryId
  144. */
  145. protected function prepareGetRegion($countryId, $regionName = 'RegionName')
  146. {
  147. $region = $this->createPartialMock(
  148. \Magento\Directory\Model\Region::class,
  149. ['getCountryId', 'getName', '__wakeup', 'load']
  150. );
  151. $region->expects($this->once())
  152. ->method('getName')
  153. ->will($this->returnValue($regionName));
  154. $region->expects($this->once())
  155. ->method('getCountryId')
  156. ->will($this->returnValue($countryId));
  157. $this->regionFactoryMock->expects($this->once())
  158. ->method('create')
  159. ->will($this->returnValue($region));
  160. }
  161. /**
  162. * @param $countryId
  163. */
  164. protected function prepareGetRegionCode($countryId, $regionCode = 'UK')
  165. {
  166. $region = $this->createPartialMock(
  167. \Magento\Directory\Model\Region::class,
  168. ['getCountryId', 'getCode', '__wakeup', 'load']
  169. );
  170. $region->expects($this->once())
  171. ->method('getCode')
  172. ->will($this->returnValue($regionCode));
  173. $region->expects($this->once())
  174. ->method('getCountryId')
  175. ->will($this->returnValue($countryId));
  176. $this->regionFactoryMock->expects($this->once())
  177. ->method('create')
  178. ->will($this->returnValue($region));
  179. }
  180. /**
  181. * Test for setData method
  182. *
  183. * @return void
  184. */
  185. public function testSetData()
  186. {
  187. $key = [
  188. 'key' => 'value'
  189. ];
  190. $this->model->setData($key);
  191. $this->assertEquals($key, $this->model->getData());
  192. }
  193. /**
  194. * Test for setData method with multidimensional array in "key" argument
  195. *
  196. * @return void
  197. */
  198. public function testSetDataWithMultidimensionalArray()
  199. {
  200. $expected = [
  201. 'key' => 'value',
  202. 'street' => 'value1',
  203. ];
  204. $key = [
  205. 'key' => 'value',
  206. 'street' => [
  207. 'key1' => 'value1',
  208. ]
  209. ];
  210. $this->model->setData($key);
  211. $this->assertEquals($expected, $this->model->getData());
  212. }
  213. /**
  214. * Test for setData method with "value" argument
  215. *
  216. * @return void
  217. */
  218. public function testSetDataWithValue()
  219. {
  220. $value = [
  221. 'street' => 'value',
  222. ];
  223. $this->model->setData('street', $value);
  224. $this->assertEquals($value, $this->model->getData());
  225. }
  226. /**
  227. * Test for setData method with "value" argument
  228. *
  229. * @return void
  230. */
  231. public function testSetDataWithObject()
  232. {
  233. $value = [
  234. 'key' => new \Magento\Framework\DataObject(),
  235. ];
  236. $expected = [
  237. 'key' => [
  238. 'key' => new \Magento\Framework\DataObject()
  239. ]
  240. ];
  241. $this->model->setData('key', $value);
  242. $this->assertEquals($expected, $this->model->getData());
  243. }
  244. /**
  245. * @param array $data
  246. * @param array|bool $expected
  247. * @return void
  248. *
  249. * @dataProvider validateDataProvider
  250. */
  251. public function testValidate(array $data, $expected)
  252. {
  253. $this->compositeValidatorMock->method('validate')->with($this->model)->willReturn($expected);
  254. foreach ($data as $key => $value) {
  255. $this->model->setData($key, $value);
  256. }
  257. $actual = $this->model->validate();
  258. $this->assertEquals($expected, $actual);
  259. }
  260. /**
  261. * @return array
  262. */
  263. public function validateDataProvider()
  264. {
  265. $countryId = 1;
  266. $data = [
  267. 'firstname' => 'First Name',
  268. 'lastname' => 'Last Name',
  269. 'street' => "Street 1\nStreet 2",
  270. 'city' => 'Odessa',
  271. 'telephone' => '555-55-55',
  272. 'country_id' => $countryId,
  273. 'postcode' => 07201,
  274. 'region_id' => 1,
  275. 'company' => 'Magento',
  276. 'fax' => '222-22-22'
  277. ];
  278. return [
  279. 'firstname' => [
  280. array_merge(array_diff_key($data, ['firstname' => '']), ['country_id' => $countryId++]),
  281. ['"firstname" is required. Enter and try again.'],
  282. ],
  283. 'lastname' => [
  284. array_merge(array_diff_key($data, ['lastname' => '']), ['country_id' => $countryId++]),
  285. ['"lastname" is required. Enter and try again.'],
  286. ],
  287. 'street' => [
  288. array_merge(array_diff_key($data, ['street' => '']), ['country_id' => $countryId++]),
  289. ['"street" is required. Enter and try again.'],
  290. ],
  291. 'city' => [
  292. array_merge(array_diff_key($data, ['city' => '']), ['country_id' => $countryId++]),
  293. ['"city" is required. Enter and try again.'],
  294. ],
  295. 'telephone' => [
  296. array_merge(array_diff_key($data, ['telephone' => '']), ['country_id' => $countryId++]),
  297. ['"telephone" is required. Enter and try again.'],
  298. ],
  299. 'postcode' => [
  300. array_merge(array_diff_key($data, ['postcode' => '']), ['country_id' => $countryId++]),
  301. ['"postcode" is required. Enter and try again.'],
  302. ],
  303. 'region_id' => [
  304. array_merge($data, ['country_id' => $countryId++, 'region_id' => 2]),
  305. ['Invalid value of "2" provided for the regionId field.'],
  306. ],
  307. 'country_id' => [
  308. array_diff_key($data, ['country_id' => '']),
  309. ['"countryId" is required. Enter and try again.'],
  310. ],
  311. 'validated' => [array_merge($data, ['country_id' => $countryId++]), true],
  312. ];
  313. }
  314. /**
  315. * @dataProvider getStreetFullDataProvider
  316. */
  317. public function testGetStreetFullAlwaysReturnsString($expectedResult, $street)
  318. {
  319. $this->model->setData('street', $street);
  320. $this->assertEquals($expectedResult, $this->model->getStreetFull());
  321. }
  322. /**
  323. * @dataProvider getStreetFullDataProvider
  324. */
  325. public function testSetDataStreetAlwaysConvertedToString($expectedResult, $street)
  326. {
  327. $this->model->setData('street', $street);
  328. $this->assertEquals($expectedResult, $this->model->getData('street'));
  329. }
  330. /**
  331. * @return array
  332. */
  333. public function getStreetFullDataProvider()
  334. {
  335. return [
  336. [null, null],
  337. ['', []],
  338. ["first line\nsecond line", ['first line', 'second line']],
  339. ['single line', ['single line']],
  340. ['single line', 'single line'],
  341. ];
  342. }
  343. protected function tearDown()
  344. {
  345. $this->objectManager->setBackwardCompatibleProperty(
  346. $this->model,
  347. '_countryModels',
  348. []
  349. );
  350. }
  351. }