Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassScheduleTest.php
Go to the documentation of this file.
1 <?php
13 
26 
30 class MassScheduleTest extends \PHPUnit\Framework\TestCase
31 {
35  protected $consumers = ['async.operations.all'];
36 
40  protected $objectManager;
41 
45  private $massSchedule;
46 
50  private $collection;
51 
55  private $productRepository;
56 
60  private $publisherConsumerController;
61 
65  private $skus = [];
66 
70  private $registry;
71 
72  protected function setUp()
73  {
74  $this->objectManager = Bootstrap::getObjectManager();
75  $this->registry = $this->objectManager->get(Registry::class);
76  $this->massSchedule = $this->objectManager->create(MassSchedule::class);
77  $this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
78  $this->collection = $this->objectManager->create(Collection::class);
79  $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
81  $this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
82  'consumers' => $this->consumers,
83  'logFilePath' => $this->logFilePath,
84  'appInitParams' => \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams()
85  ]);
86 
87  try {
88  $this->publisherConsumerController->initialize();
90  $this->markTestSkipped($e->getMessage());
91  } catch (PreconditionFailedException $e) {
92  $this->fail($e->getMessage());
93  }
94 
95  parent::setUp();
96  }
97 
102  public function testScheduleMass($products)
103  {
104  try {
105  $this->sendBulk($products);
106  } catch (BulkException $bulkException) {
107  $this->fail('Bulk was not accepted in full');
108  }
109 
110  //assert all products are created
111  try {
112  $this->publisherConsumerController->waitForAsynchronousResult(
113  [$this, 'assertProductExists'],
114  [$this->skus, count($this->skus)]
115  );
116  } catch (PreconditionFailedException $e) {
117  $this->fail("Not all products were created");
118  }
119  }
120 
121  public function sendBulk($products)
122  {
123  $this->skus = [];
124  foreach ($products as $data) {
125  if (isset($data['product'])) {
126  $this->skus[] = $data['product']->getSku();
127  }
128  }
129  $this->clearProducts();
130 
131  $result = $this->massSchedule->publishMass('async.V1.products.POST', $products);
132 
133  //assert bulk accepted with no errors
134  $this->assertFalse($result->isErrors());
135 
136  //assert number of products sent to queue
137  $this->assertCount(count($this->skus), $result->getRequestItems());
138  }
139 
140  public function tearDown()
141  {
142  $this->publisherConsumerController->stopConsumers();
143  $this->clearProducts();
144 
145  parent::tearDown();
146  }
147 
148  private function clearProducts()
149  {
150  $size = $this->objectManager->create(Collection::class)
151  ->addAttributeToFilter('sku', ['in' => $this->skus])
152  ->load()
153  ->getSize();
154 
155  if ($size == 0) {
156  return;
157  }
158 
159  $this->registry->unregister('isSecureArea');
160  $this->registry->register('isSecureArea', true);
161  try {
162  foreach ($this->skus as $sku) {
163  $this->productRepository->deleteById($sku);
164  }
165  } catch (\Exception $e) {
166  //nothing to delete
167  }
168  $this->registry->unregister('isSecureArea');
169 
170  $size = $this->objectManager->create(Collection::class)
171  ->addAttributeToFilter('sku', ['in' => $this->skus])
172  ->load()
173  ->getSize();
174 
175  if ($size > 0) {
176  throw new Exception(new Phrase("Collection size after clearing the products: %size", ['size' => $size]));
177  }
178  }
179 
180  public function assertProductExists($productsSkus, $count)
181  {
182  $collection = $this->objectManager->create(Collection::class)
183  ->addAttributeToFilter('sku', ['in' => $productsSkus])
184  ->load();
185  $size = $collection->getSize();
186  return $size == $count;
187  }
188 
193  public function testScheduleMassOneEntityFailure($products)
194  {
195  try {
196  $this->sendBulk($products);
197  } catch (BulkException $e) {
198  $this->assertCount(1, $e->getErrors());
199 
200  $errors = $e->getErrors();
201  $this->assertInstanceOf(\Magento\Framework\Exception\LocalizedException::class, $errors[0]);
202 
203  $this->assertEquals("Error processing 1 element of input data", $errors[0]->getMessage());
204 
205  $reasonException = $errors[0]->getPrevious();
206 
207  $expectedErrorMessage = "Data item corresponding to \"product\" " .
208  "must be specified in the message with topic " .
209  "\"async.V1.products.POST\".";
210  $this->assertEquals(
211  $expectedErrorMessage,
212  $reasonException->getMessage()
213  );
214 
216  $bulkStatus = $e->getData();
217  $this->assertTrue($bulkStatus->isErrors());
218 
220  $items = $bulkStatus->getRequestItems();
221  $this->assertCount(2, $items);
222 
223  $this->assertEquals(ItemStatus::STATUS_ACCEPTED, $items[0]->getStatus());
224  $this->assertEquals(0, $items[0]->getId());
225 
226  $this->assertEquals(ItemStatus::STATUS_REJECTED, $items[1]->getStatus());
227  $this->assertEquals(1, $items[1]->getId());
228  $this->assertEquals($expectedErrorMessage, $items[1]->getErrorMessage());
229  }
230 
231  //assert one products is created
232  try {
233  $this->publisherConsumerController->waitForAsynchronousResult(
234  [$this, 'assertProductExists'],
235  [$this->skus, count($this->skus)]
236  );
237  } catch (PreconditionFailedException $e) {
238  $this->fail("Not all products were created");
239  }
240  }
241 
242  private function getProduct()
243  {
246  ->create(ProductInterface::class);
247  $product
248  ->setTypeId('simple')
249  ->setAttributeSetId(4)
250  ->setWebsiteIds([1])
251  ->setName('Simple Product 1')
252  ->setSku('unique-simple-product1')
253  ->setPrice(10)
254  ->setMetaTitle('meta title')
255  ->setMetaKeyword('meta keyword')
256  ->setMetaDescription('meta description')
257  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
258  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
259  ->setStockData(['use_config_manage_stock' => 0]);
260  return $product;
261  }
262 
263  public function productDataProvider()
264  {
265  return [
266  'single_product' => [
267  [['product' => $this->getProduct()]],
268  ],
269  'multiple_products' => [
270  [
271  ['product' => $this->getProduct()
272  ->setName('Simple Product 3')
273  ->setSku('unique-simple-product3')
274  ->setMetaTitle('meta title 3')
275  ],
276  ['product' => $this->getProduct()
277  ->setName('Simple Product 2')
278  ->setSku('unique-simple-product2')
279  ->setMetaTitle('meta title 2')
280  ]
281  ]
282  ],
283  ];
284  }
285 
287  {
288  return [
289  'single_product' => [
290  [['product' => $this->getProduct()]],
291  ],
292  'multiple_products' => [
293  [
294  ['product' => $this->getProduct()],
295  ['customer' => $this->getProduct()]
296  ]
297  ],
298  ];
299  }
300 }
$count
Definition: recent.phtml:13
$errors
Definition: overview.phtml:9
$items