DataProviderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Test\Unit\Model\Address;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Model\Address\DataProvider;
  10. use Magento\Customer\Model\AttributeMetadataResolver;
  11. use Magento\Customer\Model\FileUploaderDataResolver;
  12. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
  13. use Magento\Customer\Model\ResourceModel\Address\Collection as AddressCollection;
  14. use Magento\Eav\Model\Config;
  15. use Magento\Eav\Model\Entity\Type;
  16. use Magento\Customer\Api\Data\CustomerInterface;
  17. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  18. use Magento\Customer\Model\Address as AddressModel;
  19. use Magento\Ui\Component\Form\Element\Multiline;
  20. use Magento\Ui\Component\Form\Field;
  21. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  22. /**
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class DataProviderTest extends \PHPUnit\Framework\TestCase
  26. {
  27. private const ATTRIBUTE_CODE = 'street';
  28. /**
  29. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $addressCollectionFactory;
  32. /**
  33. * @var AddressCollection|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $collection;
  36. /**
  37. * @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $customerRepository;
  40. /**
  41. * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $customer;
  44. /**
  45. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $eavConfig;
  48. /*
  49. * @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $context;
  52. /**
  53. * @var AddressModel|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $address;
  56. /**
  57. * @var FileUploaderDataResolver|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $fileUploaderDataResolver;
  60. /**
  61. * @var AttributeMetadataResolver|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $attributeMetadataResolver;
  64. /**
  65. * @var DataProvider
  66. */
  67. private $model;
  68. protected function setUp()
  69. {
  70. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  71. $this->fileUploaderDataResolver = $this->getMockBuilder(FileUploaderDataResolver::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->attributeMetadataResolver = $this->getMockBuilder(AttributeMetadataResolver::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->addressCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(['create'])
  80. ->getMock();
  81. $this->collection = $this->getMockBuilder(AddressCollection::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->customerRepository = $this->getMockForAbstractClass(CustomerRepositoryInterface::class);
  85. $this->context = $this->getMockForAbstractClass(ContextInterface::class);
  86. $this->addressCollectionFactory->expects($this->once())
  87. ->method('create')
  88. ->willReturn($this->collection);
  89. $this->eavConfig = $this->getMockBuilder(Config::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->eavConfig->expects($this->once())
  93. ->method('getEntityType')
  94. ->with('customer_address')
  95. ->willReturn($this->getTypeAddressMock([]));
  96. $this->customer = $this->getMockForAbstractClass(CustomerInterface::class);
  97. $this->address = $this->getMockBuilder(AddressModel::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->attributeMetadataResolver->expects($this->at(0))
  101. ->method('getAttributesMeta')
  102. ->willReturn(
  103. [
  104. 'arguments' => [
  105. 'data' => [
  106. 'config' => [
  107. 'dataType' => Multiline::NAME,
  108. 'formElement' => 'frontend_input',
  109. 'options' => 'test-options',
  110. 'visible' => null,
  111. 'required' => 'is_required',
  112. 'label' => __('Street'),
  113. 'sortOrder' => 'sort_order',
  114. 'default' => 'default_value',
  115. 'size' => 'multiline_count',
  116. 'componentType' => Field::NAME,
  117. ],
  118. ],
  119. ],
  120. ]
  121. );
  122. $this->attributeMetadataResolver->expects($this->at(1))
  123. ->method('getAttributesMeta')
  124. ->willReturn(
  125. [
  126. 'arguments' => [
  127. 'data' => [
  128. 'config' => [
  129. 'dataType' => 'frontend_input',
  130. 'formElement' => 'frontend_input',
  131. 'visible' => null,
  132. 'required' => 'is_required',
  133. 'label' => __('frontend_label'),
  134. 'sortOrder' => 'sort_order',
  135. 'default' => 'default_value',
  136. 'size' => 'multiline_count',
  137. 'componentType' => Field::NAME,
  138. 'prefer' => 'toggle',
  139. 'valueMap' => [
  140. 'true' => 1,
  141. 'false' => 0,
  142. ],
  143. ],
  144. ],
  145. ],
  146. ]
  147. );
  148. $this->model = $objectManagerHelper->getObject(
  149. DataProvider::class,
  150. [
  151. 'name' => 'test-name',
  152. 'primaryFieldName' => 'primary-field-name',
  153. 'requestFieldName' => 'request-field-name',
  154. 'addressCollectionFactory' => $this->addressCollectionFactory,
  155. 'customerRepository' => $this->customerRepository,
  156. 'eavConfig' => $this->eavConfig,
  157. 'context' => $this->context,
  158. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver,
  159. 'attributeMetadataResolver' => $this->attributeMetadataResolver,
  160. [],
  161. [],
  162. true
  163. ]
  164. );
  165. }
  166. public function testGetDefaultData(): void
  167. {
  168. $expectedData = [
  169. '' => [
  170. 'parent_id' => 1,
  171. 'firstname' => 'John',
  172. 'lastname' => 'Doe'
  173. ]
  174. ];
  175. $this->collection->expects($this->once())
  176. ->method('getItems')
  177. ->willReturn([]);
  178. $this->context->expects($this->once())
  179. ->method('getRequestParam')
  180. ->willReturn(1);
  181. $this->customerRepository->expects($this->once())
  182. ->method('getById')
  183. ->willReturn($this->customer);
  184. $this->customer->expects($this->once())
  185. ->method('getFirstname')
  186. ->willReturn('John');
  187. $this->customer->expects($this->once())
  188. ->method('getLastname')
  189. ->willReturn('Doe');
  190. $this->assertEquals($expectedData, $this->model->getData());
  191. }
  192. public function testGetData(): void
  193. {
  194. $expectedData = [
  195. '1' => [
  196. 'parent_id' => '1',
  197. 'default_billing' => '1',
  198. 'default_shipping' => '1',
  199. 'firstname' => 'John',
  200. 'lastname' => 'Doe',
  201. 'street' => [
  202. '42000 Ave W 55 Cedar City',
  203. 'Apt. 33'
  204. ]
  205. ]
  206. ];
  207. $this->collection->expects($this->once())
  208. ->method('getItems')
  209. ->willReturn([
  210. $this->address
  211. ]);
  212. $this->customerRepository->expects($this->once())
  213. ->method('getById')
  214. ->willReturn($this->customer);
  215. $this->customer->expects($this->once())
  216. ->method('getDefaultBilling')
  217. ->willReturn('1');
  218. $this->customer->expects($this->once())
  219. ->method('getDefaultShipping')
  220. ->willReturn('1');
  221. $this->address->expects($this->once())
  222. ->method('getEntityId')
  223. ->willReturn('1');
  224. $this->address->expects($this->once())
  225. ->method('load')
  226. ->with('1')
  227. ->willReturnSelf();
  228. $this->address->expects($this->once())
  229. ->method('getData')
  230. ->willReturn([
  231. 'parent_id' => '1',
  232. 'firstname' => 'John',
  233. 'lastname' => 'Doe',
  234. 'street' => "42000 Ave W 55 Cedar City\nApt. 33"
  235. ]);
  236. $this->fileUploaderDataResolver->expects($this->once())
  237. ->method('overrideFileUploaderData')
  238. ->willReturnSelf();
  239. $this->assertEquals($expectedData, $this->model->getData());
  240. }
  241. /**
  242. * Get customer address type mock
  243. *
  244. * @param array $customerAttributes
  245. * @return Type|\PHPUnit_Framework_MockObject_MockObject
  246. */
  247. protected function getTypeAddressMock($customerAttributes = [])
  248. {
  249. $typeAddressMock = $this->getMockBuilder(Type::class)
  250. ->disableOriginalConstructor()
  251. ->getMock();
  252. $attributesCollection = !empty($customerAttributes) ? $customerAttributes : $this->getAttributeMock();
  253. foreach ($attributesCollection as $attribute) {
  254. $attribute->expects($this->any())
  255. ->method('getEntityType')
  256. ->willReturn($typeAddressMock);
  257. }
  258. $typeAddressMock->expects($this->once())
  259. ->method('getAttributeCollection')
  260. ->willReturn($attributesCollection);
  261. return $typeAddressMock;
  262. }
  263. /**
  264. * Get attribute mock
  265. *
  266. * @param array $options
  267. * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
  268. */
  269. protected function getAttributeMock($options = []): array
  270. {
  271. $attributeMock = $this->getMockBuilder(AbstractAttribute::class)
  272. ->setMethods(
  273. [
  274. 'getAttributeCode',
  275. 'getDataUsingMethod',
  276. 'getFrontendInput',
  277. 'getIsVisible',
  278. 'getSource',
  279. 'getIsUserDefined',
  280. 'getUsedInForms',
  281. 'getEntityType',
  282. ]
  283. )
  284. ->disableOriginalConstructor()
  285. ->getMockForAbstractClass();
  286. $attributeCode = self::ATTRIBUTE_CODE;
  287. if (isset($options[self::ATTRIBUTE_CODE]['specific_code_prefix'])) {
  288. $attributeCode .= $options[self::ATTRIBUTE_CODE]['specific_code_prefix'];
  289. }
  290. $attributeMock->expects($this->exactly(2))
  291. ->method('getAttributeCode')
  292. ->willReturn($attributeCode);
  293. $attributeBooleanMock = $this->getMockBuilder(AbstractAttribute::class)
  294. ->setMethods(
  295. [
  296. 'getAttributeCode',
  297. 'getDataUsingMethod',
  298. 'getFrontendInput',
  299. 'getIsVisible',
  300. 'getIsUserDefined',
  301. 'getUsedInForms',
  302. 'getSource',
  303. 'getEntityType',
  304. ]
  305. )
  306. ->disableOriginalConstructor()
  307. ->getMockForAbstractClass();
  308. $booleanAttributeCode = 'test-code-boolean';
  309. if (isset($options['test-code-boolean']['specific_code_prefix'])) {
  310. $booleanAttributeCode .= $options['test-code-boolean']['specific_code_prefix'];
  311. }
  312. $attributeBooleanMock->expects($this->exactly(2))
  313. ->method('getAttributeCode')
  314. ->willReturn($booleanAttributeCode);
  315. $mocks = [$attributeMock, $attributeBooleanMock];
  316. return $mocks;
  317. }
  318. }