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