bulk.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. use Magento\TestFramework\Helper\Bootstrap;
  7. use Magento\Framework\Bulk\OperationInterface;
  8. /**
  9. * @var $resource Magento\Framework\App\ResourceConnection
  10. */
  11. $resource = Bootstrap::getObjectManager()->get(\Magento\Framework\App\ResourceConnection::class);
  12. $connection = $resource->getConnection();
  13. $bulkTable = $resource->getTableName('magento_bulk');
  14. $operationTable = $resource->getTableName('magento_operation');
  15. $bulks = [
  16. 'not_started' => [
  17. 'uuid' => 'bulk-uuid-1',
  18. 'user_id' => 1,
  19. 'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
  20. 'description' => 'Bulk Description',
  21. 'operation_count' => 1,
  22. ],
  23. 'in_progress_success' => [
  24. 'uuid' => 'bulk-uuid-2',
  25. 'user_id' => 1,
  26. 'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
  27. 'description' => 'Bulk Description',
  28. 'operation_count' => 3,
  29. ],
  30. 'in_progress_failed' => [
  31. 'uuid' => 'bulk-uuid-3',
  32. 'user_id' => 1,
  33. 'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
  34. 'description' => 'Bulk Description',
  35. 'operation_count' => 2,
  36. ],
  37. 'finish_success' => [
  38. 'uuid' => 'bulk-uuid-4',
  39. 'user_id' => 1,
  40. 'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
  41. 'description' => 'Bulk Description',
  42. 'operation_count' => 1,
  43. ],
  44. 'finish_failed' => [
  45. 'uuid' => 'bulk-uuid-5',
  46. 'user_id' => 1,
  47. 'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
  48. 'description' => 'Bulk Description',
  49. 'operation_count' => 2,
  50. ],
  51. ];
  52. // Only processed operations are saved into database (i.e. operations that are not in 'open' state)
  53. $operations = [
  54. [
  55. 'bulk_uuid' => 'bulk-uuid-2',
  56. 'topic_name' => 'topic-3',
  57. 'serialized_data' => json_encode(['entity_id' => 2]),
  58. 'status' => OperationInterface::STATUS_TYPE_COMPLETE,
  59. 'error_code' => null,
  60. 'result_message' => null,
  61. ],
  62. [
  63. 'bulk_uuid' => 'bulk-uuid-3',
  64. 'topic_name' => 'topic-3',
  65. 'serialized_data' => json_encode(['entity_id' => 3]),
  66. 'status' => OperationInterface::STATUS_TYPE_RETRIABLY_FAILED,
  67. 'error_code' => 1111,
  68. 'result_message' => 'Something went wrong during your request',
  69. ],
  70. [
  71. 'bulk_uuid' => 'bulk-uuid-4',
  72. 'topic_name' => 'topic-4',
  73. 'serialized_data' => json_encode(['entity_id' => 4]),
  74. 'status' => OperationInterface::STATUS_TYPE_COMPLETE,
  75. 'error_code' => null,
  76. 'result_message' => null,
  77. ],
  78. [
  79. 'bulk_uuid' => 'bulk-uuid-5',
  80. 'topic_name' => 'topic-4',
  81. 'serialized_data' => json_encode(['entity_id' => 5, 'meta_information' => 'Test']),
  82. 'status' => OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED,
  83. 'error_code' => 1111,
  84. 'result_message' => 'Something went wrong during your request',
  85. ],
  86. [
  87. 'bulk_uuid' => 'bulk-uuid-5',
  88. 'topic_name' => 'topic-4',
  89. 'serialized_data' => json_encode(['entity_id' => 5]),
  90. 'status' => OperationInterface::STATUS_TYPE_RETRIABLY_FAILED,
  91. 'error_code' => 2222,
  92. 'result_message' => 'Entity with ID=4 does not exist',
  93. ],
  94. ];
  95. $bulkQuery = "INSERT INTO {$bulkTable} (`uuid`, `user_id`, `user_type`, `description`, `operation_count`)"
  96. . " VALUES (:uuid, :user_id, :user_type, :description, :operation_count);";
  97. foreach ($bulks as $bulk) {
  98. $connection->query($bulkQuery, $bulk);
  99. }
  100. $operationQuery = "INSERT INTO {$operationTable}"
  101. . " (`bulk_uuid`, `topic_name`, `serialized_data`, `status`, `error_code`, `result_message`)"
  102. . " VALUES (:bulk_uuid, :topic_name, :serialized_data, :status, :error_code, :result_message);";
  103. foreach ($operations as $operation) {
  104. $connection->query($operationQuery, $operation);
  105. }