BulkManagementTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Model;
  7. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\ObjectManagerInterface;
  10. use Magento\AsynchronousOperations\Api\Data\OperationInterface;
  11. use Magento\Framework\MessageQueue\BulkPublisherInterface;
  12. use Magento\Framework\EntityManager\MetadataPool;
  13. use Magento\Framework\App\ResourceConnection;
  14. use Magento\Framework\EntityManager\EntityManager;
  15. use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
  16. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
  17. use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class BulkManagementTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var \PHPUnit\Framework\MockObject_MockObject
  25. */
  26. private $publisherMock;
  27. /**
  28. * @var BulkManagement
  29. */
  30. private $model;
  31. /**
  32. * @var ObjectManagerInterface
  33. */
  34. private $objectManager;
  35. protected function setUp()
  36. {
  37. $this->objectManager = Bootstrap::getObjectManager();
  38. $this->publisherMock = $this->createMock(BulkPublisherInterface::class);
  39. $this->model = $this->objectManager->create(
  40. BulkManagement::class,
  41. [
  42. 'publisher' => $this->publisherMock
  43. ]
  44. );
  45. }
  46. public function testScheduleBulk()
  47. {
  48. // general bulk information
  49. $bulkUuid = '5a12c1bd-a8b5-41d4-8c00-3f5bcaa6d3c7';
  50. $bulkDescription = 'Bulk General Information';
  51. $topicName = 'example.topic.name';
  52. $userId = 1;
  53. // generate bulk operations that must be saved
  54. $operationCount = 100;
  55. $operations = [];
  56. $operationFactory = $this->objectManager->get(OperationInterfaceFactory::class);
  57. for ($index = 0; $index < $operationCount; $index++) {
  58. /** @var OperationInterface $operation */
  59. $operation = $operationFactory->create();
  60. $operation->setBulkUuid($bulkUuid);
  61. $operation->setTopicName($topicName);
  62. $operation->setSerializedData(json_encode(['entity_id' => $index]));
  63. $operations[] = $operation;
  64. }
  65. $this->publisherMock->expects($this->once())
  66. ->method('publish')
  67. ->with($topicName, $operations);
  68. // schedule bulk
  69. $this->assertTrue($this->model->scheduleBulk($bulkUuid, $operations, $bulkDescription, $userId));
  70. $storedData = $this->getStoredOperationData();
  71. // No operations should be saved to database during bulk creation
  72. $this->assertCount(0, $storedData);
  73. }
  74. /**
  75. * @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
  76. */
  77. public function testRetryBulk()
  78. {
  79. $bulkUuid = 'bulk-uuid-5';
  80. $topicName = 'topic-4';
  81. $errorCodes = [1111, 2222];
  82. $operations = $this->objectManager->get(CollectionFactory::class)
  83. ->create()
  84. ->addFieldToFilter('bulk_uuid', ['eq' => $bulkUuid])
  85. ->getItems();
  86. foreach ($operations as $operation) {
  87. $operation->setId(null);
  88. }
  89. $this->publisherMock->expects($this->once())
  90. ->method('publish')
  91. ->with($topicName, array_values($operations));
  92. $this->assertEquals(2, $this->model->retryBulk($bulkUuid, $errorCodes));
  93. $operations = $this->objectManager->get(CollectionFactory::class)
  94. ->create()
  95. ->addFieldToFilter('bulk_uuid', ['eq' => $bulkUuid])
  96. ->getItems();
  97. // Failed operations should be removed from database during bulk retry
  98. $this->assertCount(0, $operations);
  99. }
  100. /**
  101. * @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
  102. */
  103. public function testDeleteBulk()
  104. {
  105. $this->model->deleteBulk('bulk-uuid-1');
  106. /** @var EntityManager $entityManager */
  107. $entityManager = $this->objectManager->get(EntityManager::class);
  108. $bulkSummaryFactory = $this->objectManager->get(BulkSummaryInterfaceFactory::class);
  109. /** @var BulkSummaryInterface $bulkSummary */
  110. $bulkSummary = $entityManager->load($bulkSummaryFactory->create(), 'bulk-uuid-1');
  111. $this->assertNull($bulkSummary->getBulkId());
  112. }
  113. /**
  114. * Retrieve stored operation data
  115. *
  116. * @return array
  117. * @throws \Exception
  118. */
  119. private function getStoredOperationData()
  120. {
  121. /** @var MetadataPool $metadataPool */
  122. $metadataPool = $this->objectManager->get(MetadataPool::class);
  123. $operationMetadata = $metadataPool->getMetadata(OperationInterface::class);
  124. /** @var ResourceConnection $resourceConnection */
  125. $resourceConnection = $this->objectManager->get(ResourceConnection::class);
  126. $connection = $resourceConnection->getConnectionByName($operationMetadata->getEntityConnectionName());
  127. return $connection->fetchAll($connection->select()->from($operationMetadata->getEntityTable()));
  128. }
  129. }