11
11
use Magento \Eav \Setup \EavSetup ;
12
12
use Magento \Eav \Setup \EavSetupFactory ;
13
13
use Magento \Framework \DB \Adapter \AdapterInterface ;
14
+ use Magento \Framework \DB \Query \BatchIteratorInterface ;
15
+ use Magento \Framework \DB \Query \Generator ;
14
16
use Magento \Framework \DB \Select ;
15
17
use Magento \Framework \Setup \ModuleDataSetupInterface ;
18
+ use PHPUnit \Framework \MockObject \MockObject ;
16
19
use PHPUnit \Framework \TestCase ;
17
20
18
21
class UpdateMultiselectAttributesBackendTypesTest extends TestCase
19
22
{
20
23
/**
21
- * @var ModuleDataSetupInterface|\PHPUnit\Framework\MockObject\ MockObject
24
+ * @var ModuleDataSetupInterface|MockObject
22
25
*/
23
26
private $ dataSetup ;
24
27
25
28
/**
26
- * @var EavSetupFactory|\PHPUnit\Framework\MockObject\ MockObject
29
+ * @var EavSetupFactory|MockObject
27
30
*/
28
31
private $ eavSetupFactory ;
29
32
33
+ /**
34
+ * @var Generator|MockObject
35
+ */
36
+ private $ batchQueryGenerator ;
37
+
30
38
/**
31
39
* @var UpdateMultiselectAttributesBackendTypes
32
40
*/
@@ -40,35 +48,64 @@ protected function setUp(): void
40
48
parent ::setUp ();
41
49
$ this ->dataSetup = $ this ->createMock (ModuleDataSetupInterface::class);
42
50
$ this ->eavSetupFactory = $ this ->createMock (EavSetupFactory::class);
43
- $ this ->model = new UpdateMultiselectAttributesBackendTypes ($ this ->dataSetup , $ this ->eavSetupFactory );
51
+ $ this ->batchQueryGenerator = $ this ->createMock (Generator::class);
52
+ $ this ->model = new UpdateMultiselectAttributesBackendTypes (
53
+ $ this ->dataSetup ,
54
+ $ this ->eavSetupFactory ,
55
+ $ this ->batchQueryGenerator
56
+ );
44
57
}
45
58
59
+ /**
60
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
61
+ * @return void
62
+ */
46
63
public function testApply (): void
47
64
{
48
65
$ attributeIds = [3 , 7 ];
49
66
$ entityTypeId = 4 ;
50
67
$ eavSetup = $ this ->createMock (EavSetup::class);
51
68
$ connection = $ this ->createMock (AdapterInterface::class);
52
- $ select1 = $ this ->createMock (Select::class);
53
- $ select2 = $ this ->createMock (Select::class);
54
- $ select3 = $ this ->createMock (Select::class);
55
- $ this ->eavSetupFactory ->method ('create ' )
69
+ $ selectAttributes = $ this ->createMock (Select::class);
70
+ $ selectAttributesValues = $ this ->createMock (Select::class);
71
+ $ selectForInsert1 = $ this ->createMock (Select::class);
72
+ $ selectForInsert2 = $ this ->createMock (Select::class);
73
+ $ selectForDelete1 = $ this ->createMock (Select::class);
74
+ $ selectForDelete2 = $ this ->createMock (Select::class);
75
+ $ batchIterator = $ this ->getMockForAbstractClass (BatchIteratorInterface::class);
76
+ $ batchIterator ->method ('current ' )
77
+ ->willReturnOnConsecutiveCalls ($ selectForInsert1 , $ selectForInsert2 );
78
+ $ batchIterator ->method ('valid ' )
79
+ ->willReturnOnConsecutiveCalls (true , true , false );
80
+ $ this ->eavSetupFactory ->expects ($ this ->once ())
81
+ ->method ('create ' )
56
82
->willReturn ($ eavSetup );
57
- $ this ->dataSetup ->method ('getConnection ' )
83
+ $ this ->dataSetup ->expects ($ this ->once ())
84
+ ->method ('getConnection ' )
58
85
->willReturn ($ connection );
59
86
$ this ->dataSetup ->method ('getTable ' )
60
87
->willReturnArgument (0 );
88
+ $ this ->batchQueryGenerator ->expects ($ this ->once ())
89
+ ->method ('generate ' )
90
+ ->willReturn ($ batchIterator );
61
91
$ eavSetup ->method ('getEntityTypeId ' )
62
92
->willReturn (4 );
63
- $ eavSetup ->method ('updateAttribute ' )
93
+ $ eavSetup ->expects ($ this ->exactly (2 ))
94
+ ->method ('updateAttribute ' )
64
95
->withConsecutive (
65
96
[$ entityTypeId , 3 , 'backend_type ' , 'text ' ],
66
97
[$ entityTypeId , 7 , 'backend_type ' , 'text ' ]
67
98
);
68
- $ connection ->expects ($ this ->exactly (3 ))
99
+ $ connection ->expects ($ this ->exactly (4 ))
69
100
->method ('select ' )
70
- ->willReturnOnConsecutiveCalls ($ select1 , $ select2 , $ select3 );
71
- $ connection ->method ('describeTable ' )
101
+ ->willReturnOnConsecutiveCalls (
102
+ $ selectAttributes ,
103
+ $ selectAttributesValues ,
104
+ $ selectForDelete1 ,
105
+ $ selectForDelete2
106
+ );
107
+ $ connection ->expects ($ this ->once ())
108
+ ->method ('describeTable ' )
72
109
->willReturn (
73
110
[
74
111
'value_id ' => [],
@@ -78,35 +115,78 @@ public function testApply(): void
78
115
'row_id ' => [],
79
116
]
80
117
);
81
- $ connection ->method ('fetchCol ' )
82
- ->with ($ select1 )
118
+ $ connection ->expects ($ this ->once ())
119
+ ->method ('fetchCol ' )
120
+ ->with ($ selectAttributes )
83
121
->willReturn ($ attributeIds );
84
- $ connection ->method ('insertFromSelect ' )
85
- ->with ($ select3 , 'catalog_product_entity_text ' , ['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ])
122
+ $ connection ->expects ($ this ->exactly (2 ))
123
+ ->method ('insertFromSelect ' )
124
+ ->withConsecutive (
125
+ [$ selectForInsert1 , 'catalog_product_entity_text ' , ['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ]],
126
+ [$ selectForInsert2 , 'catalog_product_entity_text ' , ['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ]],
127
+ )
86
128
->willReturn ('' );
87
- $ connection ->method ('deleteFromSelect ' )
88
- ->with ($ select2 , 'catalog_product_entity_varchar ' )
129
+ $ connection ->expects ($ this ->exactly (2 ))
130
+ ->method ('delete ' )
131
+ ->withConsecutive (
132
+ ['catalog_product_entity_varchar ' , 'value_id IN (select1) ' ],
133
+ ['catalog_product_entity_varchar ' , 'value_id IN (select2) ' ],
134
+ )
89
135
->willReturn ('' );
90
- $ select1 ->method ('from ' )
136
+ $ selectAttributes ->expects ($ this ->once ())
137
+ ->method ('from ' )
91
138
->with ('eav_attribute ' , ['attribute_id ' ])
92
139
->willReturnSelf ();
93
- $ select1 ->method ('where ' )
140
+ $ selectAttributes ->expects ($ this ->exactly (3 ))
141
+ ->method ('where ' )
94
142
->withConsecutive (
95
143
['entity_type_id = ? ' , $ entityTypeId ],
96
144
['backend_type = ? ' , 'varchar ' ],
97
145
['frontend_input = ? ' , 'multiselect ' ]
98
146
)
99
147
->willReturnSelf ();
100
- $ select2 ->method ('from ' )
101
- ->with ('catalog_product_entity_varchar ' )
148
+ $ selectForInsert1 ->expects ($ this ->exactly (2 ))
149
+ ->method ('reset ' )
150
+ ->with (Select::COLUMNS )
102
151
->willReturnSelf ();
103
- $ select2 ->method ('where ' )
104
- ->with ('attribute_id in (?) ' , $ attributeIds )
152
+ $ selectForInsert1 ->expects ($ this ->exactly (2 ))
153
+ ->method ('columns ' )
154
+ ->withConsecutive (
155
+ [['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ]],
156
+ ['value_id ' ]
157
+ )
158
+ ->willReturnSelf ();
159
+ $ selectForInsert2 ->expects ($ this ->exactly (2 ))
160
+ ->method ('reset ' )
161
+ ->with (Select::COLUMNS )
162
+ ->willReturnSelf ();
163
+ $ selectForInsert2 ->expects ($ this ->exactly (2 ))
164
+ ->method ('columns ' )
165
+ ->withConsecutive (
166
+ [['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ]],
167
+ ['value_id ' ]
168
+ )
169
+ ->willReturnSelf ();
170
+ $ selectForDelete1 ->expects ($ this ->once ())
171
+ ->method ('from ' )
172
+ ->with ($ selectForInsert1 , 'value_id ' )
173
+ ->willReturnSelf ();
174
+ $ selectForDelete1 ->expects ($ this ->once ())
175
+ ->method ('assemble ' )
176
+ ->willReturn ('select1 ' );
177
+ $ selectForDelete2 ->expects ($ this ->once ())
178
+ ->method ('from ' )
179
+ ->with ($ selectForInsert2 , 'value_id ' )
105
180
->willReturnSelf ();
106
- $ select3 ->method ('from ' )
107
- ->with ('catalog_product_entity_varchar ' , ['attribute_id ' , 'store_id ' , 'value ' , 'row_id ' ])
181
+ $ selectForDelete2 ->expects ($ this ->once ())
182
+ ->method ('assemble ' )
183
+ ->willReturn ('select2 ' );
184
+ $ selectAttributesValues ->expects ($ this ->once ())
185
+ ->method ('from ' )
186
+ ->with ('catalog_product_entity_varchar ' , '* ' )
108
187
->willReturnSelf ();
109
- $ select3 ->method ('where ' )
188
+ $ selectAttributesValues ->expects ($ this ->once ())
189
+ ->method ('where ' )
110
190
->with ('attribute_id in (?) ' , $ attributeIds )
111
191
->willReturnSelf ();
112
192
$ this ->model ->apply ();
0 commit comments