Skip to content

Commit 3e97dbc

Browse files
author
Joan He
committed
MAGETWO-59759: Remove usage of AttributeCache from \Magento\Eav\Model\ResourceModel\ReadHandler
- address code review comment
1 parent 8600a7a commit 3e97dbc

File tree

1 file changed

+80
-29
lines changed

1 file changed

+80
-29
lines changed

app/code/Magento/Eav/Test/Unit/Model/ResourceModel/ReadHandlerTest.php

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,94 @@ class ReadHandlerTest extends \PHPUnit_Framework_TestCase
2121
*/
2222
private $metadataPoolMock;
2323

24+
/**
25+
* @var EntityMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $metadataMock;
28+
2429
/**
2530
* @var \Magento\Eav\Model\ResourceModel\ReadHandler
2631
*/
2732
private $readHandler;
2833

34+
/**
35+
* @var \Magento\Framework\Model\Entity\ScopeResolver|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $scopeResolverMock;
38+
2939
protected function setUp()
3040
{
31-
$this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
41+
$objectManager = new ObjectManager($this);
42+
$args = $objectManager->getConstructArguments(\Magento\Eav\Model\ResourceModel\ReadHandler::class);
43+
$this->metadataPoolMock = $args['metadataPool'];
44+
$this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
3245
->disableOriginalConstructor()
3346
->getMock();
34-
$this->configMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
35-
->disableOriginalConstructor()
36-
->getMock();
37-
$this->readHandler = (new ObjectManager($this))->getObject(
38-
\Magento\Eav\Model\ResourceModel\ReadHandler::class,
39-
[
40-
'metadataPool' => $this->metadataPoolMock,
41-
'config' => $this->configMock,
42-
]
43-
);
47+
$this->metadataPoolMock->expects($this->any())
48+
->method('getMetadata')
49+
->willReturn($this->metadataMock);
50+
$this->configMock = $args['config'];
51+
$this->scopeResolverMock = $args['scopeResolver'];
52+
$this->scopeResolverMock->method('getEntityContext')
53+
->willReturn([]);
54+
55+
$this->readHandler = $objectManager->getObject(\Magento\Eav\Model\ResourceModel\ReadHandler::class, $args);
4456
}
4557

4658
/**
4759
* @param string $eavEntityType
4860
* @param int $callNum
4961
* @param array $expected
50-
* @dataProvider getAttributesDataProvider
62+
* @param bool $isStatic
63+
* @dataProvider executeDataProvider
5164
*/
52-
public function testGetAttributes($eavEntityType, $callNum, array $expected)
65+
public function testExecute($eavEntityType, $callNum, array $expected, $isStatic = true)
5366
{
54-
$metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
67+
$entityData = ['linkField' => 'theLinkField'];
68+
$this->metadataMock->method('getEavEntityType')
69+
->willReturn($eavEntityType);
70+
$connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
5571
->disableOriginalConstructor()
5672
->getMock();
57-
$this->metadataPoolMock->expects($this->once())
58-
->method('getMetadata')
59-
->willReturn($metadataMock);
60-
$metadataMock->expects($this->once())
61-
->method('getEavEntityType')
62-
->willReturn($eavEntityType);
73+
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$selectMock->method('from')
77+
->willReturnSelf();
78+
$selectMock->method('where')
79+
->willReturnSelf();
80+
$connectionMock->method('select')
81+
->willReturn($selectMock);
82+
$connectionMock->method('fetchAll')
83+
->willReturn(
84+
[
85+
[
86+
'attribute_id' => 'attributeId',
87+
'value' => 'attributeValue',
88+
]
89+
]
90+
);
91+
$this->metadataMock->method('getEntityConnection')
92+
->willReturn($connectionMock);
93+
$this->metadataMock->method('getLinkField')
94+
->willReturn('linkField');
95+
6396
$attributeMock = $this->getMockBuilder(AbstractAttribute::class)
6497
->disableOriginalConstructor()
6598
->getMock();
99+
$attributeMock->method('isStatic')
100+
->willReturn($isStatic);
101+
$backendMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class)
102+
->disableOriginalConstructor()
103+
->getMock();
104+
$backendMock->method('getTable')
105+
->willReturn('backendTable');
106+
$attributeMock->method('getBackend')
107+
->willReturn($backendMock);
108+
$attributeMock->method('getAttributeId')
109+
->willReturn('attributeId');
110+
$attributeMock->method('getAttributeCode')
111+
->willReturn('attributeCode');
66112
$this->configMock->expects($this->exactly($callNum))
67113
->method('getAttributes')
68114
->willReturn([$attributeMock]);
@@ -71,25 +117,30 @@ public function testGetAttributes($eavEntityType, $callNum, array $expected)
71117
'getAttributes'
72118
);
73119
$getAttributesMethod->setAccessible(true);
74-
$this->assertEquals($expected, $getAttributesMethod->invoke($this->readHandler, 'entity_type'));
120+
$this->assertEquals($expected, $this->readHandler->execute('entity_type', $entityData));
75121
}
76122

77-
public function getAttributesDataProvider()
123+
public function executeDataProvider()
78124
{
79-
$attributeMock = $this->getMockBuilder(AbstractAttribute::class)
80-
->disableOriginalConstructor()
81-
->getMock();
82-
83125
return [
84-
[null, 0, []],
85-
['env-entity-type', 1, [$attributeMock]]
126+
'null entity type' => [null, 0, ['linkField' => 'theLinkField']],
127+
'static attribute' => ['env-entity-type', 1, ['linkField' => 'theLinkField']],
128+
'non-static attribute' => [
129+
'env-entity-type',
130+
1,
131+
[
132+
'linkField' => 'theLinkField',
133+
'attributeCode' => 'attributeValue'
134+
],
135+
false
136+
],
86137
];
87138
}
88139

89140
/**
90141
* @expectedException \Exception
91142
*/
92-
public function testGetAttributesWithException()
143+
public function testExecuteWithException()
93144
{
94145
$this->metadataPoolMock->expects($this->once())
95146
->method('getMetadata')

0 commit comments

Comments
 (0)