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:
and a quote similat to that:
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
Solved! Go to Solution.
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.
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:
Except for the last part (Unassign created product from default Source). Does that need to be done maybe?
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.
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