10
10
*/
11
11
namespace Magento \Theme \Test \Unit \Model \Indexer \Design ;
12
12
13
+ use Magento \Framework \App \ResourceConnection ;
13
14
use Magento \Framework \Data \Collection ;
14
15
use Magento \Framework \Indexer \FieldsetInterface ;
15
16
use Magento \Framework \Indexer \FieldsetPool ;
16
17
use Magento \Framework \Indexer \HandlerInterface ;
17
18
use Magento \Framework \Indexer \HandlerPool ;
18
19
use Magento \Framework \Indexer \IndexStructureInterface ;
19
- use Magento \Framework \Indexer \SaveHandler \IndexerInterface ;
20
+ use Magento \Framework \Indexer \SaveHandler \Batch ;
20
21
use Magento \Framework \Indexer \SaveHandlerFactory ;
22
+ use Magento \Framework \Indexer \ScopeResolver \FlatScopeResolver ;
23
+ use Magento \Framework \Indexer \ScopeResolver \IndexScopeResolver ;
21
24
use Magento \Framework \Indexer \StructureFactory ;
25
+ use Magento \Theme \Model \Data \Design \Config as DesignConfig ;
22
26
use Magento \Theme \Model \Indexer \Design \Config ;
27
+ use Magento \Theme \Model \ResourceModel \Design \Config \Scope \CollectionFactory ;
28
+ use PHPUnit \Framework \MockObject \MockObject ;
23
29
use PHPUnit \Framework \TestCase ;
30
+ use Magento \Theme \Model \Indexer \Design \IndexerHandler ;
31
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
24
32
33
+ /**
34
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35
+ */
25
36
class ConfigTest extends TestCase
26
37
{
27
- /** @var Config */
28
- protected $ model ;
38
+ /**
39
+ * @var AdapterInterface|MockObject
40
+ */
41
+ private $ adapter ;
42
+ /**
43
+ * @var ResourceConnection|MockObject
44
+ */
45
+ private $ resourceConnection ;
46
+ /**
47
+ * @var Batch|MockObject
48
+ */
49
+ private $ batch ;
50
+ /**
51
+ * @var IndexStructureInterface|MockObject
52
+ */
53
+ private $ indexerStructure ;
54
+ /**
55
+ * @var IndexScopeResolver|MockObject
56
+ */
57
+ private $ indexScopeResolver ;
58
+ /**
59
+ * @var FlatScopeResolver|MockObject
60
+ */
61
+ private $ flatScopeResolver ;
62
+ /**
63
+ * @var SaveHandlerFactory|MockObject
64
+ */
65
+ private $ saveHandlerFactory ;
66
+ /**
67
+ * @var StructureFactory|MockObject
68
+ */
69
+ private $ structureFactory ;
70
+ /**
71
+ * @var FieldsetInterface|MockObject
72
+ */
73
+ private $ indexerFieldset ;
74
+ /**
75
+ * @var FieldsetPool|MockObject
76
+ */
77
+ private $ fieldsetPool ;
78
+ /**
79
+ * @var HandlerInterface|MockObject
80
+ */
81
+ private $ indexerHandler ;
82
+ /**
83
+ * @var HandlerPool|MockObject
84
+ */
85
+ private $ handlerPool ;
86
+ /**
87
+ * @var Collection|MockObject
88
+ */
89
+ private $ collection ;
90
+ /**
91
+ * @var CollectionFactory|MockObject
92
+ */
93
+ private $ collectionFactory ;
29
94
30
95
protected function setUp (): void
31
96
{
32
- $ indexerStructure = $ this ->getMockBuilder (IndexStructureInterface::class)
97
+ $ this -> indexerStructure = $ this ->getMockBuilder (IndexStructureInterface::class)
33
98
->getMockForAbstractClass ();
34
- $ structureFactory = $ this ->getMockBuilder (StructureFactory::class)
99
+ $ this -> structureFactory = $ this ->getMockBuilder (StructureFactory::class)
35
100
->disableOriginalConstructor ()
36
101
->getMock ();
37
- $ structureFactory ->expects ($ this ->any ())
38
- ->method ('create ' )
39
- ->willReturn ($ indexerStructure );
40
-
41
- $ indexer = $ this ->getMockBuilder (IndexerInterface::class)
102
+ $ this ->resourceConnection = $ this ->getMockBuilder (ResourceConnection::class)
103
+ ->disableOriginalConstructor ()
104
+ ->getMock ();
105
+ $ this ->adapter = $ this ->getMockBuilder (AdapterInterface::class)
42
106
->getMockForAbstractClass ();
43
- $ saveHandlerFactory = $ this ->getMockBuilder (SaveHandlerFactory::class)
107
+ $ this ->batch = $ this ->getMockBuilder (Batch::class)
108
+ ->disableOriginalConstructor ()
109
+ ->getMock ();
110
+ $ this ->indexScopeResolver = $ this ->getMockBuilder (IndexScopeResolver::class)
111
+ ->disableOriginalConstructor ()
112
+ ->getMock ();
113
+ $ this ->flatScopeResolver = $ this ->getMockBuilder (FlatScopeResolver::class)
114
+ ->disableOriginalConstructor ()
115
+ ->getMock ();
116
+ $ this ->saveHandlerFactory = $ this ->getMockBuilder (SaveHandlerFactory::class)
117
+ ->disableOriginalConstructor ()
118
+ ->getMock ();
119
+ $ this ->fieldsetPool = $ this ->getMockBuilder (FieldsetPool::class)
44
120
->disableOriginalConstructor ()
45
121
->getMock ();
46
- $ saveHandlerFactory ->expects ($ this ->any ())
122
+ $ this ->collection = $ this ->getMockBuilder (Collection::class)
123
+ ->disableOriginalConstructor ()
124
+ ->getMock ();
125
+ $ this ->collectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
126
+ ->disableOriginalConstructor ()
127
+ ->getMock ();
128
+ $ this ->indexerHandler = $ this ->getMockBuilder (HandlerInterface::class)
129
+ ->getMockForAbstractClass ();
130
+ $ this ->handlerPool = $ this ->getMockBuilder (HandlerPool::class)
131
+ ->disableOriginalConstructor ()
132
+ ->getMock ();
133
+ $ this ->indexerFieldset = $ this ->getMockBuilder (FieldsetInterface::class)
134
+ ->getMockForAbstractClass ();
135
+ }
136
+
137
+ /**
138
+ * Generate flat index table name from design config grid index ID
139
+ *
140
+ * @return string
141
+ */
142
+ private function getFlatIndexTableName (): string
143
+ {
144
+ return DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID . '_flat ' ;
145
+ }
146
+
147
+ /**
148
+ * Initialize and return Design Config Indexer Model
149
+ *
150
+ * @return Config
151
+ */
152
+ private function getDesignConfigIndexerModel (): Config
153
+ {
154
+ $ this ->structureFactory ->expects ($ this ->any ())
155
+ ->method ('create ' )
156
+ ->willReturn ($ this ->indexerStructure );
157
+ $ this ->resourceConnection
158
+ ->expects ($ this ->any ())
159
+ ->method ('getConnection ' )
160
+ ->willReturn ($ this ->adapter );
161
+ $ this ->flatScopeResolver ->expects ($ this ->any ())
162
+ ->method ('resolve ' )
163
+ ->willReturn ($ this ->getFlatIndexTableName ());
164
+
165
+ $ indexer = new IndexerHandler (
166
+ $ this ->indexerStructure ,
167
+ $ this ->resourceConnection ,
168
+ $ this ->batch ,
169
+ $ this ->indexScopeResolver ,
170
+ $ this ->flatScopeResolver ,
171
+ [
172
+ 'fieldsets ' => [],
173
+ 'indexer_id ' => DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID
174
+ ]
175
+ );
176
+
177
+ $ this ->saveHandlerFactory ->expects ($ this ->any ())
47
178
->method ('create ' )
48
179
->willReturn ($ indexer );
49
180
50
- $ indexerFieldset = $ this ->getMockBuilder (FieldsetInterface::class)
51
- ->getMockForAbstractClass ();
52
- $ indexerFieldset ->expects ($ this ->any ())
181
+ $ this ->indexerFieldset ->expects ($ this ->any ())
53
182
->method ('addDynamicData ' )
54
183
->willReturnArgument (0 );
55
- $ fieldsetPool = $ this ->getMockBuilder (FieldsetPool::class)
56
- ->disableOriginalConstructor ()
57
- ->getMock ();
58
- $ fieldsetPool ->expects ($ this ->any ())
184
+
185
+ $ this ->fieldsetPool ->expects ($ this ->any ())
59
186
->method ('get ' )
60
- ->willReturn ($ indexerFieldset );
187
+ ->willReturn ($ this -> indexerFieldset );
61
188
62
- $ indexerHandler = $ this ->getMockBuilder (HandlerInterface::class)
63
- ->getMockForAbstractClass ();
64
- $ handlerPool = $ this ->getMockBuilder (HandlerPool::class)
65
- ->disableOriginalConstructor ()
66
- ->getMock ();
67
- $ handlerPool ->expects ($ this ->any ())
189
+ $ this ->handlerPool ->expects ($ this ->any ())
68
190
->method ('get ' )
69
- ->willReturn ($ indexerHandler );
191
+ ->willReturn ($ this -> indexerHandler );
70
192
71
- $ collection = $ this ->getMockBuilder (Collection::class)
72
- ->disableOriginalConstructor ()
73
- ->getMock ();
74
- $ collectionFactory =
75
- $ this ->getMockBuilder (\Magento \Theme \Model \ResourceModel \Design \Config \Scope \CollectionFactory::class)
76
- ->disableOriginalConstructor ()
77
- ->setMethods (['create ' ])
78
- ->getMock ();
79
- $ collectionFactory ->expects ($ this ->any ())
193
+ $ this ->collectionFactory ->expects ($ this ->any ())
80
194
->method ('create ' )
81
- ->willReturn ($ collection );
82
-
83
- $ this -> model = new Config (
84
- $ structureFactory ,
85
- $ saveHandlerFactory ,
86
- $ fieldsetPool ,
87
- $ handlerPool ,
88
- $ collectionFactory ,
195
+ ->willReturn ($ this -> collection );
196
+
197
+ return new Config (
198
+ $ this -> structureFactory ,
199
+ $ this -> saveHandlerFactory ,
200
+ $ this -> fieldsetPool ,
201
+ $ this -> handlerPool ,
202
+ $ this -> collectionFactory ,
89
203
[
90
204
'fieldsets ' => ['test_fieldset ' => [
91
205
'fields ' => [
@@ -102,7 +216,7 @@ protected function setUp(): void
102
216
'handler ' => null ,
103
217
],
104
218
],
105
- 'provider ' => $ indexerFieldset ,
219
+ 'provider ' => $ this -> indexerFieldset ,
106
220
]
107
221
],
108
222
'saveHandler ' => 'saveHandlerClass ' ,
@@ -111,9 +225,46 @@ protected function setUp(): void
111
225
);
112
226
}
113
227
114
- public function testExecuteFull ()
228
+ public function testFullReindex ()
115
229
{
116
- $ result = $ this ->model ->executeFull ();
117
- $ this ->assertNull ($ result );
230
+ $ this ->adapter ->expects ($ this ->any ())
231
+ ->method ('isTableExists ' )
232
+ ->willReturn (true );
233
+ $ this ->indexerStructure ->expects ($ this ->never ())->method ('create ' )
234
+ ->with (DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID );
235
+ $ this ->adapter ->expects ($ this ->once ())->method ('delete ' )
236
+ ->with ($ this ->getFlatIndexTableName ());
237
+ $ this ->batch ->expects ($ this ->any ())
238
+ ->method ('getItems ' )->willReturn ([]);
239
+
240
+ $ this ->getDesignConfigIndexerModel ()->executeFull ();
241
+ }
242
+
243
+ public function testFullReindexWithFlatTableCreate ()
244
+ {
245
+ $ this ->adapter ->expects ($ this ->any ())->method ('isTableExists ' )
246
+ ->willReturn (false );
247
+ $ this ->indexerStructure ->expects ($ this ->once ())->method ('create ' )
248
+ ->with (DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID );
249
+ $ this ->adapter ->expects ($ this ->never ())->method ('delete ' )
250
+ ->with ($ this ->getFlatIndexTableName ());
251
+ $ this ->batch ->expects ($ this ->any ())->method ('getItems ' )
252
+ ->willReturn ([]);
253
+
254
+ $ this ->getDesignConfigIndexerModel ()->executeFull ();
255
+ }
256
+
257
+ public function testPartialReindex ()
258
+ {
259
+ $ this ->adapter ->expects ($ this ->any ())->method ('isTableExists ' )
260
+ ->willReturn (true );
261
+ $ this ->indexerStructure ->expects ($ this ->never ())->method ('create ' )
262
+ ->with (DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID );
263
+ $ this ->adapter ->expects ($ this ->once ())->method ('delete ' )
264
+ ->with ($ this ->getFlatIndexTableName (), ['entity_id IN(?) ' => [1 , 2 , 3 ]]);
265
+ $ this ->batch ->expects ($ this ->any ())->method ('getItems ' )
266
+ ->willReturn ([[1 , 2 , 3 ]]);
267
+
268
+ $ this ->getDesignConfigIndexerModel ()->executeList ([1 , 2 , 3 ]);
118
269
}
119
270
}
0 commit comments