Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeMetadataHydratorTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
11 use Magento\Customer\Api\Data\OptionInterfaceFactory;
13 use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
20 
24 class AttributeMetadataHydratorTest extends \PHPUnit\Framework\TestCase
25 {
29  private $attributeMetadataFactoryMock;
30 
34  private $optionFactoryMock;
35 
39  private $validationRuleFactoryMock;
40 
44  private $attributeMetadataMock;
45 
49  private $dataObjectProcessorMock;
50 
54  private $attributeMetadataHydrator;
55 
56  protected function setUp()
57  {
58  $objectManager = new ObjectManager($this);
59  $this->attributeMetadataFactoryMock = $this->createPartialMock(
60  AttributeMetadataInterfaceFactory::class,
61  ['create']
62  );
63  $this->optionFactoryMock = $this->createPartialMock(OptionInterfaceFactory::class, ['create']);
64  $this->validationRuleFactoryMock = $this->createPartialMock(ValidationRuleInterfaceFactory::class, ['create']);
65  $this->attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
66  $this->dataObjectProcessorMock = $this->createMock(DataObjectProcessor::class);
67  $this->attributeMetadataHydrator = $objectManager->getObject(
68  AttributeMetadataHydrator::class,
69  [
70  'attributeMetadataFactory' => $this->attributeMetadataFactoryMock,
71  'optionFactory' => $this->optionFactoryMock,
72  'validationRuleFactory' => $this->validationRuleFactoryMock,
73  'dataObjectProcessor' => $this->dataObjectProcessorMock
74  ]
75  );
76  }
77 
81  public function testHydrate()
82  {
83  $optionOneData = [
84  'label' => 'Label 1',
85  'options' => null
86  ];
87  $optionThreeData = [
88  'label' => 'Label 3',
89  'options' => null
90  ];
91  $optionFourData = [
92  'label' => 'Label 4',
93  'options' => null
94  ];
95  $optionTwoData = [
96  'label' => 'Label 2',
97  'options' => [$optionThreeData, $optionFourData]
98  ];
99  $validationRuleOneData = [
100  'name' => 'Name 1',
101  'value' => 'Value 1'
102  ];
103  $attributeMetadataData = [
104  'attribute_code' => 'attribute_code',
105  'frontend_input' => 'hidden',
106  'options' => [$optionOneData, $optionTwoData],
107  'validation_rules' => [$validationRuleOneData]
108  ];
109 
110  $optionOne = new Option($optionOneData);
111  $this->optionFactoryMock->expects($this->at(0))
112  ->method('create')
113  ->with(['data' => $optionOneData])
114  ->willReturn($optionOne);
115  $optionThree = new Option($optionThreeData);
116  $this->optionFactoryMock->expects($this->at(1))
117  ->method('create')
118  ->with(['data' => $optionThreeData])
119  ->willReturn($optionThree);
120  $optionFour = new Option($optionFourData);
121  $this->optionFactoryMock->expects($this->at(2))
122  ->method('create')
123  ->with(['data' => $optionFourData])
124  ->willReturn($optionFour);
125 
126  $optionTwoDataPartiallyConverted = [
127  'label' => 'Label 2',
128  'options' => [$optionThree, $optionFour]
129  ];
130  $optionFour = new Option($optionTwoDataPartiallyConverted);
131  $this->optionFactoryMock->expects($this->at(3))
132  ->method('create')
133  ->with(['data' => $optionTwoDataPartiallyConverted])
134  ->willReturn($optionFour);
135 
136  $validationRuleOne = new ValidationRule($validationRuleOneData);
137  $this->validationRuleFactoryMock->expects($this->once())
138  ->method('create')
139  ->with(['data' => $validationRuleOneData])
140  ->willReturn($validationRuleOne);
141 
142  $attributeMetadataPartiallyConverted = [
143  'attribute_code' => 'attribute_code',
144  'frontend_input' => 'hidden',
145  'options' => [$optionOne, $optionFour],
146  'validation_rules' => [$validationRuleOne]
147  ];
148 
149  $this->attributeMetadataFactoryMock->expects($this->once())
150  ->method('create')
151  ->with(['data' => $attributeMetadataPartiallyConverted])
152  ->willReturn(
153  new AttributeMetadata($attributeMetadataPartiallyConverted)
154  );
155 
156  $attributeMetadata = $this->attributeMetadataHydrator->hydrate($attributeMetadataData);
157 
158  $this->assertInstanceOf(AttributeMetadataInterface::class, $attributeMetadata);
159  $this->assertEquals(
160  $attributeMetadataData['attribute_code'],
161  $attributeMetadata->getAttributeCode()
162  );
163  $this->assertInternalType(
164  \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
165  $attributeMetadata->getOptions()
166  );
167  $this->assertArrayHasKey(
168  0,
169  $attributeMetadata->getOptions()
170  );
171  $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[0]);
172  $this->assertEquals(
173  $optionOneData['label'],
174  $attributeMetadata->getOptions()[0]->getLabel()
175  );
176  $this->assertArrayHasKey(1, $attributeMetadata->getOptions());
177  $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]);
178 
179  $this->assertInternalType(
180  \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
181  $attributeMetadata->getOptions()[1]->getOptions()
182  );
183  $this->assertArrayHasKey(0, $attributeMetadata->getOptions()[1]->getOptions());
184  $this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]->getOptions()[0]);
185  $this->assertEquals(
186  $optionThreeData['label'],
187  $attributeMetadata->getOptions()[1]->getOptions()[0]->getLabel()
188  );
189  $this->assertInternalType(
190  \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
191  $attributeMetadata->getValidationRules()
192  );
193  $this->assertArrayHasKey(0, $attributeMetadata->getValidationRules());
194  $this->assertInstanceOf(ValidationRuleInterface::class, $attributeMetadata->getValidationRules()[0]);
195  $this->assertEquals(
196  $validationRuleOneData['name'],
197  $attributeMetadata->getValidationRules()[0]->getName()
198  );
199  }
200 
201  public function testExtract()
202  {
203  $data = ['foo' => 'bar'];
204  $this->dataObjectProcessorMock->expects($this->once())
205  ->method('buildOutputDataArray')
206  ->with(
207  $this->attributeMetadataMock,
208  AttributeMetadata::class
209  )
210  ->willReturn($data);
211  $this->assertSame(
212  $data,
213  $this->attributeMetadataHydrator->extract($this->attributeMetadataMock)
214  );
215  }
216 }
$objectManager
Definition: bootstrap.php:17