16
16
use Magento \Framework \View \LayoutInterface ;
17
17
use Magento \Store \Model \StoreManagerInterface ;
18
18
use Magento \TestFramework \Helper \Bootstrap ;
19
+ use Magento \TestFramework \Store \ExecuteInStoreContext ;
19
20
use PHPUnit \Framework \TestCase ;
20
21
21
22
/**
@@ -34,9 +35,6 @@ class MultiStoreConfigurableViewOnProductPageTest extends TestCase
34
35
/** @var StoreManagerInterface */
35
36
private $ storeManager ;
36
37
37
- /** @var Configurable */
38
- private $ block ;
39
-
40
38
/** @var LayoutInterface */
41
39
private $ layout ;
42
40
@@ -46,6 +44,9 @@ class MultiStoreConfigurableViewOnProductPageTest extends TestCase
46
44
/** @var ProductResource */
47
45
private $ productResource ;
48
46
47
+ /** @var ExecuteInStoreContext */
48
+ private $ executeInStoreContext ;
49
+
49
50
/**
50
51
* @inheritdoc
51
52
*/
@@ -58,9 +59,9 @@ protected function setUp()
58
59
$ this ->productRepository ->cleanCache ();
59
60
$ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
60
61
$ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
61
- $ this ->block = $ this ->layout ->createBlock (Configurable::class);
62
62
$ this ->serializer = $ this ->objectManager ->get (SerializerInterface::class);
63
63
$ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
64
+ $ this ->executeInStoreContext = $ this ->objectManager ->get (ExecuteInStoreContext::class);
64
65
}
65
66
66
67
/**
@@ -74,8 +75,12 @@ protected function setUp()
74
75
*/
75
76
public function testMultiStoreLabelView (array $ expectedStoreData , array $ expectedSecondStoreData ): void
76
77
{
77
- $ this ->assertProductLabelConfigDataPerStore ($ expectedStoreData );
78
- $ this ->assertProductLabelConfigDataPerStore ($ expectedSecondStoreData , 'fixturestore ' , true );
78
+ $ this ->executeInStoreContext ->execute ([$ this , 'assertProductLabel ' ], [$ expectedStoreData ]);
79
+ $ this ->executeInStoreContext ->execute (
80
+ [$ this , 'assertProductLabel ' ],
81
+ [$ expectedSecondStoreData ],
82
+ 'fixturestore '
83
+ );
79
84
}
80
85
81
86
/**
@@ -111,6 +116,20 @@ public function expectedLabelsDataProvider(): array
111
116
];
112
117
}
113
118
119
+ /**
120
+ * Assert configurable product labels config
121
+ *
122
+ * @param $expectedStoreData
123
+ * @return void
124
+ */
125
+ public function assertProductLabel ($ expectedStoreData ): void
126
+ {
127
+ $ product = $ this ->productRepository ->get ('configurable ' , false , null , true );
128
+ $ config = $ this ->getBlockConfig ($ product )['attributes ' ] ?? null ;
129
+ $ this ->assertNotNull ($ config );
130
+ $ this ->assertAttributeConfig ($ expectedStoreData , reset ($ config ));
131
+ }
132
+
114
133
/**
115
134
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_two_websites.php
116
135
*
@@ -123,8 +142,12 @@ public function expectedLabelsDataProvider(): array
123
142
public function testMultiStoreOptionsView (array $ expectedProducts , array $ expectedSecondStoreProducts ): void
124
143
{
125
144
$ this ->prepareConfigurableProduct ('configurable ' , 'fixture_second_store ' );
126
- $ this ->assertProductConfigPerStore ($ expectedProducts );
127
- $ this ->assertProductConfigPerStore ($ expectedSecondStoreProducts , 'fixture_second_store ' , true );
145
+ $ this ->executeInStoreContext ->execute ([$ this , 'assertProductConfig ' ], [$ expectedProducts ]);
146
+ $ this ->executeInStoreContext ->execute (
147
+ [$ this , 'assertProductConfig ' ],
148
+ [$ expectedSecondStoreProducts ],
149
+ 'fixture_second_store '
150
+ );
128
151
}
129
152
130
153
/**
@@ -140,6 +163,20 @@ public function expectedProductDataProvider(): array
140
163
];
141
164
}
142
165
166
+ /**
167
+ * Assert configurable product config
168
+ *
169
+ * @param $expectedProducts
170
+ * @return void
171
+ */
172
+ public function assertProductConfig ($ expectedProducts ): void
173
+ {
174
+ $ product = $ this ->productRepository ->get ('configurable ' , false , null , true );
175
+ $ config = $ this ->getBlockConfig ($ product )['index ' ] ?? null ;
176
+ $ this ->assertNotNull ($ config );
177
+ $ this ->assertProducts ($ expectedProducts , $ config );
178
+ }
179
+
143
180
/**
144
181
* Prepare configurable product to test
145
182
*
@@ -150,9 +187,10 @@ public function expectedProductDataProvider(): array
150
187
private function prepareConfigurableProduct (string $ sku , string $ storeCode ): void
151
188
{
152
189
$ product = $ this ->productRepository ->get ($ sku , false , null , true );
153
- $ productToUpdate = $ product ->getTypeInstance ()->getUsedProductCollection ($ product )->getFirstItem ();
190
+ $ productToUpdate = $ product ->getTypeInstance ()->getUsedProductCollection ($ product )
191
+ ->setPageSize (1 )->getFirstItem ();
154
192
$ this ->assertNotEmpty ($ productToUpdate ->getData (), 'Configurable product does not have a child ' );
155
- $ this ->setProductDisabledPerStore ( $ productToUpdate , $ storeCode );
193
+ $ this ->executeInStoreContext -> execute ([ $ this , ' setProductDisabled ' ], [ $ productToUpdate] , $ storeCode );
156
194
}
157
195
158
196
/**
@@ -162,7 +200,7 @@ private function prepareConfigurableProduct(string $sku, string $storeCode): voi
162
200
* @param array $config
163
201
* @return void
164
202
*/
165
- private function assertProductConfig (array $ expectedProducts , array $ config ): void
203
+ private function assertProducts (array $ expectedProducts , array $ config ): void
166
204
{
167
205
$ this ->assertCount (count ($ expectedProducts ), $ config );
168
206
$ idsBySkus = $ this ->productResource ->getProductsIdsBySkus ($ expectedProducts );
@@ -179,102 +217,24 @@ private function assertProductConfig(array $expectedProducts, array $config): vo
179
217
* @param string $storeCode
180
218
* @return void
181
219
*/
182
- private function setProductDisabledPerStore (ProductInterface $ product, string $ storeCode ): void
220
+ public function setProductDisabled (ProductInterface $ product ): void
183
221
{
184
- $ currentStore = $ this ->storeManager ->getStore ();
185
- try {
186
- $ this ->storeManager ->setCurrentStore ($ storeCode );
187
- $ product ->setStatus (Status::STATUS_DISABLED );
188
- $ this ->productRepository ->save ($ product );
189
- } finally {
190
- $ this ->storeManager ->setCurrentStore ($ currentStore );
191
- }
192
- }
193
-
194
- /**
195
- * Assert configurable product config per stores
196
- *
197
- * @param array $expectedProducts
198
- * @param string $storeCode
199
- * @param bool $refreshBlock
200
- * @return void
201
- */
202
- private function assertProductConfigPerStore (
203
- array $ expectedProducts ,
204
- string $ storeCode = 'default ' ,
205
- bool $ refreshBlock = false
206
- ): void {
207
- $ currentStore = $ this ->storeManager ->getStore ();
208
- try {
209
- if ($ currentStore ->getCode () !== $ storeCode ) {
210
- $ this ->storeManager ->setCurrentStore ($ storeCode );
211
- }
212
- $ product = $ this ->productRepository ->get ('configurable ' , false , null , true );
213
- $ config = $ this ->getBlockConfig ($ product , $ refreshBlock )['index ' ] ?? null ;
214
- $ this ->assertNotNull ($ config );
215
- $ this ->assertProductConfig ($ expectedProducts , $ config );
216
- } finally {
217
- if ($ currentStore ->getCode () !== $ storeCode ) {
218
- $ this ->storeManager ->setCurrentStore ($ currentStore );
219
- }
220
- }
221
- }
222
-
223
- /**
224
- * Assert configurable product labels config per stores
225
- *
226
- * @param array $expectedStoreData
227
- * @param string $storeCode
228
- * @param bool $refreshBlock
229
- * @return void
230
- */
231
- private function assertProductLabelConfigDataPerStore (
232
- array $ expectedStoreData ,
233
- string $ storeCode = 'default ' ,
234
- bool $ refreshBlock = false
235
- ): void {
236
- $ currentStore = $ this ->storeManager ->getStore ();
237
- try {
238
- if ($ currentStore ->getCode () !== $ storeCode ) {
239
- $ this ->storeManager ->setCurrentStore ($ storeCode );
240
- }
241
- $ product = $ this ->productRepository ->get ('configurable ' , false , null , true );
242
- $ config = $ this ->getBlockConfig ($ product , $ refreshBlock )['attributes ' ] ?? null ;
243
- $ this ->assertNotNull ($ config );
244
- $ this ->assertAttributeConfig ($ expectedStoreData , reset ($ config ));
245
- } finally {
246
- if ($ currentStore ->getCode () !== $ storeCode ) {
247
- $ this ->storeManager ->setCurrentStore ($ currentStore );
248
- }
249
- }
250
- }
251
-
252
- /**
253
- * Get view block
254
- *
255
- * @param bool $refresh
256
- * @return Configurable
257
- */
258
- private function getBlock (bool $ refresh = false ): Configurable
259
- {
260
- if ($ refresh ) {
261
- $ this ->block = $ this ->layout ->createBlock (Configurable::class);
262
- }
263
- return $ this ->block ;
222
+ $ product ->setStatus (Status::STATUS_DISABLED );
223
+ $ this ->productRepository ->save ($ product );
264
224
}
265
225
266
226
/**
267
227
* Get block config
268
228
*
269
229
* @param ProductInterface $product
270
- * @param bool $refreshBlock
271
230
* @return array
272
231
*/
273
- private function getBlockConfig (ProductInterface $ product, bool $ refreshBlock ): array
232
+ private function getBlockConfig (ProductInterface $ product ): array
274
233
{
275
- $ this ->getBlock ($ refreshBlock )->setProduct ($ product );
234
+ $ block = $ this ->layout ->createBlock (Configurable::class);
235
+ $ block ->setProduct ($ product );
276
236
277
- return $ this ->serializer ->unserialize ($ this -> getBlock () ->getJsonConfig ());
237
+ return $ this ->serializer ->unserialize ($ block ->getJsonConfig ());
278
238
}
279
239
280
240
/**
0 commit comments