|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Model\Attribute\Backend; |
| 10 | + |
| 11 | +use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\AsynchronousOperations\Model\BulkManagement; |
| 16 | +use Magento\AsynchronousOperations\Model\BulkStatus; |
| 17 | +use Magento\AsynchronousOperations\Api\Data\OperationInterface; |
| 18 | +use Magento\Framework\MessageQueue\BulkPublisherInterface; |
| 19 | + |
| 20 | +class ConsumerTest extends TestCase |
| 21 | +{ |
| 22 | + const BULK_UUID = '5a12c1bd-a8b5-41d4-8c00-3f5bcaa6d3c8'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Magento\Catalog\Model\Attribute\Backend\Consumer |
| 26 | + */ |
| 27 | + private $model; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \PHPUnit\Framework\MockObject\MockObject |
| 31 | + */ |
| 32 | + private $publisherMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var BulkManagement |
| 36 | + */ |
| 37 | + private $bulkManagement; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var BulkStatus |
| 41 | + */ |
| 42 | + private $bulkStatus; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ObjectManagerInterface |
| 46 | + */ |
| 47 | + private $objectManager; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \Magento\Framework\Serialize\SerializerInterface |
| 51 | + */ |
| 52 | + private $serializer; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 60 | + $this->publisherMock = $this->getMockForAbstractClass(BulkPublisherInterface::class); |
| 61 | + |
| 62 | + $this->bulkManagement = $this->objectManager->create( |
| 63 | + BulkManagement::class, |
| 64 | + [ |
| 65 | + 'publisher' => $this->publisherMock |
| 66 | + ] |
| 67 | + ); |
| 68 | + $this->bulkStatus = $this->objectManager->get(BulkStatus::class); |
| 69 | + $catalogProductMock = $this->createMock(\Magento\Catalog\Helper\Product::class); |
| 70 | + $productFlatIndexerProcessorMock = $this->createMock( |
| 71 | + \Magento\Catalog\Model\Indexer\Product\Flat\Processor::class |
| 72 | + ); |
| 73 | + $productPriceIndexerProcessorMock = $this->createMock( |
| 74 | + \Magento\Catalog\Model\Indexer\Product\Price\Processor::class |
| 75 | + ); |
| 76 | + $operationManagementMock = $this->createMock( |
| 77 | + \Magento\Framework\Bulk\OperationManagementInterface::class |
| 78 | + ); |
| 79 | + $actionMock = $this->createMock(\Magento\Catalog\Model\Product\Action::class); |
| 80 | + $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class); |
| 81 | + $this->serializer = $this->objectManager->get(\Magento\Framework\Serialize\SerializerInterface::class); |
| 82 | + $entityManager = $this->objectManager->get(\Magento\Framework\EntityManager\EntityManager::class); |
| 83 | + $this->model = $this->objectManager->create( |
| 84 | + Consumer::class, |
| 85 | + [ |
| 86 | + 'catalogProduct' => $catalogProductMock, |
| 87 | + 'productFlatIndexerProcessor' => $productFlatIndexerProcessorMock, |
| 88 | + 'productPriceIndexerProcessor' => $productPriceIndexerProcessorMock, |
| 89 | + 'operationManagement' => $operationManagementMock, |
| 90 | + 'action' => $actionMock, |
| 91 | + 'logger' => $loggerMock, |
| 92 | + 'serializer' => $this->serializer, |
| 93 | + 'entityManager' => $entityManager |
| 94 | + ] |
| 95 | + ); |
| 96 | + |
| 97 | + parent::setUp(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Testing saving bulk operation during processing operation by attribute backend consumer |
| 102 | + */ |
| 103 | + public function testSaveOperationDuringProcess() |
| 104 | + { |
| 105 | + $operation = $this->prepareUpdateAttributesBulkAndOperation(); |
| 106 | + try { |
| 107 | + $this->model->process($operation); |
| 108 | + } catch (\Exception $e) { |
| 109 | + $this->fail(sprintf('Operation save process failed.: %s', $e->getMessage())); |
| 110 | + } |
| 111 | + $operationStatus = $operation->getStatus(); |
| 112 | + $this->assertEquals( |
| 113 | + 1, |
| 114 | + $this->bulkStatus->getOperationsCountByBulkIdAndStatus(self::BULK_UUID, $operationStatus) |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Schedules test bulk and returns operation |
| 120 | + * @return OperationInterface |
| 121 | + */ |
| 122 | + private function prepareUpdateAttributesBulkAndOperation(): OperationInterface |
| 123 | + { |
| 124 | + // general bulk information |
| 125 | + $bulkUuid = self::BULK_UUID; |
| 126 | + $bulkDescription = 'Update attributes for 2 selected products'; |
| 127 | + $topicName = 'product_action_attribute.update'; |
| 128 | + $userId = 1; |
| 129 | + /** @var OperationInterfaceFactory $operationFactory */ |
| 130 | + $operationFactory = $this->objectManager->get(OperationInterfaceFactory::class); |
| 131 | + $operation = $operationFactory->create(); |
| 132 | + $operation->setBulkUuid($bulkUuid) |
| 133 | + ->setTopicName($topicName) |
| 134 | + ->setSerializedData($this->serializer->serialize( |
| 135 | + ['product_ids' => [1,3], 'attributes' => [], 'store_id' => '0'] |
| 136 | + )); |
| 137 | + $this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $bulkDescription, $userId); |
| 138 | + return $operation; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Clear created bulk and operation |
| 143 | + */ |
| 144 | + protected function tearDown(): void |
| 145 | + { |
| 146 | + $this->bulkManagement->deleteBulk(self::BULK_UUID); |
| 147 | + parent::tearDown(); |
| 148 | + } |
| 149 | +} |
0 commit comments