cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3.1 integration test: "No linked stock found"

SOLVED
Highlighted

Magento 2.3.1 integration test: "No linked stock found"

I want to write a simple integration test that takes a quote and orders it.

For that I wrote a fixture, where I basically copied everythng over from the default Magento integration test fixtures, e.g. I create a product similar to this:

https://github.com/magento/magento2/blob/2.3-develop/dev/tests/integration/testsuite/Magento/Catalog...

and a quote similat to that:

https://github.com/magento/magento2/blob/2.3-develop/dev/tests/integration/testsuite/Magento/Checkou...

 

The problem is, that the fixture always fails when adding the product to the cart with the "addProduct" method.

I'll get the error:

No linked stock found
#0 vendor/magento/framework/Interception/Interceptor.php(58): Magento\InventorySales\Model\GetStockBySalesChannel->execute(Object(Magento\InventorySales\Model\SalesChannel))

 

although I don't even have the MSI modules enabled!
Anybody with the same problem and an idea how to fix that?

 

For reference, here the actual code:

/** @var ProductInterfaceFactory $productFactory */
$productFactory = Bootstrap::getObjectManager()->create(ProductInterfaceFactory::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);

$product = $productFactory->create();
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL)
    ->setId(1)
    ->setAttributeSetId(4)
    ->setName('Virtual Product')
    ->setSku('virtual-product')
    ->setPrice(10)
    ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
    ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
    ->setStockData(
        [
            'qty' => 100,
            'is_in_stock' => 1,
            'manage_stock' => 1,
            'is_qty_decimal' => true,
        ]
    );
$productRepository->save($product);
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
    ->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->get('virtual-product');

$store = Bootstrap::getObjectManager()
    ->get(\Magento\Store\Model\StoreManagerInterface::class)
    ->getStore();

//Creating the addresses omitted

/** @var \Magento\Quote\Model\Quote $quote */
$quote = Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
$quote->setCustomerIsGuest(true)
    ->setStoreId($store->getId())
    ->setReservedOrderId('test01')
    ->setBillingAddress($billingAddress)
    ->setShippingAddress($shippingAddress)
    ->addProduct($product);
//Error happens at the addProduct above
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Magento 2.3.1 integration test: "No linked stock found"

We have found the issue by now. It's caused by how we did set up our stores.
We used to have a DataPatch where we would insert stores and websites directly into the database.
When running integration tests, all modules are enabled by default, so is also MSI. MSI works by having observers around specific data types.
It will link a default stock to a website when you save the website using the resource model. Since we did not use the proper model to create the website, but inserted the data directly to the DB, this mechanism was circumvented and lead to said error during integration tests.

View solution in original post

3 REPLIES 3
Highlighted

Re: Magento 2.3.1 integration test: "No linked stock found"

Small correction: During the integration tests MSI seems to be enabled after all

/** @var Manager $moduleManager */
$moduleManager = Bootstrap::getObjectManager()->get(Manager::class);
echo('MSI enabled: ' . $moduleManager->isEnabled('Magento_InventoryCatalog'));

shows 

MSI enabled: 1

which is weird, since it's clearly disabled in app/etc/config.php.

Anyway, I already tried to create a product like they are done in the MSI integration tests like here:

https://github.com/magento-engcom/msi/blob/2.3-develop/app/code/Magento/InventoryApi/Test/_files/pro...

Except for the last part (Unassign created product from default Source). Does that need to be done maybe? 

 

Highlighted

Re: Magento 2.3.1 integration test: "No linked stock found"

We have found the issue by now. It's caused by how we did set up our stores.
We used to have a DataPatch where we would insert stores and websites directly into the database.
When running integration tests, all modules are enabled by default, so is also MSI. MSI works by having observers around specific data types.
It will link a default stock to a website when you save the website using the resource model. Since we did not use the proper model to create the website, but inserted the data directly to the DB, this mechanism was circumvented and lead to said error during integration tests.

View solution in original post

Highlighted

Re: Magento 2.3.1 integration test: "No linked stock found"

 

Go to Stores -> Inventory -> Sources, add new Sources

Go to Stores -> Inventory -> Stocks Add new Stocks and select all stores, and save Stocks.

 

Issue will resolve.

 

Thanks