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

#199 - Assign main website to default stock #221

Merged
merged 4 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 42 additions & 0 deletions app/code/Magento/InventorySales/Setup/InstallData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\InventorySales\Setup\Operation\AssignWebsiteToDefaultStock;

/**
* Assigns Main website to the Default stock
*/
class InstallData implements InstallDataInterface
{
/**
* @var AssignWebsiteToDefaultStock
*/
private $assignWebsiteToDefaultStock;

/**
* @param AssignWebsiteToDefaultStock $assignWebsiteToDefaultStock
*/
public function __construct(
AssignWebsiteToDefaultStock $assignWebsiteToDefaultStock
) {
$this->assignWebsiteToDefaultStock = $assignWebsiteToDefaultStock;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->assignWebsiteToDefaultStock->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Setup\Operation;

use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Assigns Main website to the Default stock
*/
class AssignWebsiteToDefaultStock
{
/**
* @var StockRepositoryInterface
*/
private $stockRepository;

/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* @var SalesChannelInterfaceFactory
*/
private $salesChannelFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StockRepositoryInterface $stockRepository
* @param DefaultStockProviderInterface $defaultStockProvider
* @param SalesChannelInterfaceFactory $salesChannelFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StockRepositoryInterface $stockRepository,
DefaultStockProviderInterface $defaultStockProvider,
SalesChannelInterfaceFactory $salesChannelFactory,
StoreManagerInterface $storeManager
) {
$this->stockRepository = $stockRepository;
$this->defaultStockProvider = $defaultStockProvider;
$this->salesChannelFactory = $salesChannelFactory;
$this->storeManager = $storeManager;
}

/**
* @inheritdoc
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Validation\ValidationException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute()
{
$websiteCode = $this->storeManager->getWebsite()->getCode();

$defaultStockId = $this->defaultStockProvider->getId();
$defaultStock = $this->stockRepository->get($defaultStockId);

$extensionAttributes = $defaultStock->getExtensionAttributes();
$salesChannels = $extensionAttributes->getSalesChannels();
$salesChannels[] = $this->createSalesChannelByWebsiteCode($websiteCode);

$extensionAttributes->setSalesChannels($salesChannels);
$this->stockRepository->save($defaultStock);
}

/**
* Create the sales channel by given website code
*
* @param string $websiteCode
* @return SalesChannelInterface
*/
private function createSalesChannelByWebsiteCode(string $websiteCode): SalesChannelInterface
{
$salesChannel = $this->salesChannelFactory->create();
$salesChannel->setCode($websiteCode);
$salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE);
return $salesChannel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website;
use Magento\Store\Model\WebsiteFactory;
use Magento\TestFramework\Helper\Bootstrap;
Expand All @@ -32,11 +33,37 @@ class AssignWebsiteToDefaultStockTest extends TestCase
*/
private $defaultStockProvider;

/**
* @var StoreManagerInterface
*/
private $storeManager;

protected function setUp()
{
$this->websiteFactory = Bootstrap::getObjectManager()->get(WebsiteFactory::class);
$this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
$this->defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class);
$this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
}

/**
* Test if Main website is associated to Default stock
*/
public function testIfWebsiteIsAssignedToDefaultStock()
{
$websiteCode = $this->storeManager->getWebsite()->getCode();

$defaultStockId = $this->defaultStockProvider->getId();
$defaultStock = $this->stockRepository->get($defaultStockId);

$extensionAttributes = $defaultStock->getExtensionAttributes();
$salesChannels = $extensionAttributes->getSalesChannels();
self::assertContainsOnlyInstancesOf(SalesChannelInterface::class, $salesChannels);
self::assertCount(1, $salesChannels);

$salesChannel = reset($salesChannels);
self::assertEquals($websiteCode, $salesChannel->getCode());
self::assertEquals(SalesChannelInterface::TYPE_WEBSITE, $salesChannel->getType());
}

/**
Expand All @@ -59,10 +86,15 @@ public function testCreateWebsiteIfSalesChannelsAreEmpty()
$extensionAttributes = $defaultStock->getExtensionAttributes();
$salesChannels = $extensionAttributes->getSalesChannels();
self::assertContainsOnlyInstancesOf(SalesChannelInterface::class, $salesChannels);
self::assertCount(1, $salesChannels);

$salesChannel = reset($salesChannels);
self::assertEquals($website->getCode(), $salesChannel->getCode());
self::assertEquals(SalesChannelInterface::TYPE_WEBSITE, $salesChannel->getType());
$salesChannelsOfCreatedWebsite = array_filter($salesChannels, function ($aSalesChannel) use ($websiteCode) {
return $aSalesChannel->getCode() === $websiteCode;
});

self::assertCount(1, $salesChannelsOfCreatedWebsite);

$aSalesChannelOfCreatedWebsite = reset($salesChannelsOfCreatedWebsite);
self::assertEquals($website->getCode(), $aSalesChannelOfCreatedWebsite->getCode());
self::assertEquals(SalesChannelInterface::TYPE_WEBSITE, $aSalesChannelOfCreatedWebsite->getType());
}
}