Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AsyncBulkScheduleTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
24 
35 {
36  const SERVICE_NAME = 'catalogProductRepositoryV1';
37  const SERVICE_VERSION = 'V1';
38  const REST_RESOURCE_PATH = '/V1/products';
39  const ASYNC_BULK_RESOURCE_PATH = '/async/bulk/V1/products';
40  const ASYNC_CONSUMER_NAME = 'async.operations.all';
41 
42  const KEY_TIER_PRICES = 'tier_prices';
43  const KEY_SPECIAL_PRICE = 'special_price';
44  const KEY_CATEGORY_LINKS = 'category_links';
45 
46  const BULK_UUID_KEY = 'bulk_uuid';
47 
48  protected $consumers = [
50  ];
51 
55  private $skus = [];
56 
60  private $publisherConsumerController;
61 
65  private $productRepository;
66 
70  private $objectManager;
71 
75  private $registry;
76 
77  protected function setUp()
78  {
79  $this->objectManager = Bootstrap::getObjectManager();
80  $this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
81  $this->registry = $this->objectManager->get(Registry::class);
82 
83  $params = array_merge_recursive(
84  \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams(),
85  ['MAGE_DIRS' => ['cache' => ['path' => TESTS_TEMP_DIR . '/cache']]]
86  );
87 
89  $this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
90  'consumers' => $this->consumers,
91  'logFilePath' => $this->logFilePath,
92  'appInitParams' => $params,
93  ]);
94  $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
95 
96  try {
97  $this->publisherConsumerController->initialize();
99  $this->markTestSkipped($e->getMessage());
100  } catch (PreconditionFailedException $e) {
101  $this->fail(
102  $e->getMessage()
103  );
104  }
105 
106  parent::setUp();
107  }
108 
113  {
114  $this->_markTestAsRestOnly();
115  $this->skus = [];
116  foreach ($products as $product) {
117  $this->skus[] = $product['product'][ProductInterface::SKU];
118  }
119  $this->clearProducts();
120 
121  $response = $this->saveProductAsync($products);
122  $this->assertArrayHasKey(self::BULK_UUID_KEY, $response);
123  $this->assertNotNull($response[self::BULK_UUID_KEY]);
124 
125  $this->assertCount(2, $response['request_items']);
126  foreach ($products as $key => $product) {
127  $this->assertEquals('accepted', $response['request_items'][$key]['status']);
128  }
129  $this->assertFalse($response['errors']);
130 
131  //assert one products is created
132  try {
133  $this->publisherConsumerController->waitForAsynchronousResult(
134  [$this, 'assertProductCreation'],
135  [$products]
136  );
137  } catch (PreconditionFailedException $e) {
138  $this->fail("Not all products were created");
139  }
140  }
141 
146  {
147  $this->_markTestAsRestOnly();
148  $this->skus = [];
149  $this->skus[] = $products[0]['product'][ProductInterface::SKU];
150  $this->clearProducts();
151 
152  $response = $this->saveProductAsync($products);
153  $this->assertArrayHasKey(self::BULK_UUID_KEY, $response);
154  $this->assertNotNull($response[self::BULK_UUID_KEY]);
155 
156  $this->assertCount(1, $response['request_items']);
157  $this->assertEquals('accepted', $response['request_items'][0]['status']);
158  $this->assertFalse($response['errors']);
159 
160  //assert one products is created
161  try {
162  $this->publisherConsumerController->waitForAsynchronousResult(
163  [$this, 'assertProductCreation'],
164  [$products]
165  );
166  } catch (PreconditionFailedException $e) {
167  $this->fail("Not all products were created");
168  }
169  }
170 
175  {
176  $this->_markTestAsRestOnly();
177  $this->skus = [];
178  foreach ($products as $product) {
179  $this->skus[] = $product['product'][ProductInterface::SKU];
180  }
181  $this->clearProducts();
182 
183  $response = null;
184  try {
185  $response = $this->saveProductAsync($products);
186  } catch (\Exception $e) {
187  $this->assertEquals(500, $e->getCode());
188  }
189  $this->assertNull($response);
190  $this->assertEquals(0, $this->checkProductsCreation());
191  }
192 
200  public function testGETRequestToAsyncBulk($sku, $storeCode = null)
201  {
202  $this->_markTestAsRestOnly();
203  $serviceInfo = [
204  'rest' => [
205  'resourcePath' => self::ASYNC_BULK_RESOURCE_PATH . '/' . $sku,
207  ],
208  ];
209 
210  $response = null;
211  try {
212  $response = $this->_webApiCall($serviceInfo, [ProductInterface::SKU => $sku], null, $storeCode);
213  } catch (NotFoundException $e) {
214  $this->assertEquals(400, $e->getCode());
215  }
216  $this->assertNull($response);
217  }
218 
219  public function tearDown()
220  {
221  $this->clearProducts();
222  $this->publisherConsumerController->stopConsumers();
223  parent::tearDown();
224  }
225 
226  private function clearProducts()
227  {
228  $size = $this->objectManager->create(Collection::class)
229  ->addAttributeToFilter('sku', ['in' => $this->skus])
230  ->load()
231  ->getSize();
232 
233  if ($size == 0) {
234  return;
235  }
236 
237  $this->registry->unregister('isSecureArea');
238  $this->registry->register('isSecureArea', true);
239  try {
240  foreach ($this->skus as $sku) {
241  $this->productRepository->deleteById($sku);
242  }
243  } catch (\Exception $e) {
244  throw $e;
245  //nothing to delete
246  }
247  $this->registry->unregister('isSecureArea');
248 
249  $size = $this->objectManager->create(Collection::class)
250  ->addAttributeToFilter('sku', ['in' => $this->skus])
251  ->load()
252  ->getSize();
253 
254  if ($size > 0) {
255  throw new Exception(new Phrase("Collection size after clearing the products: %size", ['size' => $size]));
256  }
257  }
258 
263  {
264  $productBuilder = function ($data) {
265  return array_replace_recursive(
266  $this->getSimpleProductData(),
267  $data
268  );
269  };
270 
271  return
272  [
273  [
274  [
275  [
276  'product' =>
277  $productBuilder([
278  ProductInterface::TYPE_ID => 'simple',
279  ProductInterface::SKU => 'psku-test-1-multiple',
280  ]),
281  ],
282  [
283  'product' => $productBuilder([
284  ProductInterface::TYPE_ID => 'virtual',
285  ProductInterface::SKU => 'psku-test-2-multiple',
286  ]),
287  ],
288  ],
289  ],
290  ];
291  }
292 
297  {
298  $productBuilder = function ($data) {
299  return array_replace_recursive(
300  $this->getSimpleProductData(),
301  $data
302  );
303  };
304 
305  return
306  [
307  [
308  [
309  [
310  'product' =>
311  $productBuilder([
312  ProductInterface::TYPE_ID => 'simple',
313  ProductInterface::SKU => 'psku-test-1-single',
314  ]),
315  ],
316  ],
317  ],
318  ];
319  }
320 
325  {
326  $productBuilder = function ($data) {
327  return array_replace_recursive(
328  $this->getSimpleProductData(),
329  $data
330  );
331  };
332 
333  $wrongProductBuilder = function ($data) {
334  return array_replace_recursive(
335  $this->getWrongProductStructureData(),
336  $data
337  );
338  };
339 
340  return
341  [
342  [
343  [
344  [
345  'product' =>
346  $productBuilder([
347  ProductInterface::TYPE_ID => 'simple',
348  ProductInterface::SKU => 'psku-test-1-wrong',
349  ]),
350  ],
351  [
352  'product' => $productBuilder([
353  ProductInterface::TYPE_ID => 'virtual',
354  ProductInterface::SKU => 'psku-test-2-wrong',
355  ]),
356  ],
357  [
358  'product' =>
359  $wrongProductBuilder([
360  'wrong_attribute' => 'simple',
361  ProductInterface::SKU => 'psku-test-3-wrong',
362  ]),
363  ],
364  ],
365  ],
366  ];
367  }
368 
372  public function productGetDataProvider()
373  {
374  return [
375  ['psku-test-1', null],
376  ];
377  }
378 
385  private function getSimpleProductData($productData = [])
386  {
387  return [
389  ? $productData[ProductInterface::SKU] : uniqid('sku-', true),
391  ? $productData[ProductInterface::NAME] : uniqid('sku-', true),
393  ProductInterface::TYPE_ID => 'simple',
394  ProductInterface::PRICE => 3.62,
396  ProductInterface::TYPE_ID => 'simple',
398  'custom_attributes' => [
399  ['attribute_code' => 'cost', 'value' => ''],
400  ['attribute_code' => 'description', 'value' => 'Description'],
401  ],
402  ];
403  }
404 
411  private function getWrongProductStructureData($productData = [])
412  {
413  return [
415  ? $productData[ProductInterface::SKU] : uniqid('sku-', true),
416  ];
417  }
418 
424  private function saveProductAsync($requestData, $storeCode = null)
425  {
426  $serviceInfo = [
427  'rest' => [
428  'resourcePath' => self::ASYNC_BULK_RESOURCE_PATH,
430  ],
431  ];
432 
433  return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode);
434  }
435 
436  public function assertProductCreation()
437  {
438  $collection = $this->objectManager->create(Collection::class)
439  ->addAttributeToFilter('sku', ['in' => $this->skus])
440  ->load();
441  $size = $collection->getSize();
442 
443  return $size == count($this->skus);
444  }
445 
449  public function checkProductsCreation()
450  {
451  $collection = $this->objectManager->create(Collection::class)
452  ->addAttributeToFilter('sku', ['in' => $this->skus])
453  ->load();
454  $size = $collection->getSize();
455 
456  return $size;
457  }
458 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$storeCode
Definition: indexer.php:15
$productData
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18