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