init
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LingxingErp\Test\Unit\Model\Inventory;
|
||||
|
||||
use Esun\LingxingErp\Model\Api\Client;
|
||||
use Esun\LingxingErp\Model\Api\Endpoint;
|
||||
use Esun\LingxingErp\Model\Inventory\InventoryFetcher;
|
||||
use Esun\LingxingErp\Model\Sync\LogWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class InventoryFetcherTest extends TestCase
|
||||
{
|
||||
public function testUsesProductInventoryEndpointAndWarehouseFilter(): void
|
||||
{
|
||||
$client = $this->createMock(Client::class);
|
||||
$client->expects(self::once())
|
||||
->method('post')
|
||||
->with(Endpoint::INVENTORY_DETAILS, [
|
||||
'wid' => '46133',
|
||||
'offset' => 0,
|
||||
'length' => 800,
|
||||
])
|
||||
->willReturn([
|
||||
'code' => 0,
|
||||
'data' => [['wid' => 46133, 'sku' => 'ERP-1', 'product_valid_num' => 12]],
|
||||
'total' => 1,
|
||||
]);
|
||||
$logWriter = $this->createMock(LogWriter::class);
|
||||
$logWriter->expects(self::once())->method('write');
|
||||
|
||||
$result = (new InventoryFetcher($client, $logWriter))->fetchAll(46133);
|
||||
|
||||
self::assertSame('ERP-1', $result[0]['sku']);
|
||||
self::assertSame(12, $result[0]['product_valid_num']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LingxingErp\Test\Unit\Model\Inventory;
|
||||
|
||||
use Esun\LingxingErp\Model\Inventory\InventoryFetcher;
|
||||
use Esun\LingxingErp\Model\Inventory\InventorySynchronizer;
|
||||
use Esun\LingxingErp\Model\Sku\ProductErpSkuRepository;
|
||||
use Esun\LingxingErp\Model\Sync\LogWriter;
|
||||
use Esun\LingxingErp\Model\Warehouse\WarehouseRepository;
|
||||
use Magento\Framework\Lock\LockManagerInterface;
|
||||
use Magento\InventoryApi\Api\Data\SourceItemInterface;
|
||||
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
|
||||
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class InventorySynchronizerTest extends TestCase
|
||||
{
|
||||
public function testFullErpQuantityIsWrittenToEveryPairedMagentoSku(): void
|
||||
{
|
||||
$warehouseRepository = $this->createMock(WarehouseRepository::class);
|
||||
$warehouseRepository->method('getWebsiteIdsForSource')->with('default')->willReturn([3]);
|
||||
$warehouseRepository->method('getExistingSourceItemSkus')->with('default')->willReturn([]);
|
||||
$fetcher = $this->createMock(InventoryFetcher::class);
|
||||
$fetcher->method('fetchAll')->with(10)->willReturn([
|
||||
['sku' => 'ERP-1', 'product_valid_num' => 30],
|
||||
]);
|
||||
$productRepository = $this->createMock(ProductErpSkuRepository::class);
|
||||
$productRepository->method('findMskusByErpSku')->with(3, 'ERP-1')
|
||||
->willReturn(['MAGENTO-A', 'MAGENTO-B']);
|
||||
$productRepository->method('getWebsiteProductSkusWithErpSku')->with(3)
|
||||
->willReturn(['MAGENTO-A', 'MAGENTO-B']);
|
||||
|
||||
$itemA = $this->createMock(SourceItemInterface::class);
|
||||
$itemA->expects(self::once())->method('setSourceCode')->with('default');
|
||||
$itemA->expects(self::once())->method('setSku')->with('MAGENTO-A');
|
||||
$itemA->expects(self::once())->method('setQuantity')->with(30.0);
|
||||
$itemA->expects(self::once())->method('setStatus')->with(SourceItemInterface::STATUS_IN_STOCK);
|
||||
$itemB = $this->createMock(SourceItemInterface::class);
|
||||
$itemB->expects(self::once())->method('setSourceCode')->with('default');
|
||||
$itemB->expects(self::once())->method('setSku')->with('MAGENTO-B');
|
||||
$itemB->expects(self::once())->method('setQuantity')->with(30.0);
|
||||
$itemB->expects(self::once())->method('setStatus')->with(SourceItemInterface::STATUS_IN_STOCK);
|
||||
$itemFactory = $this->createMock(SourceItemInterfaceFactory::class);
|
||||
$itemFactory->expects(self::exactly(2))->method('create')->willReturn($itemA, $itemB);
|
||||
$sourceItemsSave = $this->createMock(SourceItemsSaveInterface::class);
|
||||
$sourceItemsSave->expects(self::once())->method('execute')->with([$itemA, $itemB]);
|
||||
|
||||
$synchronizer = new InventorySynchronizer(
|
||||
$warehouseRepository,
|
||||
$fetcher,
|
||||
$productRepository,
|
||||
$itemFactory,
|
||||
$sourceItemsSave,
|
||||
$this->createMock(LogWriter::class),
|
||||
$this->createMock(LockManagerInterface::class)
|
||||
);
|
||||
$method = new \ReflectionMethod($synchronizer, 'synchronizeWarehouse');
|
||||
|
||||
self::assertSame(
|
||||
[
|
||||
'items' => 2,
|
||||
'unmapped' => 0,
|
||||
'zeroed' => 0,
|
||||
'preserved_without_erp_sku' => 0,
|
||||
'unmapped_erp_skus' => [],
|
||||
],
|
||||
$method->invoke($synchronizer, ['wid' => 10, 'source_code' => 'default'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testDoesNotZeroExistingSourceItemsWithoutAnErpSkuMapping(): void
|
||||
{
|
||||
$warehouseRepository = $this->createMock(WarehouseRepository::class);
|
||||
$warehouseRepository->method('getWebsiteIdsForSource')->with('default')->willReturn([3]);
|
||||
$warehouseRepository->method('getExistingSourceItemSkus')->with('default')->willReturn([
|
||||
'MAPPED-BUT-MISSING',
|
||||
'NO-ERP-SKU',
|
||||
]);
|
||||
$fetcher = $this->createMock(InventoryFetcher::class);
|
||||
$fetcher->method('fetchAll')->with(10)->willReturn([
|
||||
['sku' => 'UNKNOWN-ERP-SKU', 'product_valid_num' => 50],
|
||||
]);
|
||||
$productRepository = $this->createMock(ProductErpSkuRepository::class);
|
||||
$productRepository->method('findMskusByErpSku')->with(3, 'UNKNOWN-ERP-SKU')->willReturn([]);
|
||||
$productRepository->method('getWebsiteProductSkusWithErpSku')->with(3)
|
||||
->willReturn(['MAPPED-BUT-MISSING']);
|
||||
|
||||
$zeroedItem = $this->createMock(SourceItemInterface::class);
|
||||
$zeroedItem->expects(self::once())->method('setSourceCode')->with('default');
|
||||
$zeroedItem->expects(self::once())->method('setSku')->with('MAPPED-BUT-MISSING');
|
||||
$zeroedItem->expects(self::once())->method('setQuantity')->with(0.0);
|
||||
$zeroedItem->expects(self::once())->method('setStatus')->with(SourceItemInterface::STATUS_OUT_OF_STOCK);
|
||||
$itemFactory = $this->createMock(SourceItemInterfaceFactory::class);
|
||||
$itemFactory->expects(self::once())->method('create')->willReturn($zeroedItem);
|
||||
$sourceItemsSave = $this->createMock(SourceItemsSaveInterface::class);
|
||||
$sourceItemsSave->expects(self::once())->method('execute')->with([$zeroedItem]);
|
||||
|
||||
$synchronizer = new InventorySynchronizer(
|
||||
$warehouseRepository,
|
||||
$fetcher,
|
||||
$productRepository,
|
||||
$itemFactory,
|
||||
$sourceItemsSave,
|
||||
$this->createMock(LogWriter::class),
|
||||
$this->createMock(LockManagerInterface::class)
|
||||
);
|
||||
$method = new \ReflectionMethod($synchronizer, 'synchronizeWarehouse');
|
||||
|
||||
self::assertSame(
|
||||
[
|
||||
'items' => 1,
|
||||
'unmapped' => 1,
|
||||
'zeroed' => 1,
|
||||
'preserved_without_erp_sku' => 1,
|
||||
'unmapped_erp_skus' => ['UNKNOWN-ERP-SKU'],
|
||||
],
|
||||
$method->invoke($synchronizer, ['wid' => 10, 'source_code' => 'default'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvesEveryMagentoSkuPairedToAnErpSku(): void
|
||||
{
|
||||
$productRepository = $this->createMock(ProductErpSkuRepository::class);
|
||||
$productRepository->expects(self::exactly(2))
|
||||
->method('findMskusByErpSku')
|
||||
->willReturnMap([
|
||||
[3, 'ERP-1', ['MAGENTO-A', 'MAGENTO-B']],
|
||||
[8, 'ERP-1', ['MAGENTO-B', 'MAGENTO-C']],
|
||||
]);
|
||||
$synchronizer = new InventorySynchronizer(
|
||||
$this->createMock(WarehouseRepository::class),
|
||||
$this->createMock(InventoryFetcher::class),
|
||||
$productRepository,
|
||||
$this->createMock(SourceItemInterfaceFactory::class),
|
||||
$this->createMock(SourceItemsSaveInterface::class),
|
||||
$this->createMock(LogWriter::class),
|
||||
$this->createMock(LockManagerInterface::class)
|
||||
);
|
||||
$method = new \ReflectionMethod($synchronizer, 'resolveMagentoSkus');
|
||||
|
||||
self::assertSame(
|
||||
['MAGENTO-A', 'MAGENTO-B', 'MAGENTO-C'],
|
||||
$method->invoke($synchronizer, [3, 8], 'ERP-1')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user