Skip to content

Commit 79779da

Browse files
author
Igor Melnikov
committed
MAGETWO-64224: Remove usages of AttributeCache from customer module
- refactoring tests
1 parent de8156b commit 79779da

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
1010
use Magento\Customer\Api\Data\OptionInterface;
1111
use Magento\Customer\Api\Data\OptionInterfaceFactory;
12+
use Magento\Customer\Api\Data\ValidationRuleInterface;
1213
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
1314
use Magento\Framework\Reflection\DataObjectProcessor;
1415

@@ -71,17 +72,15 @@ public function hydrate(array $data)
7172
);
7273
}
7374
if (isset($data[AttributeMetadataInterface::VALIDATION_RULES])) {
74-
$validationRules = [];
75-
foreach ($data[AttributeMetadataInterface::VALIDATION_RULES] as $validationRuleData) {
76-
$validationRules[] = $this->validationRuleFactory->create(['data' => $validationRuleData]);
77-
}
78-
$data[AttributeMetadataInterface::VALIDATION_RULES] = $validationRules;
75+
$data[AttributeMetadataInterface::VALIDATION_RULES] = $this->createValidationRules(
76+
$data[AttributeMetadataInterface::VALIDATION_RULES]
77+
);
7978
}
8079
return $this->attributeMetadataFactory->create(['data' => $data]);
8180
}
8281

8382
/**
84-
* Convert AttributeMetadataInterface to array
83+
* Populate options with data
8584
*
8685
* @param array $data
8786
* @return OptionInterface[]
@@ -98,6 +97,22 @@ private function createOptions(array $data)
9897
}
9998

10099
/**
100+
* Populate validation rules with data
101+
*
102+
* @param array $data
103+
* @return ValidationRuleInterface[]
104+
*/
105+
private function createValidationRules(array $data)
106+
{
107+
foreach ($data as $key => $validationRuleData) {
108+
$data[$key] = $this->validationRuleFactory->create(['data' => $validationRuleData]);
109+
}
110+
return $data;
111+
}
112+
113+
/**
114+
* Convert AttributeMetadataInterface to array
115+
*
101116
* @param AttributeMetadataInterface $attributeMetadata
102117
* @return array
103118
*/

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public function testLoadCacheDisabled()
7575
->method('isEnabled')
7676
->with(Type::TYPE_IDENTIFIER)
7777
->willReturn(false);
78+
$this->cacheMock->expects($this->never())
79+
->method('load');
7880
$this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
81+
// Make sure isEnabled called once
7982
$this->attributeMetadataCache->load($entityType, $suffix);
8083
}
8184

@@ -124,9 +127,18 @@ public function testLoad()
124127
->method('hydrate')
125128
->with($attributeMetadataOneData)
126129
->willReturn($attributeMetadataMock);
130+
$attributesMetadata = $this->attributeMetadataCache->load($entityType, $suffix);
131+
$this->assertInternalType(
132+
\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY,
133+
$attributesMetadata
134+
);
135+
$this->assertArrayHasKey(
136+
0,
137+
$attributesMetadata
138+
);
127139
$this->assertInstanceOf(
128140
AttributeMetadataInterface::class,
129-
$this->attributeMetadataCache->load($entityType, $suffix)[0]
141+
$attributesMetadata[0]
130142
);
131143
}
132144

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
99
use Magento\Customer\Api\Data\AttributeMetadataInterface;
10-
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
11-
use Magento\Customer\Api\Data\OptionInterfaceFactory;
12-
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
1310
use Magento\Customer\Model\Data\AttributeMetadata;
11+
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
12+
use Magento\Customer\Api\Data\OptionInterface;
1413
use Magento\Customer\Model\Data\Option;
14+
use Magento\Customer\Api\Data\OptionInterfaceFactory;
15+
use Magento\Customer\Api\Data\ValidationRuleInterface;
1516
use Magento\Customer\Model\Data\ValidationRule;
16-
use Magento\Framework\Reflection\DataObjectProcessor;
17+
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
1718
use Magento\Customer\Model\Metadata\AttributeMetadataHydrator;
19+
use Magento\Framework\Reflection\DataObjectProcessor;
1820

1921
class AttributeMetadataHydratorTest extends \PHPUnit_Framework_TestCase
2022
{
@@ -168,18 +170,43 @@ public function testHydrate()
168170

169171
$attributeMetadata = $this->attributeMetadataHydrator->hydrate($attributeMetadataData);
170172

173+
$this->assertInstanceOf(AttributeMetadataInterface::class, $attributeMetadata);
171174
$this->assertEquals(
172175
$attributeMetadataData['attribute_code'],
173176
$attributeMetadata->getAttributeCode()
174177
);
178+
$this->assertInternalType(
179+
\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY,
180+
$attributeMetadata->getOptions()
181+
);
182+
$this->assertArrayHasKey(
183+
0,
184+
$attributeMetadata->getOptions()
185+
);
186+
$this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[0]);
175187
$this->assertEquals(
176188
$optionOneData['label'],
177189
$attributeMetadata->getOptions()[0]->getLabel()
178190
);
191+
$this->assertArrayHasKey(1, $attributeMetadata->getOptions());
192+
$this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]);
193+
194+
$this->assertInternalType(
195+
\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY,
196+
$attributeMetadata->getOptions()[1]->getOptions()
197+
);
198+
$this->assertArrayHasKey(0, $attributeMetadata->getOptions()[1]->getOptions());
199+
$this->assertInstanceOf(OptionInterface::class, $attributeMetadata->getOptions()[1]->getOptions()[0]);
179200
$this->assertEquals(
180201
$optionThreeData['label'],
181202
$attributeMetadata->getOptions()[1]->getOptions()[0]->getLabel()
182203
);
204+
$this->assertInternalType(
205+
\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY,
206+
$attributeMetadata->getValidationRules()
207+
);
208+
$this->assertArrayHasKey(0, $attributeMetadata->getValidationRules());
209+
$this->assertInstanceOf(ValidationRuleInterface::class, $attributeMetadata->getValidationRules()[0]);
183210
$this->assertEquals(
184211
$validationRuleOneData['name'],
185212
$attributeMetadata->getValidationRules()[0]->getName()

0 commit comments

Comments
 (0)