Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#32540 Throw error when updating stock when auto increment is exceed… #3309

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Inventory/Model/ResourceModel/SourceItem/SaveMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ public function execute(array $sourceItems)
SourceItemInterface::STATUS
]);
$valuesSql = $this->buildValuesSqlPart($sourceItems);
$onDuplicateSql = $this->buildOnDuplicateSqlPart([
SourceItemInterface::QUANTITY,
SourceItemInterface::STATUS,
]);
$bind = $this->getSqlBindData($sourceItems);

/* can not use "INSERT ... ON DUPLICATE KEY UPDATE" statement against a table having more than one unique
or primary key because it is unsafe */
$insertSql = sprintf(
'INSERT INTO `%s` (%s) VALUES %s %s',
'INSERT INTO `%s` (%s) VALUES %s',
$tableName,
$columnsSql,
$valuesSql,
$onDuplicateSql
$valuesSql
);
$connection->query($insertSql, $bind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Validation\ValidationException;
use Magento\Inventory\Model\ResourceModel\SourceItem\DeleteMultiple;
use Magento\Inventory\Model\ResourceModel\SourceItem\SaveMultiple;
use Magento\Inventory\Model\SourceItem\Validator\SourceItemsValidator;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
Expand All @@ -36,19 +37,27 @@ class SourceItemsSaveHandler
*/
private $logger;

/**
* @var DeleteMultiple
*/
private $deleteMultiple;

/**
* @param SourceItemsValidator $sourceItemsValidator
* @param DeleteMultiple $deleteMultiple
* @param SaveMultiple $saveMultiple
* @param LoggerInterface $logger
*/
public function __construct(
SourceItemsValidator $sourceItemsValidator,
DeleteMultiple $deleteMultiple,
SaveMultiple $saveMultiple,
LoggerInterface $logger
) {
$this->sourceItemsValidator = $sourceItemsValidator;
$this->saveMultiple = $saveMultiple;
$this->logger = $logger;
$this->deleteMultiple = $deleteMultiple;
}

/**
Expand All @@ -73,6 +82,7 @@ public function execute(array $sourceItems)
}

try {
$this->deleteMultiple->execute($sourceItems);
$this->saveMultiple->execute($sourceItems);
} catch (Exception $e) {
$this->logger->error($e->getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Test\Integration\Model\SourceItem\Command\Handler;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\Inventory\Model\ResourceModel\SourceItem;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\Inventory\Model\SourceItem\Command\Handler\SourceItemsSaveHandler;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test SourceItemsSaveHandler
*/
class SourceItemsSaveHandlerTest extends TestCase
{
/**
* @var SourceItemsSaveHandler
*/
private $sourceItemsSaveHandler;

/**
* @var SourceItemInterfaceFactory
*/
private $sourceItemFactory;

/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->sourceItemsSaveHandler = $objectManager->get(SourceItemsSaveHandler::class);
$this->sourceItemFactory = $objectManager->get(SourceItemInterfaceFactory::class);
$this->defaultSourceProvider = $objectManager->get(DefaultSourceProviderInterface::class);
}

/**
* Make sure exception is thrown when max auto_increment in inventory_source_item is reached
*
* @magentoDataFixture Magento_InventoryApi::Test/_files/products.php
* @magentoDbIsolation disabled
*/
public function testMaxAutoIncrementIsReached(): void
{
$this->expectException(CouldNotSaveException::class);
/** @var ResourceConnection $resourceConnection */
$resourceConnection = Bootstrap::getObjectManager()->get(ResourceConnection::class);

//insert record with maximum increment to inventory_source_item table
$tableName = $resourceConnection->getTableName('inventory_source_item');
$maxIncrementValue = 4294967295;

$resourceConnection->getConnection()->insert($tableName, [
SourceItem::ID_FIELD_NAME => $maxIncrementValue,
SourceItemInterface::SOURCE_CODE => $this->defaultSourceProvider->getCode(),
SourceItemInterface::SKU => 'dummy-sku',
SourceItemInterface::QUANTITY => 100,
SourceItemInterface::STATUS => 1
]);

$sourceItemParams = [
'data' => [
SourceItemInterface::SOURCE_CODE => $this->defaultSourceProvider->getCode(),
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 100,
SourceItemInterface::STATUS => 1
]
];

$sourceItem = $this->sourceItemFactory->create($sourceItemParams);

$this->sourceItemsSaveHandler->execute([$sourceItem]);
}

/**
* @inheritdoc
*/
protected function tearDown(): void
{
/** @var ResourceConnection $resourceConnection */
$resourceConnection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
$sourceItemTableName = $resourceConnection->getTableName('inventory_source_item');

//reset auto_increment
$resourceConnection->getConnection()
->delete($sourceItemTableName, [SourceItemInterface::SKU . ' = (?)' => 'dummy-sku']);
$maxSourceItemIdQuery = sprintf("select max(%s) from %s", SourceItem::ID_FIELD_NAME, $sourceItemTableName);
$maxSourceItemId = $resourceConnection->getConnection()->fetchOne($maxSourceItemIdQuery);
$autoIncrementValue = (int)$maxSourceItemId + 1;
$setIncrementQuery = sprintf("ALTER TABLE %s AUTO_INCREMENT = %s", $sourceItemTableName, $autoIncrementValue);
$resourceConnection->getConnection()->query($setIncrementQuery);

parent::tearDown();
}
}