Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AsyncScheduleTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
23 
33 {
34  const SERVICE_NAME = 'catalogProductRepositoryV1';
35  const SERVICE_VERSION = 'V1';
36  const REST_RESOURCE_PATH = '/V1/products';
37  const ASYNC_RESOURCE_PATH = '/async/V1/products';
38  const ASYNC_CONSUMER_NAME = 'async.operations.all';
39 
40  const KEY_TIER_PRICES = 'tier_prices';
41  const KEY_SPECIAL_PRICE = 'special_price';
42  const KEY_CATEGORY_LINKS = 'category_links';
43 
44  const BULK_UUID_KEY = 'bulk_uuid';
45 
46  protected $consumers = [
48  ];
49 
53  private $skus = [];
54 
58  private $publisherConsumerController;
59 
63  private $productRepository;
64 
68  private $objectManager;
69 
73  private $registry;
74 
75  protected function setUp()
76  {
77  $this->objectManager = Bootstrap::getObjectManager();
78  $this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
79  $this->registry = $this->objectManager->get(Registry::class);
80 
81  $params = array_merge_recursive(
82  \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams(),
83  ['MAGE_DIRS' => ['cache' => ['path' => TESTS_TEMP_DIR . '/cache']]]
84  );
85 
87  $this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
88  'consumers' => $this->consumers,
89  'logFilePath' => $this->logFilePath,
90  'appInitParams' => $params,
91  ]);
92  $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
93 
94  try {
95  $this->publisherConsumerController->initialize();
97  $this->markTestSkipped($e->getMessage());
98  } catch (PreconditionFailedException $e) {
99  $this->fail(
100  $e->getMessage()
101  );
102  }
103 
104  parent::setUp();
105  }
106 
111  {
112  $this->_markTestAsRestOnly();
113  $this->skus[] = $product['product'][ProductInterface::SKU];
114  $this->clearProducts();
115 
116  $response = $this->saveProductAsync($product);
117  $this->assertArrayHasKey(self::BULK_UUID_KEY, $response);
118  $this->assertNotNull($response[self::BULK_UUID_KEY]);
119 
120  $this->assertCount(1, $response['request_items']);
121  $this->assertEquals('accepted', $response['request_items'][0]['status']);
122  $this->assertFalse($response['errors']);
123 
124  //assert one products is created
125  try {
126  $this->publisherConsumerController->waitForAsynchronousResult(
127  [$this, 'assertProductCreation'],
128  [$product]
129  );
130  } catch (PreconditionFailedException $e) {
131  $this->fail("Not all products were created");
132  }
133  }
134 
135  public function tearDown()
136  {
137  $this->clearProducts();
138  $this->publisherConsumerController->stopConsumers();
139  parent::tearDown();
140  }
141 
142  private function clearProducts()
143  {
144  $size = $this->objectManager->create(Collection::class)
145  ->addAttributeToFilter('sku', ['in' => $this->skus])
146  ->load()
147  ->getSize();
148 
149  if ($size == 0) {
150  return;
151  }
152 
153  $this->registry->unregister('isSecureArea');
154  $this->registry->register('isSecureArea', true);
155  try {
156  foreach ($this->skus as $sku) {
157  $this->productRepository->deleteById($sku);
158  }
159  } catch (\Exception $e) {
160  throw $e;
161  //nothing to delete
162  }
163  $this->registry->unregister('isSecureArea');
164 
165  $size = $this->objectManager->create(Collection::class)
166  ->addAttributeToFilter('sku', ['in' => $this->skus])
167  ->load()
168  ->getSize();
169 
170  if ($size > 0) {
171  throw new Exception(new Phrase("Collection size after clearing the products: %size", ['size' => $size]));
172  }
173  }
174 
182  public function testGETRequestToAsync($sku, $storeCode = null)
183  {
184  $this->_markTestAsRestOnly();
185  $serviceInfo = [
186  'rest' => [
187  'resourcePath' => self::ASYNC_RESOURCE_PATH . '/' . $sku,
189  ],
190  ];
191 
192  $response = null;
193  try {
194  $response = $this->_webApiCall($serviceInfo, [ProductInterface::SKU => $sku], null, $storeCode);
195  } catch (NotFoundException $e) {
196  $this->assertEquals(400, $e->getCode());
197  }
198  $this->assertNull($response);
199  }
200 
204  public function productCreationProvider()
205  {
206  $productBuilder = function ($data) {
207  return array_replace_recursive(
208  $this->getSimpleProductData(),
209  $data
210  );
211  };
212 
213  return [
214  [
215  [
216  'product' =>
217  $productBuilder([
218  ProductInterface::TYPE_ID => 'simple',
219  ProductInterface::SKU => 'psku-test-1',
220  ]),
221  ],
222  ],
223  [
224  [
225  'product' => $productBuilder([
226  ProductInterface::TYPE_ID => 'virtual',
227  ProductInterface::SKU => 'psku-test-2',
228  ]),
229  ],
230  ],
231  ];
232  }
233 
237  public function productGetDataProvider()
238  {
239  return [
240  ['psku-test-1', null],
241  ];
242  }
243 
250  private function getSimpleProductData($productData = [])
251  {
252  return [
254  ? $productData[ProductInterface::SKU] : uniqid('sku-', true),
256  ? $productData[ProductInterface::NAME] : uniqid('sku-', true),
258  ProductInterface::TYPE_ID => 'simple',
259  ProductInterface::PRICE => 3.62,
261  ProductInterface::TYPE_ID => 'simple',
263  'custom_attributes' => [
264  ['attribute_code' => 'cost', 'value' => ''],
265  ['attribute_code' => 'description', 'value' => 'Description'],
266  ],
267  ];
268  }
269 
275  private function saveProductAsync($requestData, $storeCode = null)
276  {
277  $serviceInfo = [
278  'rest' => [
279  'resourcePath' => self::ASYNC_RESOURCE_PATH,
281  ],
282  ];
283 
284  return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode);
285  }
286 
287  public function assertProductCreation()
288  {
289  $collection = $this->objectManager->create(Collection::class)
290  ->addAttributeToFilter('sku', ['in' => $this->skus])
291  ->load();
292  $size = $collection->getSize();
293 
294  return $size == count($this->skus);
295  }
296 }
$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