AttributeMetadatConverterTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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;
  7. use Magento\Customer\Api\Data\OptionInterfaceFactory;
  8. use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
  9. use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
  10. use Magento\Customer\Model\AttributeMetadataConverter;
  11. /**
  12. * Class AttributeMetadataConverterTest
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. * @package Magento\Customer\Test\Unit\Model
  15. */
  16. class AttributeMetadatConverterTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var OptionInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $optionFactory;
  22. /**
  23. * @var ValidationRuleInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $validationRuleFactory;
  26. /**
  27. * @var AttributeMetadataInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $attributeMetadataFactory;
  30. /**
  31. * @var \Magento\Framework\Api\DataObjectHelper | \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $dataObjectHelper;
  34. /** @var AttributeMetadataConverter */
  35. private $model;
  36. /** @var \Magento\Customer\Model\Attribute | \PHPUnit_Framework_MockObject_MockObject */
  37. private $attribute;
  38. public function setUp()
  39. {
  40. $this->optionFactory = $this->getMockBuilder(OptionInterfaceFactory::class)
  41. ->setMethods(['create'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->validationRuleFactory = $this->getMockBuilder(ValidationRuleInterfaceFactory::class)
  45. ->setMethods(['create'])
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->attributeMetadataFactory = $this->getMockBuilder(AttributeMetadataInterfaceFactory::class)
  49. ->setMethods(['create'])
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->model = new AttributeMetadataConverter(
  59. $this->optionFactory,
  60. $this->validationRuleFactory,
  61. $this->attributeMetadataFactory,
  62. $this->dataObjectHelper
  63. );
  64. }
  65. /**
  66. * @return array
  67. */
  68. private function prepareValidateRules()
  69. {
  70. return [
  71. 'one' => 'numeric',
  72. 'two' => 'alphanumeric'
  73. ];
  74. }
  75. /**
  76. * @return array
  77. */
  78. private function prepareOptions()
  79. {
  80. return [
  81. [
  82. 'label' => 'few_values',
  83. 'value' => [
  84. [1], [2]
  85. ]
  86. ],
  87. [
  88. 'label' => 'one_value',
  89. 'value' => 1
  90. ]
  91. ];
  92. }
  93. public function testCreateAttributeMetadataTestWithSource()
  94. {
  95. $validatedRules = $this->prepareValidateRules();
  96. $options = $this->prepareOptions();
  97. $optionDataObjectForSimpleValue1 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $optionDataObjectForSimpleValue2 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $optionObject1 = $this->createMock(\Magento\Customer\Api\Data\OptionInterface::class);
  104. $optionObject2 = $this->createMock(\Magento\Customer\Api\Data\OptionInterface::class);
  105. $this->optionFactory->expects($this->exactly(4))
  106. ->method('create')
  107. ->will(
  108. $this->onConsecutiveCalls(
  109. $optionDataObjectForSimpleValue2,
  110. $optionObject1,
  111. $optionObject2,
  112. $optionDataObjectForSimpleValue1
  113. )
  114. );
  115. $source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $source->expects($this->once())
  119. ->method('getAllOptions')
  120. ->willReturn($options);
  121. $this->attribute->expects($this->once())
  122. ->method('usesSource')
  123. ->willReturn(true);
  124. $this->attribute->expects($this->once())
  125. ->method('getSource')
  126. ->willReturn($source);
  127. $optionDataObjectForSimpleValue1->expects($this->once())
  128. ->method('setValue')
  129. ->with(1);
  130. $optionDataObjectForSimpleValue2->expects($this->once())
  131. ->method('setLabel')
  132. ->with('few_values');
  133. $optionDataObjectForSimpleValue1->expects($this->once())
  134. ->method('setLabel')
  135. ->with('one_value');
  136. $this->dataObjectHelper->expects($this->exactly(2))
  137. ->method('populateWithArray')
  138. ->withConsecutive(
  139. [$optionObject1, ['1'], \Magento\Customer\Api\Data\OptionInterface::class],
  140. [$optionObject2, ['2'], \Magento\Customer\Api\Data\OptionInterface::class]
  141. );
  142. $validationRule1 = $this->createMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
  143. $validationRule2 = $this->createMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
  144. $this->validationRuleFactory->expects($this->exactly(2))
  145. ->method('create')
  146. ->will($this->onConsecutiveCalls($validationRule1, $validationRule2));
  147. $validationRule1->expects($this->once())
  148. ->method('setValue')
  149. ->with('numeric');
  150. $validationRule1->expects($this->once())
  151. ->method('setName')
  152. ->with('one')
  153. ->willReturnSelf();
  154. $validationRule2->expects($this->once())
  155. ->method('setValue')
  156. ->with('alphanumeric');
  157. $validationRule2->expects($this->once())
  158. ->method('setName')
  159. ->with('two')
  160. ->willReturnSelf();
  161. $mockMethods = ['setAttributeCode', 'setFrontendInput'];
  162. $attributeMetaData = $this->getMockBuilder(\Magento\Customer\Model\Data\AttributeMetadata::class)
  163. ->setMethods($mockMethods)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. foreach ($mockMethods as $method) {
  167. $attributeMetaData->expects($this->once())->method($method)->willReturnSelf();
  168. }
  169. $this->attribute->expects($this->once())
  170. ->method('getValidateRules')
  171. ->willReturn($validatedRules);
  172. $this->attributeMetadataFactory->expects($this->once())
  173. ->method('create')
  174. ->willReturn($attributeMetaData);
  175. $frontend = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend::class)
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $this->attribute->expects($this->once())
  179. ->method('getFrontend')
  180. ->willReturn($frontend);
  181. $optionDataObjectForSimpleValue2->expects($this->once())
  182. ->method('setOptions')
  183. ->with([$optionObject1, $optionObject2]);
  184. $this->model->createMetadataAttribute($this->attribute);
  185. }
  186. }