Skip to content

Commit 63ce966

Browse files
committed
Change implementation for use DTO in API. And provide with integration test
1 parent 4aadde4 commit 63ce966

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

app/code/Magento/AsynchronousOperations/Model/BulkManagement.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function scheduleBulk($bulkUuid, array $operations, $description, $userId
117117
$bulkSummary->setUserId($userId);
118118
$bulkSummary->setUserType($userType);
119119
$bulkSummary->setOperationCount((int)$bulkSummary->getOperationCount() + count($operations));
120-
121120
$this->entityManager->save($bulkSummary);
122121

123122
$connection->commit();

app/code/Magento/AsynchronousOperations/Model/MassSchedule.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
140140

141141
$operations = [];
142142
$requestItems = [];
143-
$singleOperationsData = [];
144143
$bulkException = new BulkException();
145144
foreach ($entitiesArray as $key => $entityParams) {
146145
/** @var \Magento\AsynchronousOperations\Api\Data\ItemStatusInterface $requestItem */
@@ -149,7 +148,6 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
149148
try {
150149
$operation = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
151150
$operations[] = $operation;
152-
$singleOperationsData[] = $operation->getData();
153151
$requestItem->setId($key);
154152
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
155153
$requestItem->setDataHash(
@@ -170,7 +168,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
170168
}
171169
}
172170

173-
$this->saveMultipleOperations->execute($singleOperationsData);
171+
$this->saveMultipleOperations->execute($operations);
174172
if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
175173
throw new LocalizedException(
176174
__('Something went wrong while processing the request.')

app/code/Magento/AsynchronousOperations/Model/SaveMultipleOperations.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Magento\AsynchronousOperations\Model;
1010

11-
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
1211
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
1312
use Magento\AsynchronousOperations\Model\ResourceModel\Operation as OperationResource;
1413
use Magento\Framework\Exception\CouldNotSaveException;
@@ -41,10 +40,14 @@ public function __construct(
4140
public function execute(array $operations): void
4241
{
4342
try {
43+
$operationsToInsert = array_map(function ($operation) {
44+
return $operation->getData();
45+
}, $operations);
46+
4447
$connection = $this->operationResource->getConnection();
4548
$connection->insertMultiple(
4649
$this->operationResource->getTable(OperationResource::TABLE_NAME),
47-
$operations
50+
$operationsToInsert
4851
);
4952
} catch (\Exception $exception) {
5053
throw new CouldNotSaveException(__($exception->getMessage()));
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\AsynchronousOperations\Model;
8+
9+
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
10+
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
11+
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
12+
use Magento\AsynchronousOperations\Model\BulkStatus;
13+
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
14+
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
15+
use Magento\Framework\EntityManager\EntityManager;
16+
17+
class SaveMultipleOperationsTest extends \PHPUnit\Framework\TestCase
18+
{
19+
20+
private const BULK_UUID = "bulk-uuid-multiple-0";
21+
22+
/**
23+
* @var BulkStatus
24+
*/
25+
private $bulkStatusManagement;
26+
27+
/**
28+
* @var OperationInterfaceFactory
29+
*/
30+
private $operationFactory;
31+
32+
/**
33+
* @var SaveMultipleOperationsInterface
34+
*/
35+
private $saveMultipleOperationsInterface;
36+
37+
/**
38+
* @var EntityManager
39+
*/
40+
private $entityManager;
41+
42+
/**
43+
* @var BulkSummaryInterfaceFactory
44+
*/
45+
private $bulkSummaryFactory;
46+
47+
/**
48+
* Set Up the test
49+
*/
50+
protected function setUp()
51+
{
52+
$this->saveMultipleOperationsInterface = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
53+
SaveMultipleOperationsInterface::class
54+
);
55+
$this->operationFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
56+
OperationInterfaceFactory::class
57+
);
58+
$this->bulkStatusManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
59+
BulkStatus::class
60+
);
61+
$this->bulkSummaryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
62+
BulkSummaryInterfaceFactory::class
63+
);
64+
$this->entityManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
65+
EntityManager::class
66+
);
67+
}
68+
69+
/**
70+
* Test execute() of SaveMultipleOperations
71+
*/
72+
public function testExecute()
73+
{
74+
$operation = $this->createOperation();
75+
$operations = [$operation, $operation, $operation];
76+
77+
$bulkSummary = $this->bulkSummaryFactory->create();
78+
$this->entityManager->load($bulkSummary, self::BULK_UUID);
79+
$bulkSummary->setBulkId(self::BULK_UUID);
80+
$bulkSummary->setDescription("Test Bulk");
81+
$bulkSummary->setUserId(1);
82+
$bulkSummary->setUserType(1);
83+
$bulkSummary->setOperationCount(count($operations));
84+
$this->entityManager->save($bulkSummary);
85+
86+
$this->saveMultipleOperationsInterface->execute($operations);
87+
$operationsCount = $this->bulkStatusManagement->getOperationsCountByBulkIdAndStatus(self::BULK_UUID, OperationInterface::STATUS_TYPE_OPEN);
88+
$this->assertEquals($operationsCount, 3);
89+
}
90+
91+
/**
92+
* Create Operation object and pre-fill with test data
93+
* @return OperationInterface
94+
*/
95+
public function createOperation()
96+
{
97+
$serializedData = [
98+
'entity_id' => null,
99+
'entity_link' => '',
100+
'meta_information' => json_encode([
101+
'entity_id' => 5,
102+
'meta_information' => 'Test'
103+
])
104+
];
105+
106+
$data = [
107+
'data' => [
108+
OperationInterface::BULK_ID => self::BULK_UUID,
109+
OperationInterface::TOPIC_NAME => "topic-4",
110+
OperationInterface::SERIALIZED_DATA => json_encode($serializedData),
111+
OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN,
112+
],
113+
];
114+
return $this->operationFactory->create($data);
115+
}
116+
}

0 commit comments

Comments
 (0)