15
15
use Magento \Framework \DB \Select ;
16
16
use Magento \Framework \EntityManager \EntityMetadataInterface ;
17
17
use Magento \Framework \EntityManager \MetadataPool ;
18
+ use Magento \Framework \Model \Entity \ScopeInterface ;
18
19
use Magento \Framework \Model \Entity \ScopeResolver ;
19
20
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
21
use PHPUnit \Framework \MockObject \MockObject ;
21
22
use PHPUnit \Framework \TestCase ;
22
23
24
+ /**
25
+ * Eav attributes read handler tests
26
+ *
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
+ */
23
29
class ReadHandlerTest extends TestCase
24
30
{
25
31
/**
@@ -60,8 +66,17 @@ protected function setUp(): void
60
66
->willReturn ($ this ->metadataMock );
61
67
$ this ->configMock = $ args ['config ' ];
62
68
$ this ->scopeResolverMock = $ args ['scopeResolver ' ];
69
+
70
+ $ scopeMock = $ this ->getMockBuilder (ScopeInterface::class)
71
+ ->disableOriginalConstructor ()
72
+ ->getMock ();
73
+ $ fallback = clone $ scopeMock ;
74
+ $ scopeMock ->method ('getIdentifier ' )->willReturn ('store_id ' );
75
+ $ scopeMock ->method ('getValue ' )->willReturn (1 );
76
+ $ scopeMock ->method ('getFallback ' )->willReturn ($ fallback );
77
+
63
78
$ this ->scopeResolverMock ->method ('getEntityContext ' )
64
- ->willReturn ([]);
79
+ ->willReturn ([$ scopeMock ]);
65
80
66
81
$ this ->readHandler = $ objectManager ->getObject (ReadHandler::class, $ args );
67
82
}
@@ -73,8 +88,12 @@ protected function setUp(): void
73
88
* @param bool $isStatic
74
89
* @dataProvider executeDataProvider
75
90
*/
76
- public function testExecute ($ eavEntityType , $ callNum , array $ expected , $ isStatic = true )
77
- {
91
+ public function testExecute (
92
+ $ eavEntityType ,
93
+ $ callNum ,
94
+ array $ expected ,
95
+ $ isStatic = true
96
+ ) {
78
97
$ entityData = ['linkField ' => 'theLinkField ' ];
79
98
$ this ->metadataMock ->method ('getEavEntityType ' )
80
99
->willReturn ($ eavEntityType );
@@ -105,10 +124,90 @@ public function testExecute($eavEntityType, $callNum, array $expected, $isStatic
105
124
->willReturn ('linkField ' );
106
125
107
126
$ attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
127
+ ->setMethods (['getAttributeCode ' , 'isScopeWebsite ' , 'isStatic ' , 'getBackend ' , 'getAttributeId ' ])
128
+ ->disableOriginalConstructor ()
129
+ ->getMockForAbstractClass ();
130
+ $ attributeMock ->method ('isStatic ' )
131
+ ->willReturn ($ isStatic );
132
+ $ backendMock = $ this ->getMockBuilder (AbstractBackend::class)
108
133
->disableOriginalConstructor ()
109
134
->getMock ();
135
+ $ backendMock ->method ('getTable ' )
136
+ ->willReturn ('backendTable ' );
137
+ $ attributeMock ->method ('getBackend ' )
138
+ ->willReturn ($ backendMock );
139
+ $ attributeMock ->method ('getAttributeId ' )
140
+ ->willReturn ('attributeId ' );
141
+ $ attributeMock ->method ('getAttributeCode ' )
142
+ ->willReturn ('attributeCode ' );
143
+ $ this ->configMock ->expects ($ this ->exactly ($ callNum ))
144
+ ->method ('getEntityAttributes ' )
145
+ ->willReturn ([$ attributeMock ]);
146
+ $ this ->assertEquals ($ expected , $ this ->readHandler ->execute ('entity_type ' , $ entityData ));
147
+ }
148
+
149
+ /**
150
+ * @param string $eavEntityType
151
+ * @param int $callNum
152
+ * @param array $expected
153
+ * @param bool $isStatic
154
+ * @param null|int $isGlobalScope
155
+ * @throws \Magento\Framework\Exception\ConfigurationMismatchException
156
+ * @throws \Magento\Framework\Exception\LocalizedException
157
+ * @dataProvider executeGlobalScopeDataProvider
158
+ */
159
+ public function testExecuteGlobalScope (
160
+ $ eavEntityType ,
161
+ $ callNum ,
162
+ array $ expected ,
163
+ $ isStatic = true ,
164
+ $ isGlobalScope = null
165
+ ) {
166
+ $ entityData = ['linkField ' => 'theLinkField ' ];
167
+ $ this ->metadataMock ->method ('getEavEntityType ' )
168
+ ->willReturn ($ eavEntityType );
169
+ $ connectionMock = $ this ->getMockBuilder (AdapterInterface::class)
170
+ ->disableOriginalConstructor ()
171
+ ->getMockForAbstractClass ();
172
+ $ selectMock = $ this ->getMockBuilder (Select::class)
173
+ ->disableOriginalConstructor ()
174
+ ->getMock ();
175
+ $ selectMock ->method ('from ' )
176
+ ->willReturnSelf ();
177
+ $ selectMock ->method ('where ' )
178
+ ->willReturnSelf ();
179
+ $ connectionMock ->method ('select ' )
180
+ ->willReturn ($ selectMock );
181
+ $ connectionMock ->method ('fetchAll ' )
182
+ ->willReturn (
183
+ [
184
+ [
185
+ 'attribute_id ' => 'attributeId ' ,
186
+ 'value ' => 'attributeValue ' ,
187
+ 'store_id ' => 0
188
+ ]
189
+ ]
190
+ );
191
+ $ this ->metadataMock ->method ('getEntityConnection ' )
192
+ ->willReturn ($ connectionMock );
193
+ $ this ->metadataMock ->method ('getLinkField ' )
194
+ ->willReturn ('linkField ' );
195
+
196
+ $ attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
197
+ ->setMethods ([
198
+ 'getAttributeCode ' ,
199
+ 'isScopeWebsite ' ,
200
+ 'getIsGlobal ' ,
201
+ 'isStatic ' ,
202
+ 'getBackend ' ,
203
+ 'getAttributeId '
204
+ ])
205
+ ->disableOriginalConstructor ()
206
+ ->getMockForAbstractClass ();
110
207
$ attributeMock ->method ('isStatic ' )
111
208
->willReturn ($ isStatic );
209
+ $ attributeMock ->method ('getIsGlobal ' )
210
+ ->willReturn ($ isGlobalScope );
112
211
$ backendMock = $ this ->getMockBuilder (AbstractBackend::class)
113
212
->disableOriginalConstructor ()
114
213
->getMock ();
@@ -142,6 +241,39 @@ public function executeDataProvider()
142
241
'attributeCode ' => 'attributeValue '
143
242
],
144
243
false
244
+ ]
245
+ ];
246
+ }
247
+
248
+ /**
249
+ * @return array
250
+ */
251
+ public function executeGlobalScopeDataProvider ()
252
+ {
253
+ return [
254
+ 'null entity type ' => [null , 0 , ['linkField ' => 'theLinkField ' ]],
255
+ 'static attribute ' => ['env-entity-type ' , 1 , ['linkField ' => 'theLinkField ' ]],
256
+ 'non-static attribute ' => [
257
+ 'env-entity-type ' ,
258
+ 1 ,
259
+ [
260
+ 'linkField ' => 'theLinkField ' ,
261
+ 'attributeCode ' => 'attributeValue '
262
+ ],
263
+ false ,
264
+ null ,
265
+ 1
266
+ ],
267
+ 'non-static attribute2 ' => [
268
+ 'env-entity-type ' ,
269
+ 1 ,
270
+ [
271
+ 'linkField ' => 'theLinkField ' ,
272
+ 'attributeCode ' => 'attributeValue '
273
+ ],
274
+ false ,
275
+ 1 ,
276
+ 0
145
277
],
146
278
];
147
279
}
0 commit comments