Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkStatusTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class BulkStatusTest extends \PHPUnit\Framework\TestCase
16 {
20  private $model;
21 
25  private $bulkCollectionFactory;
26 
30  private $operationCollectionFactory;
31 
35  private $operationMock;
36 
40  private $bulkMock;
41 
45  private $resourceConnectionMock;
46 
50  private $calculatedStatusSqlMock;
51 
55  private $metadataPoolMock;
56 
60  private $bulkDetailedFactory;
61 
65  private $bulkShortFactory;
66 
70  private $entityMetadataMock;
71 
75  private $entityManager;
76 
80  private $connectionMock;
81 
82  protected function setUp()
83  {
84  $this->bulkCollectionFactory = $this->createPartialMock(
85  \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory::class,
86  ['create']
87  );
88  $this->operationCollectionFactory = $this->createPartialMock(
89  \Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory::class,
90  ['create']
91  );
92  $this->operationMock = $this->createMock(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class);
93  $this->bulkMock = $this->createMock(\Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class);
94  $this->resourceConnectionMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
95  $this->calculatedStatusSqlMock = $this->createMock(
96  \Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql::class
97  );
98  $this->metadataPoolMock = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
99  $this->bulkDetailedFactory = $this->createPartialMock(
100  \Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterfaceFactory ::class,
101  ['create']
102  );
103  $this->bulkShortFactory = $this->createPartialMock(
104  \Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterfaceFactory::class,
105  ['create']
106  );
107  $this->entityManager = $this->createMock(\Magento\Framework\EntityManager\EntityManager::class);
108 
109  $this->entityMetadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
110  $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
111 
112  $this->model = new \Magento\AsynchronousOperations\Model\BulkStatus(
113  $this->bulkCollectionFactory,
114  $this->operationCollectionFactory,
115  $this->resourceConnectionMock,
116  $this->calculatedStatusSqlMock,
117  $this->metadataPoolMock,
118  $this->bulkDetailedFactory,
119  $this->bulkShortFactory,
120  $this->entityManager
121  );
122  }
123 
129  public function testGetFailedOperationsByBulkId($failureType, $failureCodes)
130  {
131  $bulkUuid = 'bulk-1';
132  $operationCollection = $this->createMock(
133  \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
134  );
135  $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
136  $operationCollection
137  ->expects($this->at(0))
138  ->method('addFieldToFilter')
139  ->with('bulk_uuid', $bulkUuid)
140  ->willReturnSelf();
141  $operationCollection
142  ->expects($this->at(1))
143  ->method('addFieldToFilter')
144  ->with('status', $failureCodes)
145  ->willReturnSelf();
146  $operationCollection->expects($this->once())->method('getItems')->willReturn([$this->operationMock]);
147  $this->assertEquals([$this->operationMock], $this->model->getFailedOperationsByBulkId($bulkUuid, $failureType));
148  }
149 
151  {
152  $bulkUuid = 'bulk-1';
153  $status = 1354;
154  $size = 32;
155 
156  $operationCollection = $this->createMock(
157  \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
158  );
159  $this->operationCollectionFactory->expects($this->once())->method('create')->willReturn($operationCollection);
160  $operationCollection
161  ->expects($this->at(0))
162  ->method('addFieldToFilter')
163  ->with('bulk_uuid', $bulkUuid)
164  ->willReturnSelf();
165  $operationCollection
166  ->expects($this->at(1))
167  ->method('addFieldToFilter')
168  ->with('status', $status)
169  ->willReturnSelf();
170  $operationCollection
171  ->expects($this->once())
172  ->method('getSize')
173  ->willReturn($size);
174  $this->assertEquals($size, $this->model->getOperationsCountByBulkIdAndStatus($bulkUuid, $status));
175  }
176 
181  {
182  return [
183  [1, [1]],
184  [
185  null,
186  [
189  ],
190  ],
191  ];
192  }
193 
194  public function testGetBulksByUser()
195  {
196  $userId = 1;
197  $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
198  $bulkCollection = $this->createMock(\Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection::class);
199  $bulkCollection->expects($this->once())->method('getSelect')->willReturn($selectMock);
200  $selectMock->expects($this->once())->method('columns')->willReturnSelf();
201  $selectMock->expects($this->once())->method('order')->willReturnSelf();
202  $this->bulkCollectionFactory->expects($this->once())->method('create')->willReturn($bulkCollection);
203  $bulkCollection->expects($this->once())->method('addFieldToFilter')->with('user_id', $userId)->willReturnSelf();
204  $bulkCollection->expects($this->once())->method('getItems')->willReturn([$this->bulkMock]);
205  $this->assertEquals([$this->bulkMock], $this->model->getBulksByUser($userId));
206  }
207 
208  public function testGetBulksStatus()
209  {
210  $bulkUuid = 'bulk-1';
211  $allProcessedOperationCollection = $this->createMock(
212  \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
213  );
214 
215  $completeOperationCollection = $this->createMock(
216  \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection::class
217  );
218 
219  $connectionName = 'connection_name';
220  $entityType = \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface::class;
221  $this->metadataPoolMock
222  ->expects($this->once())
223  ->method('getMetadata')
224  ->with($entityType)
225  ->willReturn($this->entityMetadataMock);
226  $this->entityMetadataMock
227  ->expects($this->once())
228  ->method('getEntityConnectionName')
229  ->willReturn($connectionName);
230  $this->resourceConnectionMock
231  ->expects($this->once())
232  ->method('getConnectionByName')
233  ->with($connectionName)
234  ->willReturn($this->connectionMock);
235 
236  $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
237  $selectMock->expects($this->once())->method('from')->willReturnSelf();
238  $selectMock->expects($this->once())->method('where')->with('uuid = ?', $bulkUuid)->willReturnSelf();
239  $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
240  $this->connectionMock->expects($this->once())->method('fetchOne')->with($selectMock)->willReturn(10);
241 
242  $this->operationCollectionFactory
243  ->expects($this->at(0))
244  ->method('create')
245  ->willReturn($allProcessedOperationCollection);
246  $this->operationCollectionFactory
247  ->expects($this->at(1))
248  ->method('create')
249  ->willReturn($completeOperationCollection);
250  $allProcessedOperationCollection
251  ->expects($this->once())
252  ->method('addFieldToFilter')
253  ->with('bulk_uuid', $bulkUuid)
254  ->willReturnSelf();
255  $allProcessedOperationCollection->expects($this->once())->method('getSize')->willReturn(5);
256 
257  $completeOperationCollection
258  ->expects($this->at(0))
259  ->method('addFieldToFilter')
260  ->with('bulk_uuid', $bulkUuid)
261  ->willReturnSelf();
262  $completeOperationCollection
263  ->expects($this->at(1))
264  ->method('addFieldToFilter')
266  ->willReturnSelf();
267  $completeOperationCollection->expects($this->any())->method('getSize')->willReturn(5);
268  $this->assertEquals(BulkSummaryInterface::IN_PROGRESS, $this->model->getBulkStatus($bulkUuid));
269  }
270 }
$status
Definition: order_status.php:8