AttributeMetadataHydratorTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Metadata;
  7. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
  9. use Magento\Customer\Api\Data\OptionInterface;
  10. use Magento\Customer\Api\Data\OptionInterfaceFactory;
  11. use Magento\Customer\Api\Data\ValidationRuleInterface;
  12. use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
  13. use Magento\Customer\Model\Data\AttributeMetadata;
  14. use Magento\Customer\Model\Data\Option;
  15. use Magento\Customer\Model\Data\ValidationRule;
  16. use Magento\Customer\Model\Metadata\AttributeMetadataHydrator;
  17. use Magento\Framework\Reflection\DataObjectProcessor;
  18. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class AttributeMetadataHydratorTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var AttributeMetadataInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $attributeMetadataFactoryMock;
  28. /**
  29. * @var OptionInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $optionFactoryMock;
  32. /**
  33. * @var ValidationRuleInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $validationRuleFactoryMock;
  36. /**
  37. * @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $attributeMetadataMock;
  40. /**
  41. * @var DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $dataObjectProcessorMock;
  44. /**
  45. * @var AttributeMetadataHydrator|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $attributeMetadataHydrator;
  48. protected function setUp()
  49. {
  50. $objectManager = new ObjectManager($this);
  51. $this->attributeMetadataFactoryMock = $this->createPartialMock(
  52. AttributeMetadataInterfaceFactory::class,
  53. ['create']
  54. );
  55. $this->optionFactoryMock = $this->createPartialMock(OptionInterfaceFactory::class, ['create']);
  56. $this->validationRuleFactoryMock = $this->createPartialMock(ValidationRuleInterfaceFactory::class, ['create']);
  57. $this->attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
  58. $this->dataObjectProcessorMock = $this->createMock(DataObjectProcessor::class);
  59. $this->attributeMetadataHydrator = $objectManager->getObject(
  60. AttributeMetadataHydrator::class,
  61. [
  62. 'attributeMetadataFactory' => $this->attributeMetadataFactoryMock,
  63. 'optionFactory' => $this->optionFactoryMock,
  64. 'validationRuleFactory' => $this->validationRuleFactoryMock,
  65. 'dataObjectProcessor' => $this->dataObjectProcessorMock
  66. ]
  67. );
  68. }
  69. /**
  70. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  71. */
  72. public function testHydrate()
  73. {
  74. $optionOneData = [
  75. 'label' => 'Label 1',
  76. 'options' => null
  77. ];
  78. $optionThreeData = [
  79. 'label' => 'Label 3',
  80. 'options' => null
  81. ];
  82. $optionFourData = [
  83. 'label' => 'Label 4',
  84. 'options' => null
  85. ];
  86. $optionTwoData = [
  87. 'label' => 'Label 2',
  88. 'options' => [$optionThreeData, $optionFourData]
  89. ];
  90. $validationRuleOneData = [
  91. 'name' => 'Name 1',
  92. 'value' => 'Value 1'
  93. ];
  94. $attributeMetadataData = [
  95. 'attribute_code' => 'attribute_code',
  96. 'frontend_input' => 'hidden',
  97. 'options' => [$optionOneData, $optionTwoData],
  98. 'validation_rules' => [$validationRuleOneData]
  99. ];
  100. $optionOne = new Option($optionOneData);
  101. $this->optionFactoryMock->expects($this->at(0))
  102. ->method('create')
  103. ->with(['data' => $optionOneData])
  104. ->willReturn($optionOne);
  105. $optionThree = new Option($optionThreeData);
  106. $this->optionFactoryMock->expects($this->at(1))
  107. ->method('create')
  108. ->with(['data' => $optionThreeData])
  109. ->willReturn($optionThree);
  110. $optionFour = new Option($optionFourData);
  111. $this->optionFactoryMock->expects($this->at(2))
  112. ->method('create')
  113. ->with(['data' => $optionFourData])
  114. ->willReturn($optionFour);
  115. $optionTwoDataPartiallyConverted = [
  116. 'label' => 'Label 2',
  117. 'options' => [$optionThree, $optionFour]
  118. ];
  119. $optionFour = new Option($optionTwoDataPartiallyConverted);
  120. $this->optionFactoryMock->expects($this->at(3))
  121. ->method('create')
  122. ->with(['data' => $optionTwoDataPartiallyConverted])
  123. ->willReturn($optionFour);
  124. $validationRuleOne = new ValidationRule($validationRuleOneData);
  125. $this->validationRuleFactoryMock->expects($this->once())
  126. ->method('create')
  127. ->with(['data' => $validationRuleOneData])
  128. ->willReturn($validationRuleOne);
  129. $attributeMetadataPartiallyConverted = [
  130. 'attribute_code' => 'attribute_code',
  131. 'frontend_input' => 'hidden',
  132. 'options' => [$optionOne, $optionFour],
  133. 'validation_rules' => [$validationRuleOne]
  134. ];
  135. $this->attributeMetadataFactoryMock->expects($this->once())
  136. ->method('create')
  137. ->with(['data' => $attributeMetadataPartiallyConverted])
  138. ->willReturn(
  139. new AttributeMetadata($attributeMetadataPartiallyConverted)
  140. );
  141. $attributeMetadata = $this->attributeMetadataHydrator->hydrate($attributeMetadataData);
  142. $this->assertInstanceOf(AttributeMetadataInterface::class, $attributeMetadata);
  143. $this->assertEquals(
  144. $attributeMetadataData['attribute_code'],
  145. $attributeMetadata->getAttributeCode()
  146. );
  147. $this->assertInternalType(
  148. \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
  149. $attributeMetadata->getOptions()
  150. );
  151. $this->assertArrayHasKey(
  152. 0,
  153. $attributeMetadata->getOptions()
  154. );
  155. $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[0]);
  156. $this->assertEquals(
  157. $optionOneData['label'],
  158. $attributeMetadata->getOptions()[0]->getLabel()
  159. );
  160. $this->assertArrayHasKey(1, $attributeMetadata->getOptions());
  161. $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]);
  162. $this->assertInternalType(
  163. \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
  164. $attributeMetadata->getOptions()[1]->getOptions()
  165. );
  166. $this->assertArrayHasKey(0, $attributeMetadata->getOptions()[1]->getOptions());
  167. $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]->getOptions()[0]);
  168. $this->assertEquals(
  169. $optionThreeData['label'],
  170. $attributeMetadata->getOptions()[1]->getOptions()[0]->getLabel()
  171. );
  172. $this->assertInternalType(
  173. \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
  174. $attributeMetadata->getValidationRules()
  175. );
  176. $this->assertArrayHasKey(0, $attributeMetadata->getValidationRules());
  177. $this->assertInstanceOf(ValidationRuleInterface::class, $attributeMetadata->getValidationRules()[0]);
  178. $this->assertEquals(
  179. $validationRuleOneData['name'],
  180. $attributeMetadata->getValidationRules()[0]->getName()
  181. );
  182. }
  183. public function testExtract()
  184. {
  185. $data = ['foo' => 'bar'];
  186. $this->dataObjectProcessorMock->expects($this->once())
  187. ->method('buildOutputDataArray')
  188. ->with(
  189. $this->attributeMetadataMock,
  190. AttributeMetadata::class
  191. )
  192. ->willReturn($data);
  193. $this->assertSame(
  194. $data,
  195. $this->attributeMetadataHydrator->extract($this->attributeMetadataMock)
  196. );
  197. }
  198. }