BulkStatusTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. use Magento\AsynchronousOperations\Api\Data\OperationInterface;
  8. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class BulkStatusTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\AsynchronousOperations\Model\BulkStatus
  16. */
  17. private $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $bulkCollectionFactory;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $operationCollectionFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $operationMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $bulkMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $resourceConnectionMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $calculatedStatusSqlMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $metadataPoolMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $bulkDetailedFactory;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $bulkShortFactory;
  54. /**
  55. * @var \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $entityMetadataMock;
  58. /**
  59. * @var \PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $entityManager;
  62. /**
  63. * @var \PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private $connectionMock;
  66. protected function setUp()
  67. {
  68. $this->bulkCollectionFactory = $this->createPartialMock(
  69. \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory::class,
  70. ['create']
  71. );
  72. $this->operationCollectionFactory = $this->createPartialMock(
  73. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory::class,
  74. ['create']
  75. );
  76. $this->operationMock = $this->createMock(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class);
  77. $this->bulkMock = $this->createMock(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class);
  78. $this->resourceConnectionMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  79. $this->calculatedStatusSqlMock = $this->createMock(
  80. \Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql::class
  81. );
  82. $this->metadataPoolMock = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
  83. $this->bulkDetailedFactory = $this->createPartialMock(
  84. \Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterfaceFactory ::class,
  85. ['create']
  86. );
  87. $this->bulkShortFactory = $this->createPartialMock(
  88. \Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterfaceFactory::class,
  89. ['create']
  90. );
  91. $this->entityManager = $this->createMock(\Magento\Framework\EntityManager\EntityManager::class);
  92. $this->entityMetadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
  93. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  94. $this->model = new \Magento\AsynchronousOperations\Model\BulkStatus(
  95. $this->bulkCollectionFactory,
  96. $this->operationCollectionFactory,
  97. $this->resourceConnectionMock,
  98. $this->calculatedStatusSqlMock,
  99. $this->metadataPoolMock,
  100. $this->bulkDetailedFactory,
  101. $this->bulkShortFactory,
  102. $this->entityManager
  103. );
  104. }
  105. /**
  106. * @param int|null $failureType
  107. * @param array $failureCodes
  108. * @dataProvider getFailedOperationsByBulkIdDataProvider
  109. */
  110. public function testGetFailedOperationsByBulkId($failureType, $failureCodes)
  111. {
  112. $bulkUuid = 'bulk-1';
  113. $operationCollection = $this->createMock(
  114. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
  115. );
  116. $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
  117. $operationCollection
  118. ->expects($this->at(0))
  119. ->method('addFieldToFilter')
  120. ->with('bulk_uuid', $bulkUuid)
  121. ->willReturnSelf();
  122. $operationCollection
  123. ->expects($this->at(1))
  124. ->method('addFieldToFilter')
  125. ->with('status', $failureCodes)
  126. ->willReturnSelf();
  127. $operationCollection->expects($this->once())->method('getItems')->willReturn([$this->operationMock]);
  128. $this->assertEquals([$this->operationMock], $this->model->getFailedOperationsByBulkId($bulkUuid, $failureType));
  129. }
  130. public function testGetOperationsCountByBulkIdAndStatus()
  131. {
  132. $bulkUuid = 'bulk-1';
  133. $status = 1354;
  134. $size = 32;
  135. $operationCollection = $this->createMock(
  136. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
  137. );
  138. $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
  139. $operationCollection
  140. ->expects($this->at(0))
  141. ->method('addFieldToFilter')
  142. ->with('bulk_uuid', $bulkUuid)
  143. ->willReturnSelf();
  144. $operationCollection
  145. ->expects($this->at(1))
  146. ->method('addFieldToFilter')
  147. ->with('status', $status)
  148. ->willReturnSelf();
  149. $operationCollection
  150. ->expects($this->once())
  151. ->method('getSize')
  152. ->willReturn($size);
  153. $this->assertEquals($size, $this->model->getOperationsCountByBulkIdAndStatus($bulkUuid, $status));
  154. }
  155. /**
  156. * @return array
  157. */
  158. public function getFailedOperationsByBulkIdDataProvider()
  159. {
  160. return [
  161. [1, [1]],
  162. [
  163. null,
  164. [
  165. OperationInterface::STATUS_TYPE_RETRIABLY_FAILED,
  166. OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED,
  167. ],
  168. ],
  169. ];
  170. }
  171. public function testGetBulksByUser()
  172. {
  173. $userId = 1;
  174. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  175. $bulkCollection = $this->createMock(\Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection::class);
  176. $bulkCollection->expects($this->once())->method('getSelect')->willReturn($selectMock);
  177. $selectMock->expects($this->once())->method('columns')->willReturnSelf();
  178. $selectMock->expects($this->once())->method('order')->willReturnSelf();
  179. $this->bulkCollectionFactory->expects($this->once())->method('create')->willReturn($bulkCollection);
  180. $bulkCollection->expects($this->once())->method('addFieldToFilter')->with('user_id', $userId)->willReturnSelf();
  181. $bulkCollection->expects($this->once())->method('getItems')->willReturn([$this->bulkMock]);
  182. $this->assertEquals([$this->bulkMock], $this->model->getBulksByUser($userId));
  183. }
  184. public function testGetBulksStatus()
  185. {
  186. $bulkUuid = 'bulk-1';
  187. $allProcessedOperationCollection = $this->createMock(
  188. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
  189. );
  190. $completeOperationCollection = $this->createMock(
  191. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
  192. );
  193. $connectionName = 'connection_name';
  194. $entityType = \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class;
  195. $this->metadataPoolMock
  196. ->expects($this->once())
  197. ->method('getMetadata')
  198. ->with($entityType)
  199. ->willReturn($this->entityMetadataMock);
  200. $this->entityMetadataMock
  201. ->expects($this->once())
  202. ->method('getEntityConnectionName')
  203. ->willReturn($connectionName);
  204. $this->resourceConnectionMock
  205. ->expects($this->once())
  206. ->method('getConnectionByName')
  207. ->with($connectionName)
  208. ->willReturn($this->connectionMock);
  209. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  210. $selectMock->expects($this->once())->method('from')->willReturnSelf();
  211. $selectMock->expects($this->once())->method('where')->with('uuid = ?', $bulkUuid)->willReturnSelf();
  212. $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
  213. $this->connectionMock->expects($this->once())->method('fetchOne')->with($selectMock)->willReturn(10);
  214. $this->operationCollectionFactory
  215. ->expects($this->at(0))
  216. ->method('create')
  217. ->willReturn($allProcessedOperationCollection);
  218. $this->operationCollectionFactory
  219. ->expects($this->at(1))
  220. ->method('create')
  221. ->willReturn($completeOperationCollection);
  222. $allProcessedOperationCollection
  223. ->expects($this->once())
  224. ->method('addFieldToFilter')
  225. ->with('bulk_uuid', $bulkUuid)
  226. ->willReturnSelf();
  227. $allProcessedOperationCollection->expects($this->once())->method('getSize')->willReturn(5);
  228. $completeOperationCollection
  229. ->expects($this->at(0))
  230. ->method('addFieldToFilter')
  231. ->with('bulk_uuid', $bulkUuid)
  232. ->willReturnSelf();
  233. $completeOperationCollection
  234. ->expects($this->at(1))
  235. ->method('addFieldToFilter')
  236. ->with('status', OperationInterface::STATUS_TYPE_COMPLETE)
  237. ->willReturnSelf();
  238. $completeOperationCollection->expects($this->any())->method('getSize')->willReturn(5);
  239. $this->assertEquals(BulkSummaryInterface::IN_PROGRESS, $this->model->getBulkStatus($bulkUuid));
  240. }
  241. }