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