Skip to content

Commit

Permalink
Merge pull request magento#221 from magento-engcom/199_assign_main_we…
Browse files Browse the repository at this point in the history
…bsite_to_default_stock

magento#199 - Assign main website to default stock
  • Loading branch information
Valeriy Nayda authored Nov 24, 2017
2 parents 91795d5 + 5bd6991 commit d33c139
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 26 deletions.
10 changes: 5 additions & 5 deletions app/code/Magento/Inventory/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*",
"magento/framework": "100.3.*",
"magento/module-inventory-api": "100.0.*",
"magento/module-backend": "100.2.*",
"magento/module-directory": "100.2.*",
"magento/module-shipping": "100.2.*",
"magento/module-ui": "100.2.*"
"magento/module-backend": "100.3.*",
"magento/module-directory": "100.3.*",
"magento/module-shipping": "100.3.*",
"magento/module-ui": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/InventoryApi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*"
"magento/framework": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down
12 changes: 6 additions & 6 deletions app/code/Magento/InventoryCatalog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*",
"magento/framework": "100.3.*",
"magento/module-inventory-api": "100.0.*",
"magento/module-backend": "100.2.*",
"magento/module-catalog": "100.2.*",
"magento/module-directory": "100.2.*",
"magento/module-shipping": "100.2.*",
"magento/module-ui": "100.2.*"
"magento/module-backend": "100.3.*",
"magento/module-catalog": "100.3.*",
"magento/module-directory": "100.3.*",
"magento/module-shipping": "100.3.*",
"magento/module-ui": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down
12 changes: 6 additions & 6 deletions app/code/Magento/InventoryImportExport/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*",
"magento/framework": "100.3.*",
"magento/module-inventory-api": "100.0.*",
"magento/module-backend": "100.2.*",
"magento/module-catalog": "100.2.*",
"magento/module-directory": "100.2.*",
"magento/module-shipping": "100.2.*",
"magento/module-ui": "100.2.*"
"magento/module-backend": "100.3.*",
"magento/module-catalog": "100.3.*",
"magento/module-directory": "100.3.*",
"magento/module-shipping": "100.3.*",
"magento/module-ui": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down
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());
}
}
7 changes: 4 additions & 3 deletions app/code/Magento/InventorySales/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*",
"magento/module-ui": "100.2.*",
"magento/module-inventory-sales-api": "100.0.*"
"magento/framework": "100.3.*",
"magento/module-ui": "100.3.*",
"magento/module-inventory-sales-api": "100.0.*",
"magento/module-store": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/InventorySalesApi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "N/A",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.2.*"
"magento/framework": "100.3.*"
},
"type": "magento2-module",
"version": "100.0.0-dev",
Expand Down

0 comments on commit d33c139

Please sign in to comment.