BulkManagementTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Test\Unit\Model;
  7. /**
  8. * Unit test for BulkManagement model.
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class BulkManagementTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\EntityManager\EntityManager|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $entityManager;
  18. /**
  19. * @var \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory
  20. * |\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $bulkSummaryFactory;
  23. /**
  24. * @var \Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory
  25. * |\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $operationCollectionFactory;
  28. /**
  29. * @var \Magento\Framework\MessageQueue\BulkPublisherInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $publisher;
  32. /**
  33. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $metadataPool;
  36. /**
  37. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $resourceConnection;
  40. /**
  41. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $logger;
  44. /**
  45. * @var \Magento\AsynchronousOperations\Model\BulkManagement
  46. */
  47. private $bulkManagement;
  48. /**
  49. * Set up.
  50. *
  51. * @return void
  52. */
  53. protected function setUp()
  54. {
  55. $this->entityManager = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityManager::class)
  56. ->disableOriginalConstructor()->getMock();
  57. $this->bulkSummaryFactory = $this
  58. ->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory::class)
  59. ->setMethods(['create'])
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->operationCollectionFactory = $this
  63. ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory::class)
  64. ->setMethods(['create'])
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->publisher = $this->getMockBuilder(\Magento\Framework\MessageQueue\BulkPublisherInterface::class)
  68. ->disableOriginalConstructor()->getMock();
  69. $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  70. ->disableOriginalConstructor()->getMock();
  71. $this->resourceConnection = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  74. ->disableOriginalConstructor()->getMock();
  75. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  76. $this->bulkManagement = $objectManager->getObject(
  77. \Magento\AsynchronousOperations\Model\BulkManagement::class,
  78. [
  79. 'entityManager' => $this->entityManager,
  80. 'bulkSummaryFactory' => $this->bulkSummaryFactory,
  81. 'operationCollectionFactory' => $this->operationCollectionFactory,
  82. 'publisher' => $this->publisher,
  83. 'metadataPool' => $this->metadataPool,
  84. 'resourceConnection' => $this->resourceConnection,
  85. 'logger' => $this->logger,
  86. ]
  87. );
  88. }
  89. /**
  90. * Test for scheduleBulk method.
  91. *
  92. * @return void
  93. */
  94. public function testScheduleBulk()
  95. {
  96. $bulkUuid = 'bulk-001';
  97. $description = 'Bulk summary description...';
  98. $userId = 1;
  99. $userType = \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN;
  100. $connectionName = 'default';
  101. $topicNames = ['topic.name.0', 'topic.name.1'];
  102. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  103. ->disableOriginalConstructor()->getMock();
  104. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  105. ->disableOriginalConstructor()->getMock();
  106. $this->metadataPool->expects($this->once())->method('getMetadata')
  107. ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  108. ->willReturn($metadata);
  109. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  110. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  111. ->disableOriginalConstructor()->getMock();
  112. $this->resourceConnection->expects($this->once())
  113. ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
  114. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  115. $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  116. ->disableOriginalConstructor()->getMock();
  117. $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
  118. $this->entityManager->expects($this->once())
  119. ->method('load')->with($bulkSummary, $bulkUuid)->willReturn($bulkSummary);
  120. $bulkSummary->expects($this->once())->method('setBulkId')->with($bulkUuid)->willReturnSelf();
  121. $bulkSummary->expects($this->once())->method('setDescription')->with($description)->willReturnSelf();
  122. $bulkSummary->expects($this->once())->method('setUserId')->with($userId)->willReturnSelf();
  123. $bulkSummary->expects($this->once())->method('setUserType')->with($userType)->willReturnSelf();
  124. $bulkSummary->expects($this->once())->method('getOperationCount')->willReturn(1);
  125. $bulkSummary->expects($this->once())->method('setOperationCount')->with(3)->willReturnSelf();
  126. $this->entityManager->expects($this->once())->method('save')->with($bulkSummary)->willReturn($bulkSummary);
  127. $connection->expects($this->once())->method('commit')->willReturnSelf();
  128. $operation->expects($this->exactly(2))->method('getTopicName')
  129. ->willReturnOnConsecutiveCalls($topicNames[0], $topicNames[1]);
  130. $this->publisher->expects($this->exactly(2))->method('publish')
  131. ->withConsecutive([$topicNames[0], [$operation]], [$topicNames[1], [$operation]])->willReturn(null);
  132. $this->assertTrue(
  133. $this->bulkManagement->scheduleBulk($bulkUuid, [$operation, $operation], $description, $userId)
  134. );
  135. }
  136. /**
  137. * Test for scheduleBulk method with exception.
  138. *
  139. * @return void
  140. */
  141. public function testScheduleBulkWithException()
  142. {
  143. $bulkUuid = 'bulk-001';
  144. $description = 'Bulk summary description...';
  145. $userId = 1;
  146. $connectionName = 'default';
  147. $exceptionMessage = 'Exception message';
  148. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  149. ->disableOriginalConstructor()->getMock();
  150. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  151. ->disableOriginalConstructor()->getMock();
  152. $this->metadataPool->expects($this->once())->method('getMetadata')
  153. ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  154. ->willReturn($metadata);
  155. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  156. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  157. ->disableOriginalConstructor()->getMock();
  158. $this->resourceConnection->expects($this->once())
  159. ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
  160. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  161. $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  162. ->disableOriginalConstructor()->getMock();
  163. $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
  164. $this->entityManager->expects($this->once())->method('load')
  165. ->with($bulkSummary, $bulkUuid)->willThrowException(new \LogicException($exceptionMessage));
  166. $connection->expects($this->once())->method('rollBack')->willReturnSelf();
  167. $this->logger->expects($this->once())->method('critical')->with($exceptionMessage);
  168. $this->publisher->expects($this->never())->method('publish');
  169. $this->assertFalse($this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId));
  170. }
  171. /**
  172. * Test for retryBulk method.
  173. *
  174. * @return void
  175. */
  176. public function testRetryBulk()
  177. {
  178. $bulkUuid = 'bulk-001';
  179. $errorCodes = ['errorCode'];
  180. $connectionName = 'default';
  181. $operationId = 1;
  182. $operationTable = 'magento_operation';
  183. $topicName = 'topic.name';
  184. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  185. ->disableOriginalConstructor()->getMock();
  186. $this->metadataPool->expects($this->once())->method('getMetadata')
  187. ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  188. ->willReturn($metadata);
  189. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  190. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  191. ->disableOriginalConstructor()->getMock();
  192. $this->resourceConnection->expects($this->once())
  193. ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
  194. $operationCollection = $this
  195. ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class)
  196. ->disableOriginalConstructor()->getMock();
  197. $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
  198. $operationCollection->expects($this->exactly(2))->method('addFieldToFilter')
  199. ->withConsecutive(['error_code', ['in' => $errorCodes]], ['bulk_uuid', ['eq' => $bulkUuid]])
  200. ->willReturnSelf();
  201. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  202. ->disableOriginalConstructor()->getMock();
  203. $operationCollection->expects($this->once())->method('getItems')->willReturn([$operation]);
  204. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  205. $operation->expects($this->once())->method('getId')->willReturn($operationId);
  206. $operation->expects($this->once())->method('setId')->with(null)->willReturnSelf();
  207. $this->resourceConnection->expects($this->once())
  208. ->method('getTableName')->with($operationTable)->willReturn($operationTable);
  209. $connection->expects($this->once())
  210. ->method('quoteInto')->with('id IN (?)', [$operationId])->willReturn('id IN (' . $operationId .')');
  211. $connection->expects($this->once())
  212. ->method('delete')->with($operationTable, 'id IN (' . $operationId .')')->willReturn(1);
  213. $connection->expects($this->once())->method('commit')->willReturnSelf();
  214. $operation->expects($this->once())->method('getTopicName')->willReturn($topicName);
  215. $this->publisher->expects($this->once())->method('publish')->with($topicName, [$operation])->willReturn(null);
  216. $this->assertEquals(1, $this->bulkManagement->retryBulk($bulkUuid, $errorCodes));
  217. }
  218. /**
  219. * Test for retryBulk method with exception.
  220. *
  221. * @return void
  222. */
  223. public function testRetryBulkWithException()
  224. {
  225. $bulkUuid = 'bulk-001';
  226. $errorCodes = ['errorCode'];
  227. $connectionName = 'default';
  228. $operationId = 1;
  229. $operationTable = 'magento_operation';
  230. $exceptionMessage = 'Exception message';
  231. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  232. ->disableOriginalConstructor()->getMock();
  233. $this->metadataPool->expects($this->once())->method('getMetadata')
  234. ->with(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  235. ->willReturn($metadata);
  236. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  237. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  238. ->disableOriginalConstructor()->getMock();
  239. $this->resourceConnection->expects($this->once())
  240. ->method('getConnectionByName')->with($connectionName)->willReturn($connection);
  241. $operationCollection = $this
  242. ->getMockBuilder(\Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class)
  243. ->disableOriginalConstructor()->getMock();
  244. $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
  245. $operationCollection->expects($this->exactly(2))->method('addFieldToFilter')
  246. ->withConsecutive(['error_code', ['in' => $errorCodes]], ['bulk_uuid', ['eq' => $bulkUuid]])
  247. ->willReturnSelf();
  248. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  249. ->disableOriginalConstructor()->getMock();
  250. $operationCollection->expects($this->once())->method('getItems')->willReturn([$operation]);
  251. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  252. $operation->expects($this->once())->method('getId')->willReturn($operationId);
  253. $operation->expects($this->once())->method('setId')->with(null)->willReturnSelf();
  254. $this->resourceConnection->expects($this->once())
  255. ->method('getTableName')->with($operationTable)->willReturn($operationTable);
  256. $connection->expects($this->once())
  257. ->method('quoteInto')->with('id IN (?)', [$operationId])->willReturn('id IN (' . $operationId .')');
  258. $connection->expects($this->once())
  259. ->method('delete')->with($operationTable, 'id IN (' . $operationId .')')
  260. ->willThrowException(new \Exception($exceptionMessage));
  261. $connection->expects($this->once())->method('rollBack')->willReturnSelf();
  262. $this->logger->expects($this->once())->method('critical')->with($exceptionMessage);
  263. $this->publisher->expects($this->never())->method('publish');
  264. $this->assertEquals(0, $this->bulkManagement->retryBulk($bulkUuid, $errorCodes));
  265. }
  266. /**
  267. * Test for deleteBulk method.
  268. *
  269. * @return void
  270. */
  271. public function testDeleteBulk()
  272. {
  273. $bulkUuid = 'bulk-001';
  274. $bulkSummary = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class)
  275. ->disableOriginalConstructor()->getMock();
  276. $this->bulkSummaryFactory->expects($this->once())->method('create')->willReturn($bulkSummary);
  277. $this->entityManager->expects($this->once())
  278. ->method('load')->with($bulkSummary, $bulkUuid)->willReturn($bulkSummary);
  279. $this->entityManager->expects($this->once())->method('delete')->with($bulkSummary)->willReturn(true);
  280. $this->assertTrue($this->bulkManagement->deleteBulk($bulkUuid));
  281. }
  282. }