|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\InventoryCatalog\Test\Unit\Plugin\CatalogInventory\Model\Stock\StockItemRepository; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Indexer\Product\Full as FullProductIndexer; |
| 12 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 13 | +use Magento\CatalogInventory\Model\Stock\StockItemRepository; |
| 14 | +use Magento\Inventory\Model\SourceItem; |
| 15 | +use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku; |
| 16 | +use Magento\InventoryIndexer\Indexer\InventoryIndexer; |
| 17 | +use Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class StockItemRepositoryPluginTest extends TestCase |
| 22 | +{ |
| 23 | + /** @var FullProductIndexer|MockObject */ |
| 24 | + private $fullProductIndexer; |
| 25 | + |
| 26 | + /** @var InventoryIndexer|MockObject */ |
| 27 | + private $inventoryIndexer; |
| 28 | + |
| 29 | + /** @var ProductRepositoryInterface|MockObject */ |
| 30 | + private $productRepository; |
| 31 | + |
| 32 | + /** @var GetSourceItemsBySku|MockObject */ |
| 33 | + private $getSourceItemsBySku; |
| 34 | + |
| 35 | + /** @var StockItemRepositoryPlugin */ |
| 36 | + private $plugin; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->fullProductIndexer = $this->createMock(FullProductIndexer::class); |
| 41 | + $this->inventoryIndexer = $this->createMock(InventoryIndexer::class); |
| 42 | + $this->productRepository = $this->createMock(ProductRepositoryInterface::class); |
| 43 | + $this->getSourceItemsBySku = $this->createMock(GetSourceItemsBySku::class); |
| 44 | + |
| 45 | + $this->plugin = new StockItemRepositoryPlugin( |
| 46 | + $this->fullProductIndexer, |
| 47 | + $this->inventoryIndexer, |
| 48 | + $this->productRepository, |
| 49 | + $this->getSourceItemsBySku |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAfterSave(): void |
| 54 | + { |
| 55 | + $productId = 123; |
| 56 | + $sku = 'test-sku'; |
| 57 | + $sourceItemId = 456; |
| 58 | + |
| 59 | + $stockItem = $this->createMock(StockItemInterface::class); |
| 60 | + $stockItem->method('getProductId')->willReturn($productId); |
| 61 | + |
| 62 | + $product = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class); |
| 63 | + $product->method('getId')->willReturn($productId); |
| 64 | + $product->method('getSku')->willReturn($sku); |
| 65 | + $this->productRepository->method('getById')->with($productId)->willReturn($product); |
| 66 | + |
| 67 | + $sourceItem = $this->createMock(SourceItem::class); |
| 68 | + $sourceItem->method('getId')->willReturn($sourceItemId); |
| 69 | + $this->getSourceItemsBySku->method('execute')->with($sku)->willReturn([$sourceItem]); |
| 70 | + |
| 71 | + $this->fullProductIndexer->expects($this->once())->method('executeRow')->with($productId); |
| 72 | + $this->inventoryIndexer->expects($this->once())->method('executeList')->with([$sourceItemId]); |
| 73 | + |
| 74 | + $result = $this->plugin->afterSave( |
| 75 | + $this->createMock(StockItemRepository::class), |
| 76 | + $stockItem |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertSame($stockItem, $result); |
| 80 | + } |
| 81 | +} |
0 commit comments