Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeMetadatConverterTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Customer\Api\Data\OptionInterfaceFactory;
9 use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
10 use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
12 
18 class AttributeMetadatConverterTest extends \PHPUnit\Framework\TestCase
19 {
23  private $optionFactory;
24 
28  private $validationRuleFactory;
29 
33  private $attributeMetadataFactory;
34 
38  private $dataObjectHelper;
39 
41  private $model;
42 
44  private $attribute;
45 
46  public function setUp()
47  {
48  $this->optionFactory = $this->getMockBuilder(OptionInterfaceFactory::class)
49  ->setMethods(['create'])
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->validationRuleFactory = $this->getMockBuilder(ValidationRuleInterfaceFactory::class)
53  ->setMethods(['create'])
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->attributeMetadataFactory = $this->getMockBuilder(AttributeMetadataInterfaceFactory::class)
57  ->setMethods(['create'])
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->model = new AttributeMetadataConverter(
68  $this->optionFactory,
69  $this->validationRuleFactory,
70  $this->attributeMetadataFactory,
71  $this->dataObjectHelper
72  );
73  }
74 
78  private function prepareValidateRules()
79  {
80  return [
81  'one' => 'numeric',
82  'two' => 'alphanumeric'
83  ];
84  }
85 
89  private function prepareOptions()
90  {
91  return [
92  [
93  'label' => 'few_values',
94  'value' => [
95  [1], [2]
96  ]
97  ],
98  [
99  'label' => 'one_value',
100  'value' => 1
101  ]
102  ];
103  }
104 
106  {
107  $validatedRules = $this->prepareValidateRules();
108  $options = $this->prepareOptions();
109  $optionDataObjectForSimpleValue1 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
110  ->disableOriginalConstructor()
111  ->getMock();
112  $optionDataObjectForSimpleValue2 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $optionObject1 = $this->createMock(\Magento\Customer\Api\Data\OptionInterface::class);
116  $optionObject2 = $this->createMock(\Magento\Customer\Api\Data\OptionInterface::class);
117  $this->optionFactory->expects($this->exactly(4))
118  ->method('create')
119  ->will(
120  $this->onConsecutiveCalls(
121  $optionDataObjectForSimpleValue2,
122  $optionObject1,
123  $optionObject2,
124  $optionDataObjectForSimpleValue1
125  )
126  );
127  $source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $source->expects($this->once())
131  ->method('getAllOptions')
132  ->willReturn($options);
133  $this->attribute->expects($this->once())
134  ->method('usesSource')
135  ->willReturn(true);
136  $this->attribute->expects($this->once())
137  ->method('getSource')
138  ->willReturn($source);
139  $optionDataObjectForSimpleValue1->expects($this->once())
140  ->method('setValue')
141  ->with(1);
142  $optionDataObjectForSimpleValue2->expects($this->once())
143  ->method('setLabel')
144  ->with('few_values');
145  $optionDataObjectForSimpleValue1->expects($this->once())
146  ->method('setLabel')
147  ->with('one_value');
148  $this->dataObjectHelper->expects($this->exactly(2))
149  ->method('populateWithArray')
150  ->withConsecutive(
151  [$optionObject1, ['1'], \Magento\Customer\Api\Data\OptionInterface::class],
152  [$optionObject2, ['2'], \Magento\Customer\Api\Data\OptionInterface::class]
153  );
154  $validationRule1 = $this->createMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
155  $validationRule2 = $this->createMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
156  $this->validationRuleFactory->expects($this->exactly(2))
157  ->method('create')
158  ->will($this->onConsecutiveCalls($validationRule1, $validationRule2));
159  $validationRule1->expects($this->once())
160  ->method('setValue')
161  ->with('numeric');
162  $validationRule1->expects($this->once())
163  ->method('setName')
164  ->with('one')
165  ->willReturnSelf();
166  $validationRule2->expects($this->once())
167  ->method('setValue')
168  ->with('alphanumeric');
169  $validationRule2->expects($this->once())
170  ->method('setName')
171  ->with('two')
172  ->willReturnSelf();
173 
174  $mockMethods = ['setAttributeCode', 'setFrontendInput'];
175  $attributeMetaData = $this->getMockBuilder(\Magento\Customer\Model\Data\AttributeMetadata::class)
176  ->setMethods($mockMethods)
177  ->disableOriginalConstructor()
178  ->getMock();
179  foreach ($mockMethods as $method) {
180  $attributeMetaData->expects($this->once())->method($method)->willReturnSelf();
181  }
182 
183  $this->attribute->expects($this->once())
184  ->method('getValidateRules')
185  ->willReturn($validatedRules);
186  $this->attributeMetadataFactory->expects($this->once())
187  ->method('create')
188  ->willReturn($attributeMetaData);
189  $frontend = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192  $this->attribute->expects($this->once())
193  ->method('getFrontend')
194  ->willReturn($frontend);
195  $optionDataObjectForSimpleValue2->expects($this->once())
196  ->method('setOptions')
197  ->with([$optionObject1, $optionObject2]);
198  $this->model->createMetadataAttribute($this->attribute);
199  }
200 }
$source
Definition: source.php:23
$method
Definition: info.phtml:13