Skip to content

Commit d352973

Browse files
author
Igor Melnikov
committed
MAGETWO-64224: Remove usages of AttributeCache from customer module
- updating unit tests
1 parent c1cec3b commit d352973

File tree

3 files changed

+240
-88
lines changed

3 files changed

+240
-88
lines changed

app/code/Magento/Customer/Model/Metadata/AttributeMetadataHydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function createOptions(array $data)
9898
}
9999

100100
/**
101-
* @param $attributeMetadata
101+
* @param AttributeMetadataInterface $attributeMetadata
102102
* @return array
103103
*/
104104
public function extract($attributeMetadata)

app/code/Magento/Customer/Test/Unit/Model/Metadata/AttributeMetadataCacheTest.php

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
*/
66
namespace Magento\Customer\Test\Unit\Model\Metadata;
77

8+
use Magento\Customer\Model\Metadata\AttributeMetadataHydrator;
89
use Magento\Framework\App\CacheInterface;
910
use Magento\Framework\App\Cache\StateInterface;
1011
use Magento\Customer\Api\Data\AttributeMetadataInterface;
11-
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
12-
use Magento\Customer\Api\Data\OptionInterface;
13-
use Magento\Customer\Api\Data\OptionInterfaceFactory;
14-
use Magento\Customer\Api\Data\ValidationRuleInterface;
15-
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
1612
use Magento\Framework\Serialize\SerializerInterface;
1713
use Magento\Eav\Model\Cache\Type;
1814
use Magento\Eav\Model\Entity\Attribute;
@@ -39,19 +35,14 @@ class AttributeMetadataCacheTest extends \PHPUnit_Framework_TestCase
3935
private $stateMock;
4036

4137
/**
42-
* @var AttributeMetadataInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
38+
* @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
4339
*/
44-
private $attributeMetadataFactoryMock;
40+
private $attributeMetadataMock;
4541

4642
/**
47-
* @var OptionInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
43+
* @var AttributeMetadataHydrator|\PHPUnit_Framework_MockObject_MockObject
4844
*/
49-
private $optionFactoryMock;
50-
51-
/**
52-
* @var ValidationRuleInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
53-
*/
54-
private $validationRuleFactoryMock;
45+
private $attributeMetadataHydratorMock;
5546

5647
/**
5748
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -68,40 +59,22 @@ protected function setUp()
6859
$objectManager = new ObjectManager($this);
6960
$this->cacheMock = $this->getMock(CacheInterface::class);
7061
$this->stateMock = $this->getMock(StateInterface::class);
71-
$this->attributeMetadataFactoryMock = $this->getMock(
72-
AttributeMetadataInterfaceFactory::class,
73-
['create'],
74-
[],
75-
'',
76-
false
77-
);
78-
$this->optionFactoryMock = $this->getMock(
79-
OptionInterfaceFactory::class,
80-
['create'],
62+
$this->serializerMock = $this->getMock(SerializerInterface::class);
63+
$this->attributeMetadataMock = $this->getMock(AttributeMetadataInterface::class);
64+
$this->attributeMetadataHydratorMock = $this->getMock(
65+
AttributeMetadataHydrator::class,
8166
[],
82-
'',
83-
false
84-
);
85-
$this->validationRuleFactoryMock = $this->getMock(
86-
ValidationRuleInterfaceFactory::class,
87-
['create'],
8867
[],
8968
'',
9069
false
9170
);
92-
$this->serializerMock = $this->getMock(SerializerInterface::class);
93-
$this->attributeMetadataMock = $this->getMock(AttributeMetadataInterface::class);
94-
$this->optionMock = $this->getMock(OptionInterface::class);
95-
$this->validationRuleMock = $this->getMock(ValidationRuleInterface::class);
9671
$this->attributeMetadataCache = $objectManager->getObject(
9772
AttributeMetadataCache::class,
9873
[
9974
'cache' => $this->cacheMock,
10075
'state' => $this->stateMock,
101-
'attributeMetadataFactory' => $this->attributeMetadataFactoryMock,
102-
'optionFactory' => $this->optionFactoryMock,
103-
'validationRuleFactory' => $this->validationRuleFactoryMock,
104-
'serializer' => $this->serializerMock
76+
'serializer' => $this->serializerMock,
77+
'attributeMetadataHydrator' => $this->attributeMetadataHydratorMock
10578
]
10679
);
10780
}
@@ -134,9 +107,6 @@ public function testLoadNoCache()
134107
$this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
135108
}
136109

137-
/**
138-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
139-
*/
140110
public function testLoad()
141111
{
142112
$entityType = 'EntityType';
@@ -169,6 +139,7 @@ public function testLoad()
169139
'options' => [$optionOneData, $optionTwoData],
170140
'validation_rules' => [$validationRuleOneData]
171141
];
142+
$attributesMetadataData = [$attributeMetadataOneData];
172143
$this->stateMock->expects($this->once())
173144
->method('isEnabled')
174145
->with(Type::TYPE_IDENTIFIER)
@@ -180,53 +151,32 @@ public function testLoad()
180151
$this->serializerMock->expects($this->once())
181152
->method('unserialize')
182153
->with($serializedString)
183-
->willReturn([$attributeMetadataOneData]);
154+
->willReturn($attributesMetadataData);
184155

185-
$optionOne = new Option($optionOneData);
186-
$this->optionFactoryMock->expects($this->at(0))
187-
->method('create')
188-
->with(['data' => $optionOneData])
189-
->willReturn($optionOne);
190-
$optionThree = new Option($optionThreeData);
191-
$this->optionFactoryMock->expects($this->at(1))
192-
->method('create')
193-
->with(['data' => $optionThreeData])
194-
->willReturn($optionThree);
195-
$optionFour = new Option($optionFourData);
196-
$this->optionFactoryMock->expects($this->at(2))
197-
->method('create')
198-
->with(['data' => $optionFourData])
199-
->willReturn($optionFour);
200-
201-
$optionTwoDataForFactory = [
156+
$optionTwoDataPartiallyConverted = [
202157
'label' => 'Label 2',
203-
'options' => [$optionThree, $optionFour]
158+
'options' => [
159+
new Option($optionThreeData),
160+
new Option($optionFourData)
161+
]
204162
];
205-
$optionFour = new Option($optionTwoDataForFactory);
206-
$this->optionFactoryMock->expects($this->at(3))
207-
->method('create')
208-
->with(['data' => $optionTwoDataForFactory])
209-
->willReturn($optionFour);
210163

211164
$validationRule = new ValidationRule($validationRuleOneData);
212-
$this->validationRuleFactoryMock->expects($this->once())
213-
->method('create')
214-
->with(['data' => $validationRuleOneData])
215-
->willReturn($validationRule);
216165

217-
$attributeMetadataDataForFactory = [
166+
$attributeMetadataDataPartiallyConverted = [
218167
'attribute_code' => 'attribute_code',
219168
'frontend_input' => 'hidden',
220-
'options' => [$optionOne, $optionFour],
169+
'options' => [
170+
new Option($optionOneData),
171+
new Option($optionTwoDataPartiallyConverted)
172+
],
221173
'validation_rules' => [$validationRule]
222174
];
223175

224-
$this->attributeMetadataFactoryMock->expects($this->once())
225-
->method('create')
226-
->with(['data' => $attributeMetadataDataForFactory])
227-
->willReturn(
228-
new AttributeMetadata($attributeMetadataDataForFactory)
229-
);
176+
$this->attributeMetadataHydratorMock->expects($this->once())
177+
->method('hydrate')
178+
->with($attributeMetadataOneData)
179+
->willReturn(new AttributeMetadata($attributeMetadataDataPartiallyConverted));
230180

231181
$attributeMetadata = $this->attributeMetadataCache->load($entityType, $suffix);
232182

@@ -278,15 +228,10 @@ public function testSave()
278228
->method('isEnabled')
279229
->with(Type::TYPE_IDENTIFIER)
280230
->willReturn(true);
281-
$attributeMetadataMock = $this->getMock(
282-
AttributeMetadata::class,
283-
[],
284-
[],
285-
'',
286-
false
287-
);
288-
$attributeMetadataMock->expects($this->once())
289-
->method('__toArray')
231+
$this->attributeMetadataMock = $this->getMock(AttributeMetadataInterface::class);
232+
$this->attributeMetadataHydratorMock->expects($this->once())
233+
->method('extract')
234+
->with($this->attributeMetadataMock)
290235
->willReturn($attributeMetadataOneData);
291236
$this->serializerMock->expects($this->once())
292237
->method('serialize')
@@ -303,7 +248,7 @@ public function testSave()
303248
System::CACHE_TAG
304249
]
305250
);
306-
$attributesMetadata = [$attributeMetadataMock];
251+
$attributesMetadata = [$this->attributeMetadataMock];
307252
$this->attributeMetadataCache->save($entityType, $attributesMetadata, $suffix);
308253
$this->assertSame(
309254
$attributesMetadata,

0 commit comments

Comments
 (0)