PageRenderTime 77ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

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