8
8
9
9
namespace Magento \WebapiAsync \Model ;
10
10
11
- use Magento \Catalog \Api \Data \ProductInterface as Product ;
12
- use Magento \TestFramework \MessageQueue \PreconditionFailedException ;
13
- use Magento \TestFramework \MessageQueue \PublisherConsumerController ;
14
- use Magento \TestFramework \MessageQueue \EnvironmentPreconditionException ;
15
- use Magento \TestFramework \TestCase \WebapiAbstract ;
16
- use Magento \TestFramework \Helper \Bootstrap ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
17
13
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
+ use Magento \Framework \ObjectManagerInterface ;
18
15
use Magento \Framework \Phrase ;
19
16
use Magento \Framework \Registry ;
20
17
use Magento \Framework \Webapi \Exception ;
21
- use Magento \Catalog \Api \ProductRepositoryInterface ;
22
- use Magento \Framework \ObjectManagerInterface ;
23
- use Magento \Store \Model \Store ;
24
18
use Magento \Framework \Webapi \Rest \Request ;
19
+ use Magento \Store \Model \Store ;
20
+ use Magento \TestFramework \Helper \Bootstrap ;
21
+ use Magento \TestFramework \MessageQueue \EnvironmentPreconditionException ;
22
+ use Magento \TestFramework \MessageQueue \PreconditionFailedException ;
23
+ use Magento \TestFramework \MessageQueue \PublisherConsumerController ;
24
+ use Magento \TestFramework \TestCase \WebapiAbstract ;
25
25
26
26
/**
27
27
* Check async request for multistore product creation service, scheduling bulk
@@ -86,27 +86,24 @@ class AsyncScheduleMultiStoreTest extends WebapiAbstract
86
86
*/
87
87
private $ registry ;
88
88
89
+ /**
90
+ * @inheritDoc
91
+ */
89
92
protected function setUp (): void
90
93
{
94
+ $ logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt " ;
91
95
$ this ->objectManager = Bootstrap::getObjectManager ();
92
- $ this ->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt " ;
93
96
$ this ->registry = $ this ->objectManager ->get (Registry::class);
94
97
95
- $ params = array_merge_recursive (
96
- Bootstrap::getInstance ()->getAppInitParams (),
97
- ['MAGE_DIRS ' => ['cache ' => ['path ' => TESTS_TEMP_DIR . '/cache ' ]]]
98
- );
99
-
100
- /** @var PublisherConsumerController publisherConsumerController */
101
98
$ this ->publisherConsumerController = $ this ->objectManager ->create (
102
99
PublisherConsumerController::class,
103
100
[
104
101
'consumers ' => $ this ->consumers ,
105
- 'logFilePath ' => $ this -> logFilePath ,
106
- 'appInitParams ' => $ params ,
102
+ 'logFilePath ' => $ logFilePath ,
103
+ 'appInitParams ' => Bootstrap:: getInstance ()-> getAppInitParams () ,
107
104
]
108
105
);
109
- $ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
106
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
110
107
111
108
try {
112
109
$ this ->publisherConsumerController ->initialize ();
@@ -124,21 +121,16 @@ protected function setUp(): void
124
121
/**
125
122
* @dataProvider storeProvider
126
123
* @magentoApiDataFixture Magento/Store/_files/core_fixturestore.php
124
+ * @param string|null $storeCode
125
+ * @return void
127
126
*/
128
- public function testAsyncScheduleBulkMultistore ($ storeCode )
127
+ public function testAsyncScheduleBulkMultistore (? string $ storeCode ): void
129
128
{
130
- if ($ storeCode === self ::STORE_CODE_FROM_FIXTURE ) {
131
- /** @var \Magento\Config\Model\Config $config */
132
- $ config = Bootstrap::getObjectManager ()->get (\Magento \Config \Model \Config::class);
133
- if (strpos ($ config ->getConfigDataValue ('catalog/search/engine ' ), 'elasticsearch ' ) !== false ) {
134
- $ this ->markTestSkipped ('MC-20452 ' );
135
- }
136
- }
137
129
$ product = $ this ->getProductData ();
138
130
$ this ->_markTestAsRestOnly ();
139
131
140
132
/** @var Store $store */
141
- $ store = $ this ->objectManager ->create (Store::class);
133
+ $ store = $ this ->objectManager ->get (Store::class);
142
134
$ store ->load (self ::STORE_CODE_FROM_FIXTURE );
143
135
$ this ->assertEquals (
144
136
self ::STORE_NAME_FROM_FIXTURE ,
@@ -147,9 +139,9 @@ public function testAsyncScheduleBulkMultistore($storeCode)
147
139
);
148
140
149
141
try {
150
- /** @var Product $productModel */
142
+ /** @var ProductInterface $productModel */
151
143
$ productModel = $ this ->objectManager ->create (
152
- Product ::class,
144
+ ProductInterface ::class,
153
145
['data ' => $ product ['product ' ]]
154
146
);
155
147
$ this ->productRepository ->save ($ productModel );
@@ -161,16 +153,21 @@ public function testAsyncScheduleBulkMultistore($storeCode)
161
153
$ this ->clearProducts ();
162
154
}
163
155
164
- private function asyncScheduleAndTest ($ product , $ storeCode = null )
156
+ /**
157
+ * @param array $product
158
+ * @param string|null $storeCode
159
+ * @return void
160
+ */
161
+ private function asyncScheduleAndTest (array $ product , $ storeCode = null ): void
165
162
{
166
- $ sku = $ product ['product ' ][Product ::SKU ];
167
- $ productName = $ product ['product ' ][Product ::NAME ];
168
- $ newProductName = $ product ['product ' ][Product ::NAME ] . $ storeCode ;
163
+ $ sku = $ product ['product ' ][ProductInterface ::SKU ];
164
+ $ productName = $ product ['product ' ][ProductInterface ::NAME ];
165
+ $ newProductName = $ product ['product ' ][ProductInterface ::NAME ] . $ storeCode ;
169
166
170
167
$ this ->skus [] = $ sku ;
171
168
172
- $ product ['product ' ][Product ::NAME ] = $ newProductName ;
173
- $ product ['product ' ][Product ::TYPE_ID ] = 'virtual ' ;
169
+ $ product ['product ' ][ProductInterface ::NAME ] = $ newProductName ;
170
+ $ product ['product ' ][ProductInterface ::TYPE_ID ] = 'virtual ' ;
174
171
175
172
$ response = $ this ->updateProductAsync ($ product , $ sku , $ storeCode );
176
173
@@ -197,14 +194,14 @@ private function asyncScheduleAndTest($product, $storeCode = null)
197
194
$ serviceInfo = [
198
195
'rest ' => [
199
196
'resourcePath ' => self ::REST_RESOURCE_PATH . '/ ' . $ sku ,
200
- 'httpMethod ' => Request::HTTP_METHOD_GET
197
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
201
198
]
202
199
];
203
200
$ storeResponse = $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ checkingStore );
204
201
if ($ checkingStore == $ storeCode || $ storeCode == self ::STORE_CODE_ALL ) {
205
202
$ this ->assertEquals (
206
203
$ newProductName ,
207
- $ storeResponse [Product ::NAME ],
204
+ $ storeResponse [ProductInterface ::NAME ],
208
205
sprintf (
209
206
'Product name in %s store is invalid after updating in store %s. ' ,
210
207
$ checkingStore ,
@@ -214,7 +211,7 @@ private function asyncScheduleAndTest($product, $storeCode = null)
214
211
} else {
215
212
$ this ->assertEquals (
216
213
$ productName ,
217
- $ storeResponse [Product ::NAME ],
214
+ $ storeResponse [ProductInterface ::NAME ],
218
215
sprintf (
219
216
'Product name in %s store is invalid after updating in store %s. ' ,
220
217
$ checkingStore ,
@@ -225,14 +222,20 @@ private function asyncScheduleAndTest($product, $storeCode = null)
225
222
}
226
223
}
227
224
225
+ /**
226
+ * @inheritDoc
227
+ */
228
228
protected function tearDown (): void
229
229
{
230
230
$ this ->clearProducts ();
231
231
$ this ->publisherConsumerController ->stopConsumers ();
232
232
parent ::tearDown ();
233
233
}
234
234
235
- private function clearProducts ()
235
+ /**
236
+ * @return void
237
+ */
238
+ private function clearProducts (): void
236
239
{
237
240
$ size = $ this ->objectManager ->create (Collection::class)
238
241
->addAttributeToFilter ('sku ' , ['in ' => $ this ->skus ])
@@ -270,7 +273,7 @@ private function clearProducts()
270
273
/**
271
274
* @return array
272
275
*/
273
- public function getProductData ()
276
+ public function getProductData (): array
274
277
{
275
278
$ productBuilder = function ($ data ) {
276
279
return array_replace_recursive (
@@ -280,23 +283,26 @@ public function getProductData()
280
283
};
281
284
282
285
return [
283
- 'product ' =>
284
- $ productBuilder (
285
- [
286
- Product::TYPE_ID => 'simple ' ,
287
- Product::SKU => 'multistore-sku-test-1 ' ,
288
- Product::NAME => 'Test Name ' ,
289
- ]
290
- ),
286
+ 'product ' => $ productBuilder (
287
+ [
288
+ ProductInterface::TYPE_ID => 'simple ' ,
289
+ ProductInterface::SKU => 'multistore-sku-test-1 ' ,
290
+ ProductInterface::NAME => 'Test Name ' ,
291
+ ]
292
+ ),
291
293
];
292
294
}
293
295
294
- public function storeProvider ()
296
+ /**
297
+ * @return array
298
+ */
299
+ public function storeProvider (): array
295
300
{
296
301
$ dataSets = [];
297
302
foreach ($ this ->stores as $ store ) {
298
303
$ dataSets [$ store ] = [$ store ];
299
304
}
305
+
300
306
return $ dataSets ;
301
307
}
302
308
@@ -306,28 +312,28 @@ public function storeProvider()
306
312
* @param array $productData
307
313
* @return array
308
314
*/
309
- private function getSimpleProductData ($ productData = [])
315
+ private function getSimpleProductData ($ productData = []): array
310
316
{
311
317
return [
312
- Product::SKU => isset ($ productData [Product::SKU ])
313
- ? $ productData [Product::SKU ] : uniqid ('sku- ' , true ),
314
- Product::NAME => isset ($ productData [Product::NAME ])
315
- ? $ productData [Product::NAME ] : uniqid ('sku- ' , true ),
316
- Product::VISIBILITY => 4 ,
317
- Product::TYPE_ID => 'simple ' ,
318
- Product::PRICE => 3.62 ,
319
- Product::STATUS => 1 ,
320
- Product::TYPE_ID => 'simple ' ,
321
- Product::ATTRIBUTE_SET_ID => 4 ,
318
+ ProductInterface::SKU => isset ($ productData [ProductInterface::SKU ])
319
+ ? $ productData [ProductInterface::SKU ] : uniqid ('sku- ' , true ),
320
+ ProductInterface::NAME => isset ($ productData [ProductInterface::NAME ])
321
+ ? $ productData [ProductInterface::NAME ] : uniqid ('sku- ' , true ),
322
+ ProductInterface::VISIBILITY => 4 ,
323
+ ProductInterface::TYPE_ID => 'simple ' ,
324
+ ProductInterface::PRICE => 3.62 ,
325
+ ProductInterface::STATUS => 1 ,
326
+ ProductInterface::ATTRIBUTE_SET_ID => 4 ,
322
327
];
323
328
}
324
329
325
330
/**
326
- * @param $requestData
331
+ * @param array $requestData
332
+ * @param string $sku
327
333
* @param string|null $storeCode
328
334
* @return mixed
329
335
*/
330
- private function updateProductAsync ($ requestData , $ sku , $ storeCode = null )
336
+ private function updateProductAsync (array $ requestData , string $ sku , $ storeCode = null )
331
337
{
332
338
$ serviceInfo = [
333
339
'rest ' => [
@@ -339,40 +345,19 @@ private function updateProductAsync($requestData, $sku, $storeCode = null)
339
345
return $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ storeCode );
340
346
}
341
347
342
- public function assertProductCreation ($ product )
348
+ /**
349
+ * @param array $product
350
+ * @return bool
351
+ */
352
+ public function assertProductCreation (array $ product ): bool
343
353
{
344
- $ sku = $ product ['product ' ][Product ::SKU ];
354
+ $ sku = $ product ['product ' ][ProductInterface ::SKU ];
345
355
$ collection = $ this ->objectManager ->create (Collection::class)
346
- ->addAttributeToFilter (Product ::SKU , ['eq ' => $ sku ])
347
- ->addAttributeToFilter (Product ::TYPE_ID , ['eq ' => 'virtual ' ])
356
+ ->addAttributeToFilter (ProductInterface ::SKU , ['eq ' => $ sku ])
357
+ ->addAttributeToFilter (ProductInterface ::TYPE_ID , ['eq ' => 'virtual ' ])
348
358
->load ();
349
359
$ size = $ collection ->getSize ();
350
360
351
361
return $ size > 0 ;
352
362
}
353
-
354
- /**
355
- * Remove test store
356
- * //phpcs:disable
357
- */
358
- public static function tearDownAfterClass (): void
359
- {
360
- parent ::tearDownAfterClass ();
361
- //phpcs:enable
362
- /** @var Registry $registry */
363
- $ registry = Bootstrap::getObjectManager ()->get (Registry::class);
364
-
365
- $ registry ->unregister ('isSecureArea ' );
366
- $ registry ->register ('isSecureArea ' , true );
367
-
368
- /** @var Store $store*/
369
- $ store = Bootstrap::getObjectManager ()->create (Store::class);
370
- $ store ->load ('fixturestore ' );
371
- if ($ store ->getId ()) {
372
- $ store ->delete ();
373
- }
374
-
375
- $ registry ->unregister ('isSecureArea ' );
376
- $ registry ->register ('isSecureArea ' , false );
377
- }
378
363
}
0 commit comments