|
| 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\CatalogInventory\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceProcessor; |
| 12 | +use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockProcessor; |
| 13 | +use Magento\CatalogInventory\Observer\ItemsForReindex; |
| 14 | +use Magento\CatalogInventory\Observer\ReindexQuoteInventoryObserver; |
| 15 | +use Magento\Framework\Event; |
| 16 | +use Magento\Framework\Event\Observer; |
| 17 | +use Magento\Framework\Exception\LocalizedException; |
| 18 | +use Magento\Quote\Model\Quote; |
| 19 | +use Magento\Quote\Model\Quote\Item; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test class for ReindexQuoteInventoryObserver |
| 25 | + */ |
| 26 | +class ReindexQuoteInventoryObserverTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var StockProcessor |
| 30 | + */ |
| 31 | + private StockProcessor $stockIndexerProcessor; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var PriceProcessor |
| 35 | + */ |
| 36 | + private PriceProcessor $priceIndexer; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ItemsForReindex |
| 40 | + */ |
| 41 | + private ItemsForReindex $itemsForReindex; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var LoggerInterface |
| 45 | + */ |
| 46 | + private LoggerInterface $logger; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Observer |
| 50 | + */ |
| 51 | + private Observer $observedObject; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Event |
| 55 | + */ |
| 56 | + private Event $event; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Quote |
| 60 | + */ |
| 61 | + private Quote $quote; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Item |
| 65 | + */ |
| 66 | + private Item $quoteItem; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var ReindexQuoteInventoryObserver |
| 70 | + */ |
| 71 | + private ReindexQuoteInventoryObserver $sut; |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritDoc |
| 75 | + */ |
| 76 | + public function setUp(): void |
| 77 | + { |
| 78 | + $this->stockIndexerProcessor = $this->createMock(StockProcessor::class); |
| 79 | + $this->priceIndexer = $this->createMock(PriceProcessor::class); |
| 80 | + $this->itemsForReindex = $this->createMock(ItemsForReindex::class); |
| 81 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 82 | + $this->observedObject = $this->createMock(Observer::class); |
| 83 | + $this->event = $this->createMock(Event::class); |
| 84 | + $this->quote = $this->createMock(Quote::class); |
| 85 | + $this->quoteItem = $this->createMock(Item::class); |
| 86 | + |
| 87 | + $this->sut = new ReindexQuoteInventoryObserver( |
| 88 | + $this->stockIndexerProcessor, |
| 89 | + $this->priceIndexer, |
| 90 | + $this->itemsForReindex, |
| 91 | + $this->logger |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test execute should re-index quote stock items. |
| 97 | + * |
| 98 | + * @test |
| 99 | + * |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + public function execute(): void |
| 103 | + { |
| 104 | + $this->observedObject->expects($this->once()) |
| 105 | + ->method('getEvent') |
| 106 | + ->willReturn($this->event); |
| 107 | + |
| 108 | + $this->event->expects($this->once()) |
| 109 | + ->method('getData') |
| 110 | + ->with('quote') |
| 111 | + ->willReturn($this->quote); |
| 112 | + |
| 113 | + $this->quote->expects($this->once()) |
| 114 | + ->method('getAllItems') |
| 115 | + ->willReturn([$this->quoteItem]); |
| 116 | + |
| 117 | + $this->quoteItem->expects($this->exactly(6)) |
| 118 | + ->method('getData') |
| 119 | + ->withConsecutive( |
| 120 | + ['product_id'], |
| 121 | + ['product_id'], |
| 122 | + ['children_items'], |
| 123 | + ['product_id'], |
| 124 | + ['product_id'], |
| 125 | + ['product_id'] |
| 126 | + )->willReturnOnConsecutiveCalls(1, 1, [$this->quoteItem], 1, 1, 1); |
| 127 | + |
| 128 | + $this->stockIndexerProcessor->expects($this->once()) |
| 129 | + ->method('reindexList') |
| 130 | + ->with([1 => 1]); |
| 131 | + |
| 132 | + $this->itemsForReindex->expects($this->once()) |
| 133 | + ->method('getItems') |
| 134 | + ->willReturn([$this->quoteItem]); |
| 135 | + |
| 136 | + $this->priceIndexer->expects($this->once()) |
| 137 | + ->method('reindexList') |
| 138 | + ->with([1]); |
| 139 | + |
| 140 | + $this->itemsForReindex->expects($this->once()) |
| 141 | + ->method('clear'); |
| 142 | + |
| 143 | + $this->sut->execute($this->observedObject); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test execute should log error on exception. |
| 148 | + * |
| 149 | + * @test |
| 150 | + * |
| 151 | + * @return void |
| 152 | + */ |
| 153 | + public function executeShouldLogOnException(): void |
| 154 | + { |
| 155 | + $this->observedObject->expects($this->once()) |
| 156 | + ->method('getEvent') |
| 157 | + ->willReturn($this->event); |
| 158 | + |
| 159 | + $this->event->expects($this->once()) |
| 160 | + ->method('getData') |
| 161 | + ->with('quote') |
| 162 | + ->willReturn($this->quote); |
| 163 | + |
| 164 | + $this->quote->expects($this->once()) |
| 165 | + ->method('getAllItems') |
| 166 | + ->willReturn([$this->quoteItem]); |
| 167 | + |
| 168 | + $this->quoteItem->expects($this->exactly(3)) |
| 169 | + ->method('getData') |
| 170 | + ->withConsecutive( |
| 171 | + ['product_id'], |
| 172 | + ['product_id'], |
| 173 | + ['children_items'] |
| 174 | + )->willReturnOnConsecutiveCalls(1, 1, []); |
| 175 | + |
| 176 | + $this->stockIndexerProcessor->expects($this->once()) |
| 177 | + ->method('reindexList') |
| 178 | + ->with([1 => 1]) |
| 179 | + ->willThrowException(new LocalizedException(__('error'))); |
| 180 | + |
| 181 | + $this->logger->expects($this->once()) |
| 182 | + ->method('error') |
| 183 | + ->with('Error while re-indexing order items: error'); |
| 184 | + |
| 185 | + $this->stockIndexerProcessor->expects($this->once()) |
| 186 | + ->method('markIndexerAsInvalid'); |
| 187 | + |
| 188 | + $this->priceIndexer->expects($this->once()) |
| 189 | + ->method('markIndexerAsInvalid'); |
| 190 | + |
| 191 | + $this->sut->execute($this->observedObject); |
| 192 | + } |
| 193 | +} |
0 commit comments