Skip to content

Commit 47884e8

Browse files
author
Yu Tang
committed
MAGETWO-32894: Refactor Catalog and related module to use mutable data object interface
- Improve performance by caching config data
1 parent 67e627b commit 47884e8

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Api\Config;
7+
8+
class MetadataConfigTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Framework\Api\Config\MetadataConfig
12+
*/
13+
protected $metadataConfig;
14+
15+
/**
16+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\Config\Reader
17+
*/
18+
protected $serviceConfigReaderMock;
19+
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\AttributeMetadataBuilderInterface
22+
*/
23+
protected $attributeMetadataBuilderMock;
24+
25+
/**
26+
* Prepare parameters
27+
*/
28+
public function setUp()
29+
{
30+
$this->serviceConfigReaderMock = $this->getMockBuilder('\Magento\Framework\Api\Config\Reader')
31+
->disableOriginalConstructor()
32+
->getMock();
33+
$this->attributeMetadataBuilderMock = $this->getMockBuilder(
34+
'\Magento\Framework\Api\AttributeMetadataBuilderInterface'
35+
)->disableOriginalConstructor()->getMock();
36+
37+
$this->metadataConfig = new \Magento\Framework\Api\Config\MetadataConfig(
38+
$this->serviceConfigReaderMock,
39+
$this->attributeMetadataBuilderMock
40+
);
41+
}
42+
43+
/**
44+
* Test caching
45+
*/
46+
public function testCaching()
47+
{
48+
$dataObjectClassName = '\Magento\Customer\Model\Data\Address';
49+
$attributeCode = 'street';
50+
$allAttributes = [
51+
$dataObjectClassName => [
52+
$attributeCode => 'value',
53+
]
54+
];
55+
$this->serviceConfigReaderMock->expects($this->once())
56+
->method('read')
57+
->willReturn($allAttributes);
58+
59+
$attributeMock = $this->getMock('\Magento\Framework\Api\MetadataObjectInterface');
60+
$this->attributeMetadataBuilderMock->expects($this->exactly(2))
61+
->method('setAttributeCode')
62+
->with($attributeCode);
63+
$this->attributeMetadataBuilderMock->expects($this->exactly(2))
64+
->method('create')
65+
->willReturn($attributeMock);
66+
67+
$attributes = $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName);
68+
$this->assertEquals($attributeMock, $attributes[$attributeCode]);
69+
$attributes = $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName);
70+
$this->assertEquals($attributeMock, $attributes[$attributeCode]);
71+
}
72+
}

lib/internal/Magento/Framework/Api/Config/MetadataConfig.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class MetadataConfig implements MetadataServiceInterface
2525
*/
2626
private $attributeMetadataBuilder;
2727

28+
/**
29+
* @var array
30+
*/
31+
private $allAttributes = [];
32+
2833
/**
2934
* Initialize dependencies.
3035
*
@@ -68,11 +73,13 @@ public function getCustomAttributesMetadata($dataObjectClassName = null)
6873
protected function getAttributesMetadata($dataObjectClassName)
6974
{
7075
$attributes = [];
71-
$allAttributes = $this->serviceConfigReader->read();
72-
if (isset($allAttributes[$dataObjectClassName])
73-
&& is_array($allAttributes[$dataObjectClassName])
76+
if (empty($this->allAttributes)) {
77+
$this->allAttributes = $this->serviceConfigReader->read();
78+
}
79+
if (isset($this->allAttributes[$dataObjectClassName])
80+
&& is_array($this->allAttributes[$dataObjectClassName])
7481
) {
75-
$attributeCodes = array_keys($allAttributes[$dataObjectClassName]);
82+
$attributeCodes = array_keys($this->allAttributes[$dataObjectClassName]);
7683
foreach ($attributeCodes as $attributeCode) {
7784
$this->attributeMetadataBuilder->setAttributeCode($attributeCode);
7885
$attributes[$attributeCode] = $this->attributeMetadataBuilder->create();

0 commit comments

Comments
 (0)