This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Esun\ProductInquiry\Test\Unit\Model;
use Esun\ProductInquiry\Model\AvailabilityResolver;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Api\Data\WebsiteInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class AvailabilityResolverTest extends TestCase
{
private StockRegistryInterface&MockObject $stockRegistry;
private StockResolverInterface&MockObject $stockResolver;
private IsProductSalableInterface&MockObject $isProductSalable;
private ProductInterface&MockObject $product;
private WebsiteInterface&MockObject $website;
private StockItemInterface&MockObject $stockItem;
private StockInterface&MockObject $stock;
protected function setUp(): void
{
$this->stockRegistry = $this->createMock(StockRegistryInterface::class);
$this->stockResolver = $this->createMock(StockResolverInterface::class);
$this->isProductSalable = $this->createMock(IsProductSalableInterface::class);
$this->product = $this->createMock(ProductInterface::class);
$this->website = $this->createMock(WebsiteInterface::class);
$this->stockItem = $this->createMock(StockItemInterface::class);
$this->stock = $this->createMock(StockInterface::class);
$this->product->method('getId')->willReturn(100);
$this->product->method('getSku')->willReturn('TEST-SKU');
$this->website->method('getId')->willReturn(2);
$this->website->method('getCode')->willReturn('base');
$this->stock->method('getStockId')->willReturn(1);
$this->stockRegistry->method('getStockItem')->with(100, 2)->willReturn($this->stockItem);
$this->stockResolver->method('execute')->with('website', 'base')->willReturn($this->stock);
}
#[DataProvider('availabilityProvider')]
public function testLegacyAndMsiAvailabilityAreBothRespected(
bool $legacyInStock,
bool $msiSalable,
string $expectedReason,
bool $requiresInquiry
): void {
$this->stockItem->method('getIsInStock')->willReturn($legacyInStock);
$this->isProductSalable->method('execute')->with('TEST-SKU', 1)->willReturn($msiSalable);
$result = (new AvailabilityResolver(
$this->stockRegistry,
$this->stockResolver,
$this->isProductSalable
))->resolve($this->product, $this->website);
self::assertSame((int)$legacyInStock, $result['stock_status']);
self::assertSame((int)$msiSalable, $result['is_salable']);
self::assertSame(1, $result['stock_id']);
self::assertSame($expectedReason, $result['reason']);
self::assertSame($requiresInquiry, $result['requires_inquiry']);
}
public static function availabilityProvider(): array
{
return [
'available in both systems' => [true, true, 'salable', false],
'manually out of stock' => [false, true, 'manual_out_of_stock', true],
'not salable in MSI' => [true, false, 'msi_not_salable', true],
'unavailable in both systems' => [false, false, 'both', true],
];
}
}