|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\InventoryCatalogAdminUi\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Framework\Api\SearchCriteria; |
| 11 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 12 | +use Magento\Framework\Api\SearchCriteriaBuilderFactory; |
| 13 | +use Magento\InventoryApi\Api\Data\SourceInterface; |
| 14 | +use Magento\InventoryApi\Api\Data\SourceItemInterface; |
| 15 | +use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface; |
| 16 | +use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface; |
| 17 | +use Magento\InventoryApi\Api\SourceItemRepositoryInterface; |
| 18 | +use Magento\InventoryApi\Api\SourceRepositoryInterface; |
| 19 | +use Magento\InventoryCatalogAdminUi\Model\GetQuantityInformationPerSourceBySkus; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class GetQuantityInformationPerSourceBySkusTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var SourceItemRepositoryInterface|MockObject |
| 27 | + */ |
| 28 | + private $sourceItemRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var SearchCriteriaBuilderFactory|MockObject |
| 32 | + */ |
| 33 | + private $searchCriteriaBuilderFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SourceRepositoryInterface|MockObject |
| 37 | + */ |
| 38 | + private $sourceRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var GetQuantityInformationPerSourceBySkus |
| 42 | + */ |
| 43 | + private $model; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var array[] |
| 47 | + */ |
| 48 | + private array $sources = [ |
| 49 | + 'source_code1' => [ |
| 50 | + 'getSourceCode' => 'source_code1', |
| 51 | + 'getName' => 'source_name1', |
| 52 | + ], |
| 53 | + 'source_code2' => [ |
| 54 | + 'getSourceCode' => 'source_code2', |
| 55 | + 'getName' => 'source_name2', |
| 56 | + ], |
| 57 | + ]; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var array[] |
| 61 | + */ |
| 62 | + private array $sourceItems = [ |
| 63 | + [ |
| 64 | + 'getSku' => 'sku1', |
| 65 | + 'getSourceCode' => 'source_code1', |
| 66 | + 'getQuantity' => 100.00, |
| 67 | + 'getStatus' => 1, |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'getSku' => 'sku2', |
| 71 | + 'getSourceCode' => 'source_code1', |
| 72 | + 'getQuantity' => 200.00, |
| 73 | + 'getStatus' => 1, |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'getSku' => 'sku2', |
| 77 | + 'getSourceCode' => 'source_code2', |
| 78 | + 'getQuantity' => 201.00, |
| 79 | + 'getStatus' => 1, |
| 80 | + ], |
| 81 | + [ |
| 82 | + 'getSku' => 'sku3', |
| 83 | + 'getSourceCode' => 'source_code2', |
| 84 | + 'getQuantity' => 300.00, |
| 85 | + 'getStatus' => 1, |
| 86 | + ], |
| 87 | + ]; |
| 88 | + |
| 89 | + protected function setUp(): void |
| 90 | + { |
| 91 | + parent::setUp(); |
| 92 | + $this->sourceItemRepository = $this->createMock(SourceItemRepositoryInterface::class); |
| 93 | + $this->searchCriteriaBuilderFactory = $this->createMock(SearchCriteriaBuilderFactory::class); |
| 94 | + $this->sourceRepository = $this->createMock(SourceRepositoryInterface::class); |
| 95 | + $this->model = new GetQuantityInformationPerSourceBySkus( |
| 96 | + $this->sourceItemRepository, |
| 97 | + $this->searchCriteriaBuilderFactory, |
| 98 | + $this->sourceRepository |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testExecute(): void |
| 103 | + { |
| 104 | + $skus = ['sku1', 'sku2', 'sku3']; |
| 105 | + $sourceItems[] = $this->createConfiguredMock(SourceItemInterface::class, $this->sourceItems[0]); |
| 106 | + $sourceItems[] = $this->createConfiguredMock(SourceItemInterface::class, $this->sourceItems[1]); |
| 107 | + $sourceItems[] = $this->createConfiguredMock(SourceItemInterface::class, $this->sourceItems[2]); |
| 108 | + $sourceItems[] = $this->createConfiguredMock(SourceItemInterface::class, $this->sourceItems[3]); |
| 109 | + $sources[] = $this->createConfiguredMock(SourceInterface::class, $this->sources['source_code1']); |
| 110 | + $sources[] = $this->createConfiguredMock(SourceInterface::class, $this->sources['source_code2']); |
| 111 | + $searchCriteria1 = $this->createMock(SearchCriteria::class); |
| 112 | + $searchCriteria2 = $this->createMock(SearchCriteria::class); |
| 113 | + $searchCriteriaBuilder1 = $this->createMock(SearchCriteriaBuilder::class); |
| 114 | + $searchCriteriaBuilder2 = $this->createMock(SearchCriteriaBuilder::class); |
| 115 | + $searchCriteriaBuilders = [$searchCriteriaBuilder1, $searchCriteriaBuilder2]; |
| 116 | + $sourceItemSearchResult = $this->createMock(SourceItemSearchResultsInterface::class); |
| 117 | + $sourceSearchResult = $this->createMock(SourceSearchResultsInterface::class); |
| 118 | + $this->searchCriteriaBuilderFactory->expects($this->exactly(2)) |
| 119 | + ->method('create') |
| 120 | + ->willReturnCallback( |
| 121 | + function () use (&$searchCriteriaBuilders) { |
| 122 | + return array_shift($searchCriteriaBuilders); |
| 123 | + } |
| 124 | + ); |
| 125 | + |
| 126 | + $searchCriteriaBuilder1->expects($this->once()) |
| 127 | + ->method('addFilter') |
| 128 | + ->with(SourceItemInterface::SKU, $skus, 'in') |
| 129 | + ->willReturnSelf(); |
| 130 | + $searchCriteriaBuilder1 |
| 131 | + ->expects($this->once())->method('create') |
| 132 | + ->willReturn($searchCriteria1); |
| 133 | + |
| 134 | + $searchCriteriaBuilder2->expects($this->once()) |
| 135 | + ->method('addFilter') |
| 136 | + ->with(SourceInterface::SOURCE_CODE, ['source_code1', 'source_code2'], 'in') |
| 137 | + ->willReturnSelf(); |
| 138 | + $searchCriteriaBuilder2->expects($this->once()) |
| 139 | + ->method('create') |
| 140 | + ->willReturn($searchCriteria2); |
| 141 | + |
| 142 | + $sourceItemSearchResult->expects($this->once()) |
| 143 | + ->method('getItems') |
| 144 | + ->willReturn($sourceItems); |
| 145 | + $this->sourceItemRepository->expects($this->once()) |
| 146 | + ->method('getList') |
| 147 | + ->with($searchCriteria1) |
| 148 | + ->willReturn($sourceItemSearchResult); |
| 149 | + |
| 150 | + $sourceSearchResult->expects($this->once()) |
| 151 | + ->method('getItems') |
| 152 | + ->willReturn($sources); |
| 153 | + $this->sourceRepository->expects($this->once()) |
| 154 | + ->method('getList') |
| 155 | + ->with($searchCriteria2) |
| 156 | + ->willReturn($sourceSearchResult); |
| 157 | + |
| 158 | + $this->assertEquals($this->getExpectedResult($skus), $this->model->execute($skus)); |
| 159 | + } |
| 160 | + |
| 161 | + private function getExpectedResult(array $skus): array |
| 162 | + { |
| 163 | + $result = []; |
| 164 | + foreach ($this->sourceItems as $sourceItem) { |
| 165 | + if (in_array($sourceItem['getSku'], $skus, true)) { |
| 166 | + $result[$sourceItem['getSku']][] = [ |
| 167 | + 'source_code' => $sourceItem['getSourceCode'], |
| 168 | + 'quantity' => $sourceItem['getQuantity'], |
| 169 | + 'source_name' => $this->sources[$sourceItem['getSourceCode']]['getName'], |
| 170 | + 'status' => $sourceItem['getStatus'], |
| 171 | + ]; |
| 172 | + } |
| 173 | + } |
| 174 | + return $result; |
| 175 | + } |
| 176 | +} |
0 commit comments