Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkManagementTest.php
Go to the documentation of this file.
1 <?php
8 
14 class BulkManagementTest extends \PHPUnit\Framework\TestCase
15 {
19  private $entityManager;
20 
25  private $bulkSummaryFactory;
26 
31  private $operationCollectionFactory;
32 
36  private $publisher;
37 
41  private $metadataPool;
42 
46  private $resourceConnection;
47 
51  private $logger;
52 
56  private $bulkManagement;
57 
63  protected function setUp()
64  {
65  $this->entityManager = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityManager::class)
66  ->disableOriginalConstructor()->getMock();
67  $this->bulkSummaryFactory = $this
68  ->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory::class)
69  ->setMethods(['create'])
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->operationCollectionFactory = $this
73  ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory::class)
74  ->setMethods(['create'])
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->publisher = $this->getMockBuilder(\Magento\Framework\MessageQueue\BulkPublisherInterface::class)
78  ->disableOriginalConstructor()->getMock();
79  $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
80  ->disableOriginalConstructor()->getMock();
81  $this->resourceConnection = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
82  ->disableOriginalConstructor()->getMock();
83  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
84  ->disableOriginalConstructor()->getMock();
85 
86  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
87  $this->bulkManagement = $objectManager->getObject(
88  \Magento\AsynchronousOperations\Model\BulkManagement::class,
89  [
90  'entityManager' => $this->entityManager,
91  'bulkSummaryFactory' => $this->bulkSummaryFactory,
92  'operationCollectionFactory' => $this->operationCollectionFactory,
93  'publisher' => $this->publisher,
94  'metadataPool' => $this->metadataPool,
95  'resourceConnection' => $this->resourceConnection,
96  'logger' => $this->logger,
97  ]
98  );
99  }
100 
106  public function testScheduleBulk()
107  {
108  $bulkUuid = 'bulk-001';
109  $description = 'Bulk summary description...';
110  $userId = 1;
112  $connectionName = 'default';
113  $topicNames = ['topic.name.0', 'topic.name.1'];
114  $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
115  ->disableOriginalConstructor()->getMock();
116  $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
117  ->disableOriginalConstructor()->getMock();
118  $this->metadataPool->expects($this->once())->method('getMetadata')
119  ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
120  ->willReturn($metadata);
121  $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
122  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
123  ->disableOriginalConstructor()->getMock();
124  $this->resourceConnection->expects($this->once())
125  ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
126  $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
127  $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
128  ->disableOriginalConstructor()->getMock();
129  $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
130  $this->entityManager->expects($this->once())
131  ->method('load')->with($bulkSummary, $bulkUuid)->willReturn($bulkSummary);
132  $bulkSummary->expects($this->once())->method('setBulkId')->with($bulkUuid)->willReturnSelf();
133  $bulkSummary->expects($this->once())->method('setDescription')->with($description)->willReturnSelf();
134  $bulkSummary->expects($this->once())->method('setUserId')->with($userId)->willReturnSelf();
135  $bulkSummary->expects($this->once())->method('setUserType')->with($userType)->willReturnSelf();
136  $bulkSummary->expects($this->once())->method('getOperationCount')->willReturn(1);
137  $bulkSummary->expects($this->once())->method('setOperationCount')->with(3)->willReturnSelf();
138  $this->entityManager->expects($this->once())->method('save')->with($bulkSummary)->willReturn($bulkSummary);
139  $connection->expects($this->once())->method('commit')->willReturnSelf();
140  $operation->expects($this->exactly(2))->method('getTopicName')
141  ->willReturnOnConsecutiveCalls($topicNames[0], $topicNames[1]);
142  $this->publisher->expects($this->exactly(2))->method('publish')
143  ->withConsecutive([$topicNames[0], [$operation]], [$topicNames[1], [$operation]])->willReturn(null);
144  $this->assertTrue(
145  $this->bulkManagement->scheduleBulk($bulkUuid, [$operation, $operation], $description, $userId)
146  );
147  }
148 
155  {
156  $bulkUuid = 'bulk-001';
157  $description = 'Bulk summary description...';
158  $userId = 1;
159  $connectionName = 'default';
160  $exceptionMessage = 'Exception message';
161  $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
162  ->disableOriginalConstructor()->getMock();
163  $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
164  ->disableOriginalConstructor()->getMock();
165  $this->metadataPool->expects($this->once())->method('getMetadata')
166  ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
167  ->willReturn($metadata);
168  $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
169  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
170  ->disableOriginalConstructor()->getMock();
171  $this->resourceConnection->expects($this->once())
172  ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
173  $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
174  $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
175  ->disableOriginalConstructor()->getMock();
176  $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
177  $this->entityManager->expects($this->once())->method('load')
178  ->with($bulkSummary, $bulkUuid)->willThrowException(new \LogicException($exceptionMessage));
179  $connection->expects($this->once())->method('rollBack')->willReturnSelf();
180  $this->logger->expects($this->once())->method('critical')->with($exceptionMessage);
181  $this->publisher->expects($this->never())->method('publish');
182  $this->assertFalse($this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId));
183  }
184 
190  public function testRetryBulk()
191  {
192  $bulkUuid = 'bulk-001';
193  $errorCodes = ['errorCode'];
194  $connectionName = 'default';
195  $operationId = 1;
196  $operationTable = 'magento_operation';
197  $topicName = 'topic.name';
198  $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
199  ->disableOriginalConstructor()->getMock();
200  $this->metadataPool->expects($this->once())->method('getMetadata')
201  ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
202  ->willReturn($metadata);
203  $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
204  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
205  ->disableOriginalConstructor()->getMock();
206  $this->resourceConnection->expects($this->once())
207  ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
208  $operationCollection = $this
209  ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class)
210  ->disableOriginalConstructor()->getMock();
211  $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
212  $operationCollection->expects($this->exactly(2))->method('addFieldToFilter')
213  ->withConsecutive(['error_code', ['in' => $errorCodes]], ['bulk_uuid', ['eq' => $bulkUuid]])
214  ->willReturnSelf();
215  $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
216  ->disableOriginalConstructor()->getMock();
217  $operationCollection->expects($this->once())->method('getItems')->willReturn([$operation]);
218  $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
219  $operation->expects($this->once())->method('getId')->willReturn($operationId);
220  $operation->expects($this->once())->method('setId')->with(null)->willReturnSelf();
221  $this->resourceConnection->expects($this->once())
222  ->method('getTableName')->with($operationTable)->willReturn($operationTable);
223  $connection->expects($this->once())
224  ->method('quoteInto')->with('id IN (?)', [$operationId])->willReturn('id IN (' . $operationId .')');
225  $connection->expects($this->once())
226  ->method('delete')->with($operationTable, 'id IN (' . $operationId .')')->willReturn(1);
227  $connection->expects($this->once())->method('commit')->willReturnSelf();
228  $operation->expects($this->once())->method('getTopicName')->willReturn($topicName);
229  $this->publisher->expects($this->once())->method('publish')->with($topicName, [$operation])->willReturn(null);
230  $this->assertEquals(1, $this->bulkManagement->retryBulk($bulkUuid, $errorCodes));
231  }
232 
238  public function testRetryBulkWithException()
239  {
240  $bulkUuid = 'bulk-001';
241  $errorCodes = ['errorCode'];
242  $connectionName = 'default';
243  $operationId = 1;
244  $operationTable = 'magento_operation';
245  $exceptionMessage = 'Exception message';
246  $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
247  ->disableOriginalConstructor()->getMock();
248  $this->metadataPool->expects($this->once())->method('getMetadata')
249  ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
250  ->willReturn($metadata);
251  $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
252  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
253  ->disableOriginalConstructor()->getMock();
254  $this->resourceConnection->expects($this->once())
255  ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
256  $operationCollection = $this
257  ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class)
258  ->disableOriginalConstructor()->getMock();
259  $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
260  $operationCollection->expects($this->exactly(2))->method('addFieldToFilter')
261  ->withConsecutive(['error_code', ['in' => $errorCodes]], ['bulk_uuid', ['eq' => $bulkUuid]])
262  ->willReturnSelf();
263  $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
264  ->disableOriginalConstructor()->getMock();
265  $operationCollection->expects($this->once())->method('getItems')->willReturn([$operation]);
266  $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
267  $operation->expects($this->once())->method('getId')->willReturn($operationId);
268  $operation->expects($this->once())->method('setId')->with(null)->willReturnSelf();
269  $this->resourceConnection->expects($this->once())
270  ->method('getTableName')->with($operationTable)->willReturn($operationTable);
271  $connection->expects($this->once())
272  ->method('quoteInto')->with('id IN (?)', [$operationId])->willReturn('id IN (' . $operationId .')');
273  $connection->expects($this->once())
274  ->method('delete')->with($operationTable, 'id IN (' . $operationId .')')
275  ->willThrowException(new \Exception($exceptionMessage));
276  $connection->expects($this->once())->method('rollBack')->willReturnSelf();
277  $this->logger->expects($this->once())->method('critical')->with($exceptionMessage);
278  $this->publisher->expects($this->never())->method('publish');
279  $this->assertEquals(0, $this->bulkManagement->retryBulk($bulkUuid, $errorCodes));
280  }
281 
287  public function testDeleteBulk()
288  {
289  $bulkUuid = 'bulk-001';
290  $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
291  ->disableOriginalConstructor()->getMock();
292  $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
293  $this->entityManager->expects($this->once())
294  ->method('load')->with($bulkSummary, $bulkUuid)->willReturn($bulkSummary);
295  $this->entityManager->expects($this->once())->method('delete')->with($bulkSummary)->willReturn(true);
296  $this->assertTrue($this->bulkManagement->deleteBulk($bulkUuid));
297  }
298 }
$objectManager
Definition: bootstrap.php:17
$operationTable
Definition: bulk.php:15
$connection
Definition: bulk.php:13