|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogInventory\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\CatalogInventory\Api\Data\StockItemCollectionInterface; |
| 11 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 12 | +use Magento\CatalogInventory\Api\Data\StockStatusCollectionInterface; |
| 13 | +use Magento\CatalogInventory\Api\Data\StockStatusInterface; |
| 14 | +use Magento\CatalogInventory\Api\StockConfigurationInterface; |
| 15 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterface; |
| 16 | +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
| 17 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; |
| 18 | +use Magento\CatalogInventory\Api\StockStatusCriteriaInterface; |
| 19 | +use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory; |
| 20 | +use Magento\CatalogInventory\Api\StockStatusRepositoryInterface; |
| 21 | +use Magento\CatalogInventory\Model\StockRegistryPreloader; |
| 22 | +use Magento\CatalogInventory\Model\StockRegistryStorage; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +/** |
| 27 | + * Test for StockRegistryStorage |
| 28 | + * |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class StockRegistryPreloaderTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var StockItemRepositoryInterface|MockObject |
| 35 | + */ |
| 36 | + private $stockItemRepository; |
| 37 | + /** |
| 38 | + * @var StockStatusRepositoryInterface|MockObject |
| 39 | + */ |
| 40 | + private $stockStatusRepository; |
| 41 | + /** |
| 42 | + * @var MockObject |
| 43 | + */ |
| 44 | + private $stockItemCriteriaFactory; |
| 45 | + /** |
| 46 | + * @var MockObject |
| 47 | + */ |
| 48 | + private $stockStatusCriteriaFactory; |
| 49 | + /** |
| 50 | + * @var StockConfigurationInterface|MockObject |
| 51 | + */ |
| 52 | + private $stockConfiguration; |
| 53 | + /** |
| 54 | + * @var StockRegistryStorage |
| 55 | + */ |
| 56 | + private $stockRegistryStorage; |
| 57 | + /** |
| 58 | + * @var StockRegistryPreloader |
| 59 | + */ |
| 60 | + private $model; |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + */ |
| 65 | + protected function setUp(): void |
| 66 | + { |
| 67 | + parent::setUp(); |
| 68 | + $this->stockItemRepository = $this->createMock(StockItemRepositoryInterface::class); |
| 69 | + $this->stockStatusRepository = $this->createMock(StockStatusRepositoryInterface::class); |
| 70 | + $this->stockItemCriteriaFactory = $this->createMock(StockItemCriteriaInterfaceFactory::class); |
| 71 | + $this->stockStatusCriteriaFactory = $this->createMock(StockStatusCriteriaInterfaceFactory::class); |
| 72 | + $this->stockConfiguration = $this->createMock(StockConfigurationInterface::class); |
| 73 | + $this->stockRegistryStorage = new StockRegistryStorage(); |
| 74 | + $this->model = new StockRegistryPreloader( |
| 75 | + $this->stockItemRepository, |
| 76 | + $this->stockStatusRepository, |
| 77 | + $this->stockItemCriteriaFactory, |
| 78 | + $this->stockStatusCriteriaFactory, |
| 79 | + $this->stockConfiguration, |
| 80 | + $this->stockRegistryStorage, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function testPreloadStockItems(): void |
| 85 | + { |
| 86 | + $productIds = [10, 20]; |
| 87 | + $scopeId = 1; |
| 88 | + $stockItems = [ |
| 89 | + $this->createConfiguredMock(StockItemInterface::class, ['getProductId' => 10]), |
| 90 | + $this->createConfiguredMock(StockItemInterface::class, ['getProductId' => 20]), |
| 91 | + ]; |
| 92 | + $collection = $this->createConfiguredMock(StockItemCollectionInterface::class, ['getItems' => $stockItems]); |
| 93 | + $criteria = $this->createMock(StockItemCriteriaInterface::class); |
| 94 | + $criteria->expects($this->once()) |
| 95 | + ->method('setProductsFilter') |
| 96 | + ->with($productIds) |
| 97 | + ->willReturnSelf(); |
| 98 | + $criteria->expects($this->once()) |
| 99 | + ->method('setScopeFilter') |
| 100 | + ->with($scopeId) |
| 101 | + ->willReturnSelf(); |
| 102 | + $this->stockItemRepository->method('getList') |
| 103 | + ->willReturn($collection); |
| 104 | + $this->stockItemCriteriaFactory->method('create') |
| 105 | + ->willReturn($criteria); |
| 106 | + $this->assertEquals($stockItems, $this->model->preloadStockItems($productIds, $scopeId)); |
| 107 | + $this->assertSame($stockItems[0], $this->stockRegistryStorage->getStockItem(10, $scopeId)); |
| 108 | + $this->assertSame($stockItems[1], $this->stockRegistryStorage->getStockItem(20, $scopeId)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testPreloadStockStatuses(): void |
| 112 | + { |
| 113 | + $productIds = [10, 20]; |
| 114 | + $scopeId = 1; |
| 115 | + $stockItems = [ |
| 116 | + $this->createConfiguredMock(StockStatusInterface::class, ['getProductId' => 10]), |
| 117 | + $this->createConfiguredMock(StockStatusInterface::class, ['getProductId' => 20]), |
| 118 | + ]; |
| 119 | + $collection = $this->createConfiguredMock(StockStatusCollectionInterface::class, ['getItems' => $stockItems]); |
| 120 | + $criteria = $this->createMock(StockStatusCriteriaInterface::class); |
| 121 | + $criteria->expects($this->once()) |
| 122 | + ->method('setProductsFilter') |
| 123 | + ->with($productIds) |
| 124 | + ->willReturnSelf(); |
| 125 | + $criteria->expects($this->once()) |
| 126 | + ->method('setScopeFilter') |
| 127 | + ->with($scopeId) |
| 128 | + ->willReturnSelf(); |
| 129 | + $this->stockStatusRepository->method('getList') |
| 130 | + ->willReturn($collection); |
| 131 | + $this->stockStatusCriteriaFactory->method('create') |
| 132 | + ->willReturn($criteria); |
| 133 | + $this->assertEquals($stockItems, $this->model->preloadStockStatuses($productIds, $scopeId)); |
| 134 | + $this->assertSame($stockItems[0], $this->stockRegistryStorage->getStockStatus(10, $scopeId)); |
| 135 | + $this->assertSame($stockItems[1], $this->stockRegistryStorage->getStockStatus(20, $scopeId)); |
| 136 | + } |
| 137 | + |
| 138 | + public function testSetStockItems(): void |
| 139 | + { |
| 140 | + $scopeId = 1; |
| 141 | + $stockItems = [ |
| 142 | + $this->createConfiguredMock(StockItemInterface::class, ['getProductId' => 10]), |
| 143 | + $this->createConfiguredMock(StockItemInterface::class, ['getProductId' => 20]), |
| 144 | + ]; |
| 145 | + $this->model->setStockItems($stockItems, $scopeId); |
| 146 | + $this->assertSame($stockItems[0], $this->stockRegistryStorage->getStockItem(10, $scopeId)); |
| 147 | + $this->assertSame($stockItems[1], $this->stockRegistryStorage->getStockItem(20, $scopeId)); |
| 148 | + } |
| 149 | + |
| 150 | + public function testSetStockStatuses(): void |
| 151 | + { |
| 152 | + $scopeId = 1; |
| 153 | + $stockItems = [ |
| 154 | + $this->createConfiguredMock(StockStatusInterface::class, ['getProductId' => 10]), |
| 155 | + $this->createConfiguredMock(StockStatusInterface::class, ['getProductId' => 20]), |
| 156 | + ]; |
| 157 | + $this->model->setStockStatuses($stockItems, $scopeId); |
| 158 | + $this->assertSame($stockItems[0], $this->stockRegistryStorage->getStockStatus(10, $scopeId)); |
| 159 | + $this->assertSame($stockItems[1], $this->stockRegistryStorage->getStockStatus(20, $scopeId)); |
| 160 | + } |
| 161 | +} |
0 commit comments