Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AsyncScheduleCustomRouteTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
23 
33 {
34  const ASYNC_RESOURCE_CUSTOM_PATH = '/asyncProducts';
35  const ASYNC_CONSUMER_NAME = 'async.operations.all';
36 
37  const KEY_TIER_PRICES = 'tier_prices';
38  const KEY_SPECIAL_PRICE = 'special_price';
39  const KEY_CATEGORY_LINKS = 'category_links';
40 
41  const BULK_UUID_KEY = 'bulk_uuid';
42 
43  protected $consumers = [
45  ];
46 
50  private $skus = [];
51 
55  private $publisherConsumerController;
56 
60  private $productRepository;
61 
65  private $objectManager;
66 
70  private $registry;
71 
72  protected function setUp()
73  {
74  $this->objectManager = Bootstrap::getObjectManager();
75  $this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
76  $this->registry = $this->objectManager->get(Registry::class);
77 
78  $params = array_merge_recursive(
79  \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams(),
80  ['MAGE_DIRS' => ['cache' => ['path' => TESTS_TEMP_DIR . '/cache']]]
81  );
82 
84  $this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
85  'consumers' => $this->consumers,
86  'logFilePath' => $this->logFilePath,
87  'appInitParams' => $params,
88  ]);
89  $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
90 
91  try {
92  $this->publisherConsumerController->initialize();
94  $this->markTestSkipped($e->getMessage());
95  } catch (PreconditionFailedException $e) {
96  $this->fail(
97  $e->getMessage()
98  );
99  }
100 
101  parent::setUp();
102  }
103 
108  {
109  $this->_markTestAsRestOnly();
110  $this->skus[] = $product['product'][ProductInterface::SKU];
111  $this->clearProducts();
112 
113  $response = $this->saveProductByCustomRoute($product);
114  $this->assertArrayHasKey(self::BULK_UUID_KEY, $response);
115  $this->assertNotNull($response[self::BULK_UUID_KEY]);
116 
117  $this->assertCount(1, $response['request_items']);
118  $this->assertEquals('accepted', $response['request_items'][0]['status']);
119  $this->assertFalse($response['errors']);
120 
121  //assert one products is created
122  try {
123  $this->publisherConsumerController->waitForAsynchronousResult(
124  [$this, 'assertProductCreation'],
125  [$product]
126  );
127  } catch (PreconditionFailedException $e) {
128  $this->fail("Not all products were created");
129  }
130  }
131 
132  public function tearDown()
133  {
134  $this->clearProducts();
135  $this->publisherConsumerController->stopConsumers();
136  parent::tearDown();
137  }
138 
139  private function clearProducts()
140  {
141  $size = $this->objectManager->create(Collection::class)
142  ->addAttributeToFilter('sku', ['in' => $this->skus])
143  ->load()
144  ->getSize();
145 
146  if ($size == 0) {
147  return;
148  }
149 
150  $this->registry->unregister('isSecureArea');
151  $this->registry->register('isSecureArea', true);
152  try {
153  foreach ($this->skus as $sku) {
154  $this->productRepository->deleteById($sku);
155  }
156  } catch (\Exception $e) {
157  throw $e;
158  //nothing to delete
159  }
160  $this->registry->unregister('isSecureArea');
161 
162  $size = $this->objectManager->create(Collection::class)
163  ->addAttributeToFilter('sku', ['in' => $this->skus])
164  ->load()
165  ->getSize();
166 
167  if ($size > 0) {
168  throw new Exception(new Phrase("Collection size after clearing the products: %size", ['size' => $size]));
169  }
170  }
171 
175  public function productCreationProvider()
176  {
177  $productBuilder = function ($data) {
178  return array_replace_recursive(
179  $this->getSimpleProductData(),
180  $data
181  );
182  };
183 
184  return [
185  [
186  [
187  'product' =>
188  $productBuilder([
189  ProductInterface::TYPE_ID => 'simple',
190  ProductInterface::SKU => 'psku-test-1',
191  ]),
192  ],
193  ],
194  [
195  [
196  'product' => $productBuilder([
197  ProductInterface::TYPE_ID => 'virtual',
198  ProductInterface::SKU => 'psku-test-2',
199  ]),
200  ],
201  ],
202  ];
203  }
204 
211  private function getSimpleProductData($productData = [])
212  {
213  return [
215  ? $productData[ProductInterface::SKU] : uniqid('sku-', true),
217  ? $productData[ProductInterface::NAME] : uniqid('sku-', true),
219  ProductInterface::TYPE_ID => 'simple',
220  ProductInterface::PRICE => 3.62,
222  ProductInterface::TYPE_ID => 'simple',
224  'custom_attributes' => [
225  ['attribute_code' => 'cost', 'value' => ''],
226  ['attribute_code' => 'description', 'value' => 'Description'],
227  ],
228  ];
229  }
230 
236  private function saveProductByCustomRoute($requestData, $storeCode = null)
237  {
238  $serviceInfo = [
239  'rest' => [
240  'resourcePath' => self::ASYNC_RESOURCE_CUSTOM_PATH,
242  ],
243  ];
244 
245  return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode);
246  }
247 
248  public function assertProductCreation()
249  {
250  $collection = $this->objectManager->create(Collection::class)
251  ->addAttributeToFilter('sku', ['in' => $this->skus])
252  ->load();
253  $size = $collection->getSize();
254 
255  return $size == count($this->skus);
256  }
257 }
$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